]>
Commit | Line | Data |
---|---|---|
ce4fd7b5 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/debugrpt.cpp | |
3 | // Purpose: wxDebugReport and related classes implementation | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 2005-01-17 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2005 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
526954c5 | 9 | // Licence: wxWindows licence |
ce4fd7b5 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #ifndef WX_PRECOMP | |
27 | #include "wx/app.h" | |
28 | #include "wx/log.h" | |
29 | #include "wx/intl.h" | |
2b1d737b | 30 | #include "wx/utils.h" |
ce4fd7b5 VZ |
31 | #endif // WX_PRECOMP |
32 | ||
2cbe2aea | 33 | #if wxUSE_DEBUGREPORT && wxUSE_XML |
ce4fd7b5 VZ |
34 | |
35 | #include "wx/debugrpt.h" | |
36 | ||
a44f14cf | 37 | #include "wx/ffile.h" |
ce4fd7b5 VZ |
38 | #include "wx/filename.h" |
39 | #include "wx/dir.h" | |
40 | #include "wx/dynlib.h" | |
41 | ||
42 | #include "wx/xml/xml.h" | |
43 | ||
44 | #if wxUSE_STACKWALKER | |
45 | #include "wx/stackwalk.h" | |
46 | #endif | |
47 | ||
48 | #if wxUSE_CRASHREPORT | |
49 | #include "wx/msw/crashrpt.h" | |
50 | #endif | |
51 | ||
52 | #if wxUSE_ZIPSTREAM | |
53 | #include "wx/wfstream.h" | |
54 | #include "wx/zipstrm.h" | |
55 | #endif // wxUSE_ZIPSTREAM | |
56 | ||
61639efb | 57 | WX_CHECK_BUILD_OPTIONS("wxQA") |
ce4fd7b5 VZ |
58 | |
59 | // ---------------------------------------------------------------------------- | |
60 | // XmlStackWalker: stack walker specialization which dumps stack in XML | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
61639efb VZ |
63 | #if wxUSE_STACKWALKER |
64 | ||
ce4fd7b5 VZ |
65 | class XmlStackWalker : public wxStackWalker |
66 | { | |
67 | public: | |
68 | XmlStackWalker(wxXmlNode *nodeStack) | |
69 | { | |
70 | m_isOk = false; | |
71 | m_nodeStack = nodeStack; | |
72 | } | |
73 | ||
74 | bool IsOk() const { return m_isOk; } | |
75 | ||
76 | protected: | |
77 | virtual void OnStackFrame(const wxStackFrame& frame); | |
78 | ||
79 | wxXmlNode *m_nodeStack; | |
80 | bool m_isOk; | |
81 | }; | |
82 | ||
ce4fd7b5 VZ |
83 | // ---------------------------------------------------------------------------- |
84 | // local functions | |
85 | // ---------------------------------------------------------------------------- | |
86 | ||
87 | static inline void | |
88 | HexProperty(wxXmlNode *node, const wxChar *name, unsigned long value) | |
89 | { | |
9a83f860 | 90 | node->AddAttribute(name, wxString::Format(wxT("%08lx"), value)); |
ce4fd7b5 VZ |
91 | } |
92 | ||
93 | static inline void | |
94 | NumProperty(wxXmlNode *node, const wxChar *name, unsigned long value) | |
95 | { | |
9a83f860 | 96 | node->AddAttribute(name, wxString::Format(wxT("%lu"), value)); |
ce4fd7b5 VZ |
97 | } |
98 | ||
99 | static inline void | |
100 | TextElement(wxXmlNode *node, const wxChar *name, const wxString& value) | |
101 | { | |
102 | wxXmlNode *nodeChild = new wxXmlNode(wxXML_ELEMENT_NODE, name); | |
103 | node->AddChild(nodeChild); | |
7a893a31 | 104 | nodeChild->AddChild(new wxXmlNode(wxXML_TEXT_NODE, wxEmptyString, value)); |
ce4fd7b5 VZ |
105 | } |
106 | ||
17a1ebd1 VZ |
107 | #if wxUSE_CRASHREPORT && defined(__INTEL__) |
108 | ||
ce4fd7b5 VZ |
109 | static inline void |
110 | HexElement(wxXmlNode *node, const wxChar *name, unsigned long value) | |
111 | { | |
9a83f860 | 112 | TextElement(node, name, wxString::Format(wxT("%08lx"), value)); |
ce4fd7b5 VZ |
113 | } |
114 | ||
17a1ebd1 VZ |
115 | #endif // wxUSE_CRASHREPORT |
116 | ||
ce4fd7b5 VZ |
117 | // ============================================================================ |
118 | // XmlStackWalker implementation | |
119 | // ============================================================================ | |
120 | ||
121 | void XmlStackWalker::OnStackFrame(const wxStackFrame& frame) | |
122 | { | |
123 | m_isOk = true; | |
124 | ||
9a83f860 | 125 | wxXmlNode *nodeFrame = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("frame")); |
ce4fd7b5 VZ |
126 | m_nodeStack->AddChild(nodeFrame); |
127 | ||
9a83f860 | 128 | NumProperty(nodeFrame, wxT("level"), frame.GetLevel()); |
ce4fd7b5 VZ |
129 | wxString func = frame.GetName(); |
130 | if ( !func.empty() ) | |
131 | { | |
9a83f860 VZ |
132 | nodeFrame->AddAttribute(wxT("function"), func); |
133 | HexProperty(nodeFrame, wxT("offset"), frame.GetOffset()); | |
ce4fd7b5 VZ |
134 | } |
135 | ||
136 | if ( frame.HasSourceLocation() ) | |
137 | { | |
9a83f860 VZ |
138 | nodeFrame->AddAttribute(wxT("file"), frame.GetFileName()); |
139 | NumProperty(nodeFrame, wxT("line"), frame.GetLine()); | |
ce4fd7b5 VZ |
140 | } |
141 | ||
142 | const size_t nParams = frame.GetParamCount(); | |
143 | if ( nParams ) | |
144 | { | |
9a83f860 | 145 | wxXmlNode *nodeParams = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("parameters")); |
ce4fd7b5 VZ |
146 | nodeFrame->AddChild(nodeParams); |
147 | ||
148 | for ( size_t n = 0; n < nParams; n++ ) | |
149 | { | |
150 | wxXmlNode * | |
9a83f860 | 151 | nodeParam = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("parameter")); |
ce4fd7b5 VZ |
152 | nodeParams->AddChild(nodeParam); |
153 | ||
9a83f860 | 154 | NumProperty(nodeParam, wxT("number"), n); |
ce4fd7b5 VZ |
155 | |
156 | wxString type, name, value; | |
157 | if ( !frame.GetParam(n, &type, &name, &value) ) | |
158 | continue; | |
159 | ||
160 | if ( !type.empty() ) | |
9a83f860 | 161 | TextElement(nodeParam, wxT("type"), type); |
ce4fd7b5 VZ |
162 | |
163 | if ( !name.empty() ) | |
9a83f860 | 164 | TextElement(nodeParam, wxT("name"), name); |
ce4fd7b5 VZ |
165 | |
166 | if ( !value.empty() ) | |
9a83f860 | 167 | TextElement(nodeParam, wxT("value"), value); |
ce4fd7b5 VZ |
168 | } |
169 | } | |
170 | } | |
171 | ||
172 | #endif // wxUSE_STACKWALKER | |
173 | ||
174 | // ============================================================================ | |
175 | // wxDebugReport implementation | |
176 | // ============================================================================ | |
177 | ||
178 | // ---------------------------------------------------------------------------- | |
179 | // initialization and cleanup | |
180 | // ---------------------------------------------------------------------------- | |
181 | ||
182 | wxDebugReport::wxDebugReport() | |
183 | { | |
184 | // get a temporary directory name | |
2cdd63c6 | 185 | wxString appname = GetReportName(); |
ce4fd7b5 VZ |
186 | |
187 | // we can't use CreateTempFileName() because it creates a file, not a | |
188 | // directory, so do our best to create a unique name ourselves | |
189 | // | |
190 | // of course, this doesn't protect us against malicious users... | |
34af0de4 | 191 | #if wxUSE_DATETIME |
9a83f860 | 192 | m_dir.Printf(wxT("%s%c%s_dbgrpt-%lu-%s"), |
cc00695b | 193 | wxFileName::GetTempDir(), wxFILE_SEP_PATH, appname, |
ce4fd7b5 | 194 | wxGetProcessId(), |
cc00695b | 195 | wxDateTime::Now().Format(wxT("%Y%m%dT%H%M%S"))); |
34af0de4 | 196 | #else |
9a83f860 | 197 | m_dir.Printf(wxT("%s%c%s_dbgrpt-%lu"), |
cc00695b | 198 | wxFileName::GetTempDir(), wxFILE_SEP_PATH, appname, |
34af0de4 VZ |
199 | wxGetProcessId()); |
200 | #endif | |
ce4fd7b5 VZ |
201 | |
202 | // as we are going to save the process state there use restrictive | |
203 | // permissions | |
204 | if ( !wxMkdir(m_dir, 0700) ) | |
205 | { | |
206 | wxLogSysError(_("Failed to create directory \"%s\""), m_dir.c_str()); | |
207 | wxLogError(_("Debug report couldn't be created.")); | |
208 | ||
209 | Reset(); | |
210 | } | |
211 | } | |
212 | ||
213 | wxDebugReport::~wxDebugReport() | |
214 | { | |
215 | if ( !m_dir.empty() ) | |
216 | { | |
217 | // remove all files in this directory | |
218 | wxDir dir(m_dir); | |
219 | wxString file; | |
220 | for ( bool cont = dir.GetFirst(&file); cont; cont = dir.GetNext(&file) ) | |
221 | { | |
222 | if ( wxRemove(wxFileName(m_dir, file).GetFullPath()) != 0 ) | |
223 | { | |
224 | wxLogSysError(_("Failed to remove debug report file \"%s\""), | |
225 | file.c_str()); | |
226 | m_dir.clear(); | |
227 | break; | |
228 | } | |
229 | } | |
230 | } | |
231 | ||
232 | if ( !m_dir.empty() ) | |
233 | { | |
9034c590 JS |
234 | // Temp fix: what should this be? eVC++ doesn't like wxRmDir |
235 | #ifdef __WXWINCE__ | |
236 | if ( wxRmdir(m_dir.fn_str()) != 0 ) | |
237 | #else | |
2cdd63c6 | 238 | if ( wxRmDir(m_dir.fn_str()) != 0 ) |
9034c590 | 239 | #endif |
ce4fd7b5 VZ |
240 | { |
241 | wxLogSysError(_("Failed to clean up debug report directory \"%s\""), | |
242 | m_dir.c_str()); | |
243 | } | |
244 | } | |
245 | } | |
246 | ||
247 | // ---------------------------------------------------------------------------- | |
248 | // various helpers | |
249 | // ---------------------------------------------------------------------------- | |
250 | ||
251 | wxString wxDebugReport::GetReportName() const | |
252 | { | |
7735b2ea VZ |
253 | if ( wxTheApp ) |
254 | return wxTheApp->GetAppName(); | |
2cdd63c6 | 255 | |
9a83f860 | 256 | return wxT("wx"); |
ce4fd7b5 VZ |
257 | } |
258 | ||
fdc1aa52 VZ |
259 | void |
260 | wxDebugReport::AddFile(const wxString& filename, const wxString& description) | |
ce4fd7b5 | 261 | { |
fdc1aa52 VZ |
262 | wxString name; |
263 | wxFileName fn(filename); | |
264 | if ( fn.IsAbsolute() ) | |
265 | { | |
266 | // we need to copy the file to the debug report directory: give it the | |
267 | // same name there | |
268 | name = fn.GetFullName(); | |
c02f03d5 FM |
269 | |
270 | if (!wxCopyFile(fn.GetFullPath(), | |
271 | wxFileName(GetDirectory(), name).GetFullPath())) | |
272 | return; | |
fdc1aa52 VZ |
273 | } |
274 | else // file relative to the report directory | |
275 | { | |
276 | name = filename; | |
277 | ||
278 | wxASSERT_MSG( wxFileName(GetDirectory(), name).FileExists(), | |
9a83f860 | 279 | wxT("file should exist in debug report directory") ); |
fdc1aa52 VZ |
280 | } |
281 | ||
ce4fd7b5 VZ |
282 | m_files.Add(name); |
283 | m_descriptions.Add(description); | |
284 | } | |
285 | ||
e18c3e02 | 286 | bool |
fdc1aa52 | 287 | wxDebugReport::AddText(const wxString& filename, |
e18c3e02 VZ |
288 | const wxString& text, |
289 | const wxString& description) | |
290 | { | |
fdc1aa52 | 291 | wxASSERT_MSG( !wxFileName(filename).IsAbsolute(), |
9a83f860 | 292 | wxT("filename should be relative to debug report directory") ); |
fdc1aa52 VZ |
293 | |
294 | wxFileName fn(GetDirectory(), filename); | |
9a83f860 | 295 | wxFFile file(fn.GetFullPath(), wxT("w")); |
e18c3e02 VZ |
296 | if ( !file.IsOpened() || !file.Write(text) ) |
297 | return false; | |
298 | ||
fdc1aa52 | 299 | AddFile(filename, description); |
e18c3e02 VZ |
300 | |
301 | return true; | |
302 | } | |
303 | ||
ce4fd7b5 VZ |
304 | void wxDebugReport::RemoveFile(const wxString& name) |
305 | { | |
306 | const int n = m_files.Index(name); | |
9a83f860 | 307 | wxCHECK_RET( n != wxNOT_FOUND, wxT("No such file in wxDebugReport") ); |
ce4fd7b5 VZ |
308 | |
309 | m_files.RemoveAt(n); | |
310 | m_descriptions.RemoveAt(n); | |
311 | ||
312 | wxRemove(wxFileName(GetDirectory(), name).GetFullPath()); | |
313 | } | |
314 | ||
315 | bool wxDebugReport::GetFile(size_t n, wxString *name, wxString *desc) const | |
316 | { | |
317 | if ( n >= m_files.GetCount() ) | |
318 | return false; | |
319 | ||
320 | if ( name ) | |
321 | *name = m_files[n]; | |
322 | if ( desc ) | |
323 | *desc = m_descriptions[n]; | |
324 | ||
325 | return true; | |
326 | } | |
327 | ||
328 | void wxDebugReport::AddAll(Context context) | |
329 | { | |
330 | #if wxUSE_STACKWALKER | |
331 | AddContext(context); | |
332 | #endif // wxUSE_STACKWALKER | |
333 | ||
334 | #if wxUSE_CRASHREPORT | |
335 | AddDump(context); | |
336 | #endif // wxUSE_CRASHREPORT | |
2cdd63c6 WS |
337 | |
338 | #if !wxUSE_STACKWALKER && !wxUSE_CRASHREPORT | |
339 | wxUnusedVar(context); | |
340 | #endif | |
ce4fd7b5 VZ |
341 | } |
342 | ||
343 | // ---------------------------------------------------------------------------- | |
344 | // adding basic text information about current context | |
345 | // ---------------------------------------------------------------------------- | |
346 | ||
347 | #if wxUSE_STACKWALKER | |
348 | ||
349 | bool wxDebugReport::DoAddSystemInfo(wxXmlNode *nodeSystemInfo) | |
350 | { | |
9a83f860 | 351 | nodeSystemInfo->AddAttribute(wxT("description"), wxGetOsDescription()); |
ce4fd7b5 VZ |
352 | |
353 | return true; | |
354 | } | |
355 | ||
356 | bool wxDebugReport::DoAddLoadedModules(wxXmlNode *nodeModules) | |
357 | { | |
358 | wxDynamicLibraryDetailsArray modules(wxDynamicLibrary::ListLoaded()); | |
359 | const size_t count = modules.GetCount(); | |
360 | if ( !count ) | |
361 | return false; | |
362 | ||
363 | for ( size_t n = 0; n < count; n++ ) | |
364 | { | |
365 | const wxDynamicLibraryDetails& info = modules[n]; | |
366 | ||
9a83f860 | 367 | wxXmlNode *nodeModule = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("module")); |
ce4fd7b5 VZ |
368 | nodeModules->AddChild(nodeModule); |
369 | ||
370 | wxString path = info.GetPath(); | |
371 | if ( path.empty() ) | |
372 | path = info.GetName(); | |
373 | if ( !path.empty() ) | |
9a83f860 | 374 | nodeModule->AddAttribute(wxT("path"), path); |
ce4fd7b5 | 375 | |
d64dc197 WS |
376 | void *addr = NULL; |
377 | size_t len = 0; | |
ce4fd7b5 VZ |
378 | if ( info.GetAddress(&addr, &len) ) |
379 | { | |
9a83f860 VZ |
380 | HexProperty(nodeModule, wxT("address"), wxPtrToUInt(addr)); |
381 | HexProperty(nodeModule, wxT("size"), len); | |
ce4fd7b5 VZ |
382 | } |
383 | ||
384 | wxString ver = info.GetVersion(); | |
385 | if ( !ver.empty() ) | |
386 | { | |
9a83f860 | 387 | nodeModule->AddAttribute(wxT("version"), ver); |
ce4fd7b5 VZ |
388 | } |
389 | } | |
390 | ||
391 | return true; | |
392 | } | |
393 | ||
394 | bool wxDebugReport::DoAddExceptionInfo(wxXmlNode *nodeContext) | |
395 | { | |
396 | #if wxUSE_CRASHREPORT | |
397 | wxCrashContext c; | |
398 | if ( !c.code ) | |
399 | return false; | |
400 | ||
9a83f860 | 401 | wxXmlNode *nodeExc = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("exception")); |
ce4fd7b5 VZ |
402 | nodeContext->AddChild(nodeExc); |
403 | ||
9a83f860 VZ |
404 | HexProperty(nodeExc, wxT("code"), c.code); |
405 | nodeExc->AddAttribute(wxT("name"), c.GetExceptionString()); | |
406 | HexProperty(nodeExc, wxT("address"), wxPtrToUInt(c.addr)); | |
ce4fd7b5 VZ |
407 | |
408 | #ifdef __INTEL__ | |
9a83f860 | 409 | wxXmlNode *nodeRegs = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("registers")); |
ce4fd7b5 | 410 | nodeContext->AddChild(nodeRegs); |
9a83f860 VZ |
411 | HexElement(nodeRegs, wxT("eax"), c.regs.eax); |
412 | HexElement(nodeRegs, wxT("ebx"), c.regs.ebx); | |
413 | HexElement(nodeRegs, wxT("ecx"), c.regs.edx); | |
414 | HexElement(nodeRegs, wxT("edx"), c.regs.edx); | |
415 | HexElement(nodeRegs, wxT("esi"), c.regs.esi); | |
416 | HexElement(nodeRegs, wxT("edi"), c.regs.edi); | |
417 | ||
418 | HexElement(nodeRegs, wxT("ebp"), c.regs.ebp); | |
419 | HexElement(nodeRegs, wxT("esp"), c.regs.esp); | |
420 | HexElement(nodeRegs, wxT("eip"), c.regs.eip); | |
421 | ||
422 | HexElement(nodeRegs, wxT("cs"), c.regs.cs); | |
423 | HexElement(nodeRegs, wxT("ds"), c.regs.ds); | |
424 | HexElement(nodeRegs, wxT("es"), c.regs.es); | |
425 | HexElement(nodeRegs, wxT("fs"), c.regs.fs); | |
426 | HexElement(nodeRegs, wxT("gs"), c.regs.gs); | |
427 | HexElement(nodeRegs, wxT("ss"), c.regs.ss); | |
428 | ||
429 | HexElement(nodeRegs, wxT("flags"), c.regs.flags); | |
ce4fd7b5 VZ |
430 | #endif // __INTEL__ |
431 | ||
432 | return true; | |
433 | #else // !wxUSE_CRASHREPORT | |
434 | wxUnusedVar(nodeContext); | |
435 | ||
436 | return false; | |
437 | #endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT | |
438 | } | |
439 | ||
440 | bool wxDebugReport::AddContext(wxDebugReport::Context ctx) | |
441 | { | |
9a83f860 | 442 | wxCHECK_MSG( IsOk(), false, wxT("use IsOk() first") ); |
ce4fd7b5 VZ |
443 | |
444 | // create XML dump of current context | |
445 | wxXmlDocument xmldoc; | |
9a83f860 | 446 | wxXmlNode *nodeRoot = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("report")); |
ce4fd7b5 | 447 | xmldoc.SetRoot(nodeRoot); |
9a83f860 VZ |
448 | nodeRoot->AddAttribute(wxT("version"), wxT("1.0")); |
449 | nodeRoot->AddAttribute(wxT("kind"), ctx == Context_Current ? wxT("user") | |
450 | : wxT("exception")); | |
ce4fd7b5 VZ |
451 | |
452 | // add system information | |
9a83f860 | 453 | wxXmlNode *nodeSystemInfo = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("system")); |
ce4fd7b5 VZ |
454 | if ( DoAddSystemInfo(nodeSystemInfo) ) |
455 | nodeRoot->AddChild(nodeSystemInfo); | |
456 | else | |
457 | delete nodeSystemInfo; | |
458 | ||
459 | // add information about the loaded modules | |
9a83f860 | 460 | wxXmlNode *nodeModules = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("modules")); |
ce4fd7b5 VZ |
461 | if ( DoAddLoadedModules(nodeModules) ) |
462 | nodeRoot->AddChild(nodeModules); | |
463 | else | |
464 | delete nodeModules; | |
465 | ||
466 | // add CPU context information: this only makes sense for exceptions as our | |
467 | // current context is not very interesting otherwise | |
468 | if ( ctx == Context_Exception ) | |
469 | { | |
9a83f860 | 470 | wxXmlNode *nodeContext = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("context")); |
ce4fd7b5 VZ |
471 | if ( DoAddExceptionInfo(nodeContext) ) |
472 | nodeRoot->AddChild(nodeContext); | |
473 | else | |
474 | delete nodeContext; | |
475 | } | |
476 | ||
477 | // add stack traceback | |
478 | #if wxUSE_STACKWALKER | |
9a83f860 | 479 | wxXmlNode *nodeStack = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("stack")); |
ce4fd7b5 | 480 | XmlStackWalker sw(nodeStack); |
4db307e1 | 481 | #if wxUSE_ON_FATAL_EXCEPTION |
ce4fd7b5 VZ |
482 | if ( ctx == Context_Exception ) |
483 | { | |
484 | sw.WalkFromException(); | |
485 | } | |
478cde32 | 486 | else // Context_Current |
4db307e1 | 487 | #endif // wxUSE_ON_FATAL_EXCEPTION |
ce4fd7b5 VZ |
488 | { |
489 | sw.Walk(); | |
490 | } | |
491 | ||
492 | if ( sw.IsOk() ) | |
493 | nodeRoot->AddChild(nodeStack); | |
494 | else | |
495 | delete nodeStack; | |
496 | #endif // wxUSE_STACKWALKER | |
497 | ||
498 | // finally let the user add any extra information he needs | |
499 | DoAddCustomContext(nodeRoot); | |
500 | ||
501 | ||
502 | // save the entire context dump in a file | |
9a83f860 | 503 | wxFileName fn(m_dir, GetReportName(), wxT("xml")); |
ce4fd7b5 VZ |
504 | |
505 | if ( !xmldoc.Save(fn.GetFullPath()) ) | |
506 | return false; | |
507 | ||
508 | AddFile(fn.GetFullName(), _("process context description")); | |
509 | ||
510 | return true; | |
511 | } | |
512 | ||
513 | #endif // wxUSE_STACKWALKER | |
514 | ||
515 | // ---------------------------------------------------------------------------- | |
516 | // adding core dump | |
517 | // ---------------------------------------------------------------------------- | |
518 | ||
519 | #if wxUSE_CRASHREPORT | |
520 | ||
521 | bool wxDebugReport::AddDump(Context ctx) | |
522 | { | |
9a83f860 | 523 | wxCHECK_MSG( IsOk(), false, wxT("use IsOk() first") ); |
ce4fd7b5 | 524 | |
9a83f860 | 525 | wxFileName fn(m_dir, GetReportName(), wxT("dmp")); |
ce4fd7b5 VZ |
526 | wxCrashReport::SetFileName(fn.GetFullPath()); |
527 | ||
528 | if ( !(ctx == Context_Exception ? wxCrashReport::Generate() | |
529 | : wxCrashReport::GenerateNow()) ) | |
530 | return false; | |
531 | ||
532 | AddFile(fn.GetFullName(), _("dump of the process state (binary)")); | |
533 | ||
534 | return true; | |
535 | } | |
536 | ||
537 | #endif // wxUSE_CRASHREPORT | |
538 | ||
539 | // ---------------------------------------------------------------------------- | |
540 | // report processing | |
541 | // ---------------------------------------------------------------------------- | |
542 | ||
543 | bool wxDebugReport::Process() | |
544 | { | |
545 | if ( !GetFilesCount() ) | |
546 | { | |
547 | wxLogError(_("Debug report generation has failed.")); | |
548 | ||
549 | return false; | |
550 | } | |
551 | ||
552 | if ( !DoProcess() ) | |
553 | { | |
554 | wxLogError(_("Processing debug report has failed, leaving the files in \"%s\" directory."), | |
555 | GetDirectory().c_str()); | |
556 | ||
557 | Reset(); | |
558 | ||
559 | return false; | |
560 | } | |
561 | ||
562 | return true; | |
563 | } | |
564 | ||
565 | bool wxDebugReport::DoProcess() | |
566 | { | |
96be8b4d | 567 | wxString msg(_("A debug report has been generated. It can be found in")); |
9a83f860 VZ |
568 | msg << wxT("\n") |
569 | wxT("\t") << GetDirectory() << wxT("\n\n") | |
96be8b4d | 570 | << _("And includes the following files:\n"); |
ce4fd7b5 VZ |
571 | |
572 | wxString name, desc; | |
573 | const size_t count = GetFilesCount(); | |
574 | for ( size_t n = 0; n < count; n++ ) | |
575 | { | |
576 | GetFile(n, &name, &desc); | |
78a11854 | 577 | msg += wxString::Format("\t%s: %s\n", name, desc); |
ce4fd7b5 VZ |
578 | } |
579 | ||
580 | msg += _("\nPlease send this report to the program maintainer, thank you!\n"); | |
581 | ||
9a83f860 | 582 | wxLogMessage(wxT("%s"), msg.c_str()); |
ce4fd7b5 VZ |
583 | |
584 | // we have to do this or the report would be deleted, and we don't even | |
585 | // have any way to ask the user if he wants to keep it from here | |
586 | Reset(); | |
587 | ||
588 | return true; | |
589 | } | |
590 | ||
591 | // ============================================================================ | |
592 | // wxDebugReport-derived classes | |
593 | // ============================================================================ | |
594 | ||
595 | #if wxUSE_ZIPSTREAM | |
596 | ||
597 | // ---------------------------------------------------------------------------- | |
598 | // wxDebugReportCompress | |
599 | // ---------------------------------------------------------------------------- | |
600 | ||
fdf20a26 VZ |
601 | void wxDebugReportCompress::SetCompressedFileDirectory(const wxString& dir) |
602 | { | |
603 | wxASSERT_MSG( m_zipfile.empty(), "Too late: call this before Process()" ); | |
604 | ||
605 | m_zipDir = dir; | |
606 | } | |
607 | ||
608 | void wxDebugReportCompress::SetCompressedFileBaseName(const wxString& name) | |
609 | { | |
610 | wxASSERT_MSG( m_zipfile.empty(), "Too late: call this before Process()" ); | |
611 | ||
612 | m_zipName = name; | |
613 | } | |
614 | ||
ce4fd7b5 VZ |
615 | bool wxDebugReportCompress::DoProcess() |
616 | { | |
617 | const size_t count = GetFilesCount(); | |
618 | if ( !count ) | |
619 | return false; | |
620 | ||
b78df856 VZ |
621 | // create the compressed report file outside of the directory with the |
622 | // report files as it will be deleted by wxDebugReport dtor but we want to | |
623 | // keep this one: for this we simply treat the directory name as the name | |
624 | // of the file so that its last component becomes our base name | |
625 | wxFileName fn(GetDirectory()); | |
fdf20a26 VZ |
626 | if ( !m_zipDir.empty() ) |
627 | fn.SetPath(m_zipDir); | |
628 | if ( !m_zipName.empty() ) | |
629 | fn.SetName(m_zipName); | |
b78df856 VZ |
630 | fn.SetExt("zip"); |
631 | ||
ce4fd7b5 | 632 | // create the streams |
9a83f860 | 633 | wxFFileOutputStream os(fn.GetFullPath(), wxT("wb")); |
3b676ed7 | 634 | wxZipOutputStream zos(os, 9); |
ce4fd7b5 VZ |
635 | |
636 | // add all files to the ZIP one | |
637 | wxString name, desc; | |
638 | for ( size_t n = 0; n < count; n++ ) | |
639 | { | |
640 | GetFile(n, &name, &desc); | |
641 | ||
642 | wxZipEntry *ze = new wxZipEntry(name); | |
643 | ze->SetComment(desc); | |
644 | ||
3b676ed7 | 645 | if ( !zos.PutNextEntry(ze) ) |
ce4fd7b5 VZ |
646 | return false; |
647 | ||
b78df856 | 648 | const wxFileName filename(GetDirectory(), name); |
2cdd63c6 | 649 | wxFFileInputStream is(filename.GetFullPath()); |
3b676ed7 | 650 | if ( !is.IsOk() || !zos.Write(is).IsOk() ) |
ce4fd7b5 VZ |
651 | return false; |
652 | } | |
653 | ||
3b676ed7 | 654 | if ( !zos.Close() ) |
ce4fd7b5 VZ |
655 | return false; |
656 | ||
657 | m_zipfile = fn.GetFullPath(); | |
658 | ||
659 | return true; | |
660 | } | |
661 | ||
662 | // ---------------------------------------------------------------------------- | |
663 | // wxDebugReportUpload | |
664 | // ---------------------------------------------------------------------------- | |
665 | ||
666 | wxDebugReportUpload::wxDebugReportUpload(const wxString& url, | |
667 | const wxString& input, | |
668 | const wxString& action, | |
669 | const wxString& curl) | |
670 | : m_uploadURL(url), | |
671 | m_inputField(input), | |
672 | m_curlCmd(curl) | |
673 | { | |
9a83f860 VZ |
674 | if ( m_uploadURL.Last() != wxT('/') ) |
675 | m_uploadURL += wxT('/'); | |
ce4fd7b5 VZ |
676 | m_uploadURL += action; |
677 | } | |
678 | ||
679 | bool wxDebugReportUpload::DoProcess() | |
680 | { | |
681 | if ( !wxDebugReportCompress::DoProcess() ) | |
682 | return false; | |
683 | ||
684 | ||
685 | wxArrayString output, errors; | |
686 | int rc = wxExecute(wxString::Format | |
687 | ( | |
4f4c48a8 | 688 | wxT("%s -F \"%s=@%s\" %s"), |
ce4fd7b5 VZ |
689 | m_curlCmd.c_str(), |
690 | m_inputField.c_str(), | |
691 | GetCompressedFileName().c_str(), | |
692 | m_uploadURL.c_str() | |
693 | ), | |
694 | output, | |
695 | errors); | |
696 | if ( rc == -1 ) | |
697 | { | |
698 | wxLogError(_("Failed to execute curl, please install it in PATH.")); | |
699 | } | |
700 | else if ( rc != 0 ) | |
701 | { | |
702 | const size_t count = errors.GetCount(); | |
703 | if ( count ) | |
704 | { | |
705 | for ( size_t n = 0; n < count; n++ ) | |
706 | { | |
9a83f860 | 707 | wxLogWarning(wxT("%s"), errors[n].c_str()); |
ce4fd7b5 VZ |
708 | } |
709 | } | |
710 | ||
711 | wxLogError(_("Failed to upload the debug report (error code %d)."), rc); | |
712 | } | |
713 | else // rc == 0 | |
714 | { | |
715 | if ( OnServerReply(output) ) | |
716 | return true; | |
717 | } | |
718 | ||
719 | return false; | |
720 | } | |
721 | ||
722 | #endif // wxUSE_ZIPSTREAM | |
723 | ||
724 | #endif // wxUSE_DEBUGREPORT |