1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/debugrpt.cpp
3 // Purpose: wxDebugReport and related classes implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2005 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
34 #include "wx/debugrpt.h"
36 #include "wx/filename.h"
38 #include "wx/dynlib.h"
40 #include "wx/xml/xml.h"
43 #include "wx/stackwalk.h"
47 #include "wx/msw/crashrpt.h"
51 #include "wx/wfstream.h"
52 #include "wx/zipstrm.h"
53 #endif // wxUSE_ZIPSTREAM
57 // ----------------------------------------------------------------------------
58 // XmlStackWalker: stack walker specialization which dumps stack in XML
59 // ----------------------------------------------------------------------------
61 class XmlStackWalker
: public wxStackWalker
64 XmlStackWalker(wxXmlNode
*nodeStack
)
67 m_nodeStack
= nodeStack
;
70 bool IsOk() const { return m_isOk
; }
73 virtual void OnStackFrame(const wxStackFrame
& frame
);
75 wxXmlNode
*m_nodeStack
;
79 #endif // wxUSE_STACKWALKER
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
86 HexProperty(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
88 node
->AddProperty(name
, wxString::Format(_T("%08lx"), value
));
92 NumProperty(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
94 node
->AddProperty(name
, wxString::Format(_T("%lu"), value
));
98 TextElement(wxXmlNode
*node
, const wxChar
*name
, const wxString
& value
)
100 wxXmlNode
*nodeChild
= new wxXmlNode(wxXML_ELEMENT_NODE
, name
);
101 node
->AddChild(nodeChild
);
102 nodeChild
->AddChild(new wxXmlNode(wxXML_TEXT_NODE
, _T(""), value
));
106 HexElement(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
108 TextElement(node
, name
, wxString::Format(_T("%08lx"), value
));
111 #if wxUSE_STACKWALKER
113 // ============================================================================
114 // XmlStackWalker implementation
115 // ============================================================================
117 void XmlStackWalker::OnStackFrame(const wxStackFrame
& frame
)
121 wxXmlNode
*nodeFrame
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("frame"));
122 m_nodeStack
->AddChild(nodeFrame
);
124 NumProperty(nodeFrame
, _T("level"), frame
.GetLevel());
125 wxString func
= frame
.GetName();
128 nodeFrame
->AddProperty(_T("function"), func
);
129 HexProperty(nodeFrame
, _T("offset"), frame
.GetOffset());
132 if ( frame
.HasSourceLocation() )
134 nodeFrame
->AddProperty(_T("file"), frame
.GetFileName());
135 NumProperty(nodeFrame
, _T("line"), frame
.GetLine());
138 const size_t nParams
= frame
.GetParamCount();
141 wxXmlNode
*nodeParams
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("parameters"));
142 nodeFrame
->AddChild(nodeParams
);
144 for ( size_t n
= 0; n
< nParams
; n
++ )
147 nodeParam
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("parameter"));
148 nodeParams
->AddChild(nodeParam
);
150 NumProperty(nodeParam
, _T("number"), n
);
152 wxString type
, name
, value
;
153 if ( !frame
.GetParam(n
, &type
, &name
, &value
) )
157 TextElement(nodeParam
, _T("type"), type
);
160 TextElement(nodeParam
, _T("name"), name
);
162 if ( !value
.empty() )
163 TextElement(nodeParam
, _T("value"), value
);
168 #endif // wxUSE_STACKWALKER
170 // ============================================================================
171 // wxDebugReport implementation
172 // ============================================================================
174 // ----------------------------------------------------------------------------
175 // initialization and cleanup
176 // ----------------------------------------------------------------------------
178 wxDebugReport::wxDebugReport()
180 // get a temporary directory name
181 wxString
appname(wxTheApp
? wxTheApp
->GetAppName() : _T("wx"));
183 // we can't use CreateTempFileName() because it creates a file, not a
184 // directory, so do our best to create a unique name ourselves
186 // of course, this doesn't protect us against malicious users...
188 fn
.AssignTempFileName(appname
);
189 m_dir
.Printf(_T("%s%c%s_dbgrpt-%lu-%s"),
190 fn
.GetPath().c_str(), wxFILE_SEP_PATH
, appname
.c_str(),
192 wxDateTime::Now().Format(_T("%Y%m%dT%H%M%S")).c_str());
194 // as we are going to save the process state there use restrictive
196 if ( !wxMkdir(m_dir
, 0700) )
198 wxLogSysError(_("Failed to create directory \"%s\""), m_dir
.c_str());
199 wxLogError(_("Debug report couldn't be created."));
205 wxDebugReport::~wxDebugReport()
207 if ( !m_dir
.empty() )
209 // remove all files in this directory
212 for ( bool cont
= dir
.GetFirst(&file
); cont
; cont
= dir
.GetNext(&file
) )
214 if ( wxRemove(wxFileName(m_dir
, file
).GetFullPath()) != 0 )
216 wxLogSysError(_("Failed to remove debug report file \"%s\""),
224 if ( !m_dir
.empty() )
226 if ( wxRmDir(m_dir
) != 0 )
228 wxLogSysError(_("Failed to clean up debug report directory \"%s\""),
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
238 wxString
wxDebugReport::GetReportName() const
240 return wxString(wxTheApp
? wxTheApp
->GetAppName() : _T("wx"));
243 void wxDebugReport::AddFile(const wxString
& name
, const wxString
& description
)
246 m_descriptions
.Add(description
);
249 void wxDebugReport::RemoveFile(const wxString
& name
)
251 const int n
= m_files
.Index(name
);
252 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("No such file in wxDebugReport") );
255 m_descriptions
.RemoveAt(n
);
257 wxRemove(wxFileName(GetDirectory(), name
).GetFullPath());
260 bool wxDebugReport::GetFile(size_t n
, wxString
*name
, wxString
*desc
) const
262 if ( n
>= m_files
.GetCount() )
268 *desc
= m_descriptions
[n
];
273 void wxDebugReport::AddAll(Context context
)
275 #if wxUSE_STACKWALKER
277 #endif // wxUSE_STACKWALKER
279 #if wxUSE_CRASHREPORT
281 #endif // wxUSE_CRASHREPORT
284 // ----------------------------------------------------------------------------
285 // adding basic text information about current context
286 // ----------------------------------------------------------------------------
288 #if wxUSE_STACKWALKER
290 bool wxDebugReport::DoAddSystemInfo(wxXmlNode
*nodeSystemInfo
)
292 nodeSystemInfo
->AddProperty(_T("description"), wxGetOsDescription());
297 bool wxDebugReport::DoAddLoadedModules(wxXmlNode
*nodeModules
)
299 wxDynamicLibraryDetailsArray
modules(wxDynamicLibrary::ListLoaded());
300 const size_t count
= modules
.GetCount();
304 for ( size_t n
= 0; n
< count
; n
++ )
306 const wxDynamicLibraryDetails
& info
= modules
[n
];
308 wxXmlNode
*nodeModule
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("module"));
309 nodeModules
->AddChild(nodeModule
);
311 wxString path
= info
.GetPath();
313 path
= info
.GetName();
315 nodeModule
->AddProperty(_T("path"), path
);
319 if ( info
.GetAddress(&addr
, &len
) )
321 HexProperty(nodeModule
, _T("address"), (unsigned long)addr
);
322 HexProperty(nodeModule
, _T("size"), len
);
325 wxString ver
= info
.GetVersion();
328 nodeModule
->AddProperty(_T("version"), ver
);
335 bool wxDebugReport::DoAddExceptionInfo(wxXmlNode
*nodeContext
)
337 #if wxUSE_CRASHREPORT
342 wxXmlNode
*nodeExc
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("exception"));
343 nodeContext
->AddChild(nodeExc
);
345 HexProperty(nodeExc
, _T("code"), c
.code
);
346 nodeExc
->AddProperty(_T("name"), c
.GetExceptionString());
347 HexProperty(nodeExc
, _T("address"), (unsigned long)c
.addr
);
350 wxXmlNode
*nodeRegs
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("registers"));
351 nodeContext
->AddChild(nodeRegs
);
352 HexElement(nodeRegs
, _T("eax"), c
.regs
.eax
);
353 HexElement(nodeRegs
, _T("ebx"), c
.regs
.ebx
);
354 HexElement(nodeRegs
, _T("ecx"), c
.regs
.edx
);
355 HexElement(nodeRegs
, _T("edx"), c
.regs
.edx
);
356 HexElement(nodeRegs
, _T("esi"), c
.regs
.esi
);
357 HexElement(nodeRegs
, _T("edi"), c
.regs
.edi
);
359 HexElement(nodeRegs
, _T("ebp"), c
.regs
.ebp
);
360 HexElement(nodeRegs
, _T("esp"), c
.regs
.esp
);
361 HexElement(nodeRegs
, _T("eip"), c
.regs
.eip
);
363 HexElement(nodeRegs
, _T("cs"), c
.regs
.cs
);
364 HexElement(nodeRegs
, _T("ds"), c
.regs
.ds
);
365 HexElement(nodeRegs
, _T("es"), c
.regs
.es
);
366 HexElement(nodeRegs
, _T("fs"), c
.regs
.fs
);
367 HexElement(nodeRegs
, _T("gs"), c
.regs
.gs
);
368 HexElement(nodeRegs
, _T("ss"), c
.regs
.ss
);
370 HexElement(nodeRegs
, _T("flags"), c
.regs
.flags
);
374 #else // !wxUSE_CRASHREPORT
375 wxUnusedVar(nodeContext
);
378 #endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT
381 bool wxDebugReport::AddContext(wxDebugReport::Context ctx
)
383 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
385 // create XML dump of current context
386 wxXmlDocument xmldoc
;
387 wxXmlNode
*nodeRoot
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("report"));
388 xmldoc
.SetRoot(nodeRoot
);
389 nodeRoot
->AddProperty(_T("version"), _T("1.0"));
390 nodeRoot
->AddProperty(_T("kind"), ctx
== Context_Curent
? _T("user")
393 // add system information
394 wxXmlNode
*nodeSystemInfo
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("system"));
395 if ( DoAddSystemInfo(nodeSystemInfo
) )
396 nodeRoot
->AddChild(nodeSystemInfo
);
398 delete nodeSystemInfo
;
400 // add information about the loaded modules
401 wxXmlNode
*nodeModules
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("modules"));
402 if ( DoAddLoadedModules(nodeModules
) )
403 nodeRoot
->AddChild(nodeModules
);
407 // add CPU context information: this only makes sense for exceptions as our
408 // current context is not very interesting otherwise
409 if ( ctx
== Context_Exception
)
411 wxXmlNode
*nodeContext
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("context"));
412 if ( DoAddExceptionInfo(nodeContext
) )
413 nodeRoot
->AddChild(nodeContext
);
418 // add stack traceback
419 #if wxUSE_STACKWALKER
420 wxXmlNode
*nodeStack
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("stack"));
421 XmlStackWalker
sw(nodeStack
);
422 if ( ctx
== Context_Exception
)
424 sw
.WalkFromException();
426 else // Context_Curent
432 nodeRoot
->AddChild(nodeStack
);
435 #endif // wxUSE_STACKWALKER
437 // finally let the user add any extra information he needs
438 DoAddCustomContext(nodeRoot
);
441 // save the entire context dump in a file
442 wxFileName
fn(m_dir
, GetReportName(), _T("xml"));
444 if ( !xmldoc
.Save(fn
.GetFullPath()) )
447 AddFile(fn
.GetFullName(), _("process context description"));
452 #endif // wxUSE_STACKWALKER
454 // ----------------------------------------------------------------------------
456 // ----------------------------------------------------------------------------
458 #if wxUSE_CRASHREPORT
460 bool wxDebugReport::AddDump(Context ctx
)
462 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
464 wxFileName
fn(m_dir
, GetReportName(), _T("dmp"));
465 wxCrashReport::SetFileName(fn
.GetFullPath());
467 if ( !(ctx
== Context_Exception
? wxCrashReport::Generate()
468 : wxCrashReport::GenerateNow()) )
471 AddFile(fn
.GetFullName(), _("dump of the process state (binary)"));
476 #endif // wxUSE_CRASHREPORT
478 // ----------------------------------------------------------------------------
480 // ----------------------------------------------------------------------------
482 bool wxDebugReport::Process()
484 if ( !GetFilesCount() )
486 wxLogError(_("Debug report generation has failed."));
493 wxLogError(_("Processing debug report has failed, leaving the files in \"%s\" directory."),
494 GetDirectory().c_str());
504 bool wxDebugReport::DoProcess()
506 wxString msg
= _("*** A debug report has been generated\n");
507 msg
+= wxString::Format(_("*** It can be found in \"%s\"\n"),
508 GetDirectory().c_str());
509 msg
+= _("*** And includes the following files:\n");
512 const size_t count
= GetFilesCount();
513 for ( size_t n
= 0; n
< count
; n
++ )
515 GetFile(n
, &name
, &desc
);
516 msg
+= wxString::Format(_("\t%s: %s\n"), name
.c_str(), desc
.c_str());
519 msg
+= _("\nPlease send this report to the program maintainer, thank you!\n");
521 wxLogMessage(_T("%s"), msg
.c_str());
523 // we have to do this or the report would be deleted, and we don't even
524 // have any way to ask the user if he wants to keep it from here
530 // ============================================================================
531 // wxDebugReport-derived classes
532 // ============================================================================
536 // ----------------------------------------------------------------------------
537 // wxDebugReportCompress
538 // ----------------------------------------------------------------------------
540 bool wxDebugReportCompress::DoProcess()
542 const size_t count
= GetFilesCount();
546 // create the streams
547 wxFileName
fn(GetDirectory(), GetReportName(), _T("zip"));
548 wxFFileOutputStream
os(fn
.GetFullPath(), _T("wb"));
549 wxZipOutputStream
zos(os
, 9);
551 // add all files to the ZIP one
553 for ( size_t n
= 0; n
< count
; n
++ )
555 GetFile(n
, &name
, &desc
);
557 wxZipEntry
*ze
= new wxZipEntry(name
);
558 ze
->SetComment(desc
);
560 if ( !zos
.PutNextEntry(ze
) )
563 wxFFileInputStream
is(wxFileName(fn
.GetPath(), name
).GetFullPath());
564 if ( !is
.IsOk() || !zos
.Write(is
).IsOk() )
571 m_zipfile
= fn
.GetFullPath();
576 // ----------------------------------------------------------------------------
577 // wxDebugReportUpload
578 // ----------------------------------------------------------------------------
580 wxDebugReportUpload::wxDebugReportUpload(const wxString
& url
,
581 const wxString
& input
,
582 const wxString
& action
,
583 const wxString
& curl
)
588 if ( m_uploadURL
.Last() != _T('/') )
589 m_uploadURL
+= _T('/');
590 m_uploadURL
+= action
;
593 bool wxDebugReportUpload::DoProcess()
595 if ( !wxDebugReportCompress::DoProcess() )
599 wxArrayString output
, errors
;
600 int rc
= wxExecute(wxString::Format
602 _T("%s -F %s=@%s %s"),
604 m_inputField
.c_str(),
605 GetCompressedFileName().c_str(),
612 wxLogError(_("Failed to execute curl, please install it in PATH."));
616 const size_t count
= errors
.GetCount();
619 for ( size_t n
= 0; n
< count
; n
++ )
621 wxLogWarning(_T("%s"), errors
[n
].c_str());
625 wxLogError(_("Failed to upload the debug report (error code %d)."), rc
);
629 if ( OnServerReply(output
) )
636 #endif // wxUSE_ZIPSTREAM
638 #endif // wxUSE_DEBUGREPORT