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"
35 #include "wx/debugrpt.h"
37 #include "wx/filename.h"
39 #include "wx/dynlib.h"
41 #include "wx/xml/xml.h"
44 #include "wx/stackwalk.h"
48 #include "wx/msw/crashrpt.h"
52 #include "wx/wfstream.h"
53 #include "wx/zipstrm.h"
54 #endif // wxUSE_ZIPSTREAM
56 WX_CHECK_BUILD_OPTIONS("wxQA")
58 // ----------------------------------------------------------------------------
59 // XmlStackWalker: stack walker specialization which dumps stack in XML
60 // ----------------------------------------------------------------------------
64 class XmlStackWalker
: public wxStackWalker
67 XmlStackWalker(wxXmlNode
*nodeStack
)
70 m_nodeStack
= nodeStack
;
73 bool IsOk() const { return m_isOk
; }
76 virtual void OnStackFrame(const wxStackFrame
& frame
);
78 wxXmlNode
*m_nodeStack
;
82 #endif // wxUSE_STACKWALKER
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
89 HexProperty(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
91 node
->AddProperty(name
, wxString::Format(_T("%08lx"), value
));
95 NumProperty(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
97 node
->AddProperty(name
, wxString::Format(_T("%lu"), value
));
101 TextElement(wxXmlNode
*node
, const wxChar
*name
, const wxString
& value
)
103 wxXmlNode
*nodeChild
= new wxXmlNode(wxXML_ELEMENT_NODE
, name
);
104 node
->AddChild(nodeChild
);
105 nodeChild
->AddChild(new wxXmlNode(wxXML_TEXT_NODE
, _T(""), value
));
109 HexElement(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
111 TextElement(node
, name
, wxString::Format(_T("%08lx"), value
));
114 #if wxUSE_STACKWALKER
116 // ============================================================================
117 // XmlStackWalker implementation
118 // ============================================================================
120 void XmlStackWalker::OnStackFrame(const wxStackFrame
& frame
)
124 wxXmlNode
*nodeFrame
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("frame"));
125 m_nodeStack
->AddChild(nodeFrame
);
127 NumProperty(nodeFrame
, _T("level"), frame
.GetLevel());
128 wxString func
= frame
.GetName();
131 nodeFrame
->AddProperty(_T("function"), func
);
132 HexProperty(nodeFrame
, _T("offset"), frame
.GetOffset());
135 if ( frame
.HasSourceLocation() )
137 nodeFrame
->AddProperty(_T("file"), frame
.GetFileName());
138 NumProperty(nodeFrame
, _T("line"), frame
.GetLine());
141 const size_t nParams
= frame
.GetParamCount();
144 wxXmlNode
*nodeParams
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("parameters"));
145 nodeFrame
->AddChild(nodeParams
);
147 for ( size_t n
= 0; n
< nParams
; n
++ )
150 nodeParam
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("parameter"));
151 nodeParams
->AddChild(nodeParam
);
153 NumProperty(nodeParam
, _T("number"), n
);
155 wxString type
, name
, value
;
156 if ( !frame
.GetParam(n
, &type
, &name
, &value
) )
160 TextElement(nodeParam
, _T("type"), type
);
163 TextElement(nodeParam
, _T("name"), name
);
165 if ( !value
.empty() )
166 TextElement(nodeParam
, _T("value"), value
);
171 #endif // wxUSE_STACKWALKER
173 // ============================================================================
174 // wxDebugReport implementation
175 // ============================================================================
177 // ----------------------------------------------------------------------------
178 // initialization and cleanup
179 // ----------------------------------------------------------------------------
181 wxDebugReport::wxDebugReport()
183 // get a temporary directory name
184 wxString appname
= GetReportName();
186 // we can't use CreateTempFileName() because it creates a file, not a
187 // directory, so do our best to create a unique name ourselves
189 // of course, this doesn't protect us against malicious users...
191 fn
.AssignTempFileName(appname
);
192 m_dir
.Printf(_T("%s%c%s_dbgrpt-%lu-%s"),
193 fn
.GetPath().c_str(), wxFILE_SEP_PATH
, appname
.c_str(),
195 wxDateTime::Now().Format(_T("%Y%m%dT%H%M%S")).c_str());
197 // as we are going to save the process state there use restrictive
199 if ( !wxMkdir(m_dir
, 0700) )
201 wxLogSysError(_("Failed to create directory \"%s\""), m_dir
.c_str());
202 wxLogError(_("Debug report couldn't be created."));
208 wxDebugReport::~wxDebugReport()
210 if ( !m_dir
.empty() )
212 // remove all files in this directory
215 for ( bool cont
= dir
.GetFirst(&file
); cont
; cont
= dir
.GetNext(&file
) )
217 if ( wxRemove(wxFileName(m_dir
, file
).GetFullPath()) != 0 )
219 wxLogSysError(_("Failed to remove debug report file \"%s\""),
227 if ( !m_dir
.empty() )
229 // Temp fix: what should this be? eVC++ doesn't like wxRmDir
231 if ( wxRmdir(m_dir
.fn_str()) != 0 )
233 if ( wxRmDir(m_dir
.fn_str()) != 0 )
236 wxLogSysError(_("Failed to clean up debug report directory \"%s\""),
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 wxString
wxDebugReport::GetReportName() const
249 return wxTheApp
->GetAppName();
254 void wxDebugReport::AddFile(const wxString
& name
, const wxString
& description
)
257 m_descriptions
.Add(description
);
260 void wxDebugReport::RemoveFile(const wxString
& name
)
262 const int n
= m_files
.Index(name
);
263 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("No such file in wxDebugReport") );
266 m_descriptions
.RemoveAt(n
);
268 wxRemove(wxFileName(GetDirectory(), name
).GetFullPath());
271 bool wxDebugReport::GetFile(size_t n
, wxString
*name
, wxString
*desc
) const
273 if ( n
>= m_files
.GetCount() )
279 *desc
= m_descriptions
[n
];
284 void wxDebugReport::AddAll(Context context
)
286 #if wxUSE_STACKWALKER
288 #endif // wxUSE_STACKWALKER
290 #if wxUSE_CRASHREPORT
292 #endif // wxUSE_CRASHREPORT
294 #if !wxUSE_STACKWALKER && !wxUSE_CRASHREPORT
295 wxUnusedVar(context
);
299 // ----------------------------------------------------------------------------
300 // adding basic text information about current context
301 // ----------------------------------------------------------------------------
303 #if wxUSE_STACKWALKER
305 bool wxDebugReport::DoAddSystemInfo(wxXmlNode
*nodeSystemInfo
)
307 nodeSystemInfo
->AddProperty(_T("description"), wxGetOsDescription());
312 bool wxDebugReport::DoAddLoadedModules(wxXmlNode
*nodeModules
)
314 wxDynamicLibraryDetailsArray
modules(wxDynamicLibrary::ListLoaded());
315 const size_t count
= modules
.GetCount();
319 for ( size_t n
= 0; n
< count
; n
++ )
321 const wxDynamicLibraryDetails
& info
= modules
[n
];
323 wxXmlNode
*nodeModule
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("module"));
324 nodeModules
->AddChild(nodeModule
);
326 wxString path
= info
.GetPath();
328 path
= info
.GetName();
330 nodeModule
->AddProperty(_T("path"), path
);
334 if ( info
.GetAddress(&addr
, &len
) )
336 HexProperty(nodeModule
, _T("address"), (unsigned long)addr
);
337 HexProperty(nodeModule
, _T("size"), len
);
340 wxString ver
= info
.GetVersion();
343 nodeModule
->AddProperty(_T("version"), ver
);
350 bool wxDebugReport::DoAddExceptionInfo(wxXmlNode
*nodeContext
)
352 #if wxUSE_CRASHREPORT
357 wxXmlNode
*nodeExc
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("exception"));
358 nodeContext
->AddChild(nodeExc
);
360 HexProperty(nodeExc
, _T("code"), c
.code
);
361 nodeExc
->AddProperty(_T("name"), c
.GetExceptionString());
362 HexProperty(nodeExc
, _T("address"), (unsigned long)c
.addr
);
365 wxXmlNode
*nodeRegs
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("registers"));
366 nodeContext
->AddChild(nodeRegs
);
367 HexElement(nodeRegs
, _T("eax"), c
.regs
.eax
);
368 HexElement(nodeRegs
, _T("ebx"), c
.regs
.ebx
);
369 HexElement(nodeRegs
, _T("ecx"), c
.regs
.edx
);
370 HexElement(nodeRegs
, _T("edx"), c
.regs
.edx
);
371 HexElement(nodeRegs
, _T("esi"), c
.regs
.esi
);
372 HexElement(nodeRegs
, _T("edi"), c
.regs
.edi
);
374 HexElement(nodeRegs
, _T("ebp"), c
.regs
.ebp
);
375 HexElement(nodeRegs
, _T("esp"), c
.regs
.esp
);
376 HexElement(nodeRegs
, _T("eip"), c
.regs
.eip
);
378 HexElement(nodeRegs
, _T("cs"), c
.regs
.cs
);
379 HexElement(nodeRegs
, _T("ds"), c
.regs
.ds
);
380 HexElement(nodeRegs
, _T("es"), c
.regs
.es
);
381 HexElement(nodeRegs
, _T("fs"), c
.regs
.fs
);
382 HexElement(nodeRegs
, _T("gs"), c
.regs
.gs
);
383 HexElement(nodeRegs
, _T("ss"), c
.regs
.ss
);
385 HexElement(nodeRegs
, _T("flags"), c
.regs
.flags
);
389 #else // !wxUSE_CRASHREPORT
390 wxUnusedVar(nodeContext
);
393 #endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT
396 bool wxDebugReport::AddContext(wxDebugReport::Context ctx
)
398 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
400 // create XML dump of current context
401 wxXmlDocument xmldoc
;
402 wxXmlNode
*nodeRoot
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("report"));
403 xmldoc
.SetRoot(nodeRoot
);
404 nodeRoot
->AddProperty(_T("version"), _T("1.0"));
405 nodeRoot
->AddProperty(_T("kind"), ctx
== Context_Current
? _T("user")
408 // add system information
409 wxXmlNode
*nodeSystemInfo
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("system"));
410 if ( DoAddSystemInfo(nodeSystemInfo
) )
411 nodeRoot
->AddChild(nodeSystemInfo
);
413 delete nodeSystemInfo
;
415 // add information about the loaded modules
416 wxXmlNode
*nodeModules
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("modules"));
417 if ( DoAddLoadedModules(nodeModules
) )
418 nodeRoot
->AddChild(nodeModules
);
422 // add CPU context information: this only makes sense for exceptions as our
423 // current context is not very interesting otherwise
424 if ( ctx
== Context_Exception
)
426 wxXmlNode
*nodeContext
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("context"));
427 if ( DoAddExceptionInfo(nodeContext
) )
428 nodeRoot
->AddChild(nodeContext
);
433 // add stack traceback
434 #if wxUSE_STACKWALKER
435 wxXmlNode
*nodeStack
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("stack"));
436 XmlStackWalker
sw(nodeStack
);
437 if ( ctx
== Context_Exception
)
439 sw
.WalkFromException();
441 else // Context_Current
447 nodeRoot
->AddChild(nodeStack
);
450 #endif // wxUSE_STACKWALKER
452 // finally let the user add any extra information he needs
453 DoAddCustomContext(nodeRoot
);
456 // save the entire context dump in a file
457 wxFileName
fn(m_dir
, GetReportName(), _T("xml"));
459 if ( !xmldoc
.Save(fn
.GetFullPath()) )
462 AddFile(fn
.GetFullName(), _("process context description"));
467 #endif // wxUSE_STACKWALKER
469 // ----------------------------------------------------------------------------
471 // ----------------------------------------------------------------------------
473 #if wxUSE_CRASHREPORT
475 bool wxDebugReport::AddDump(Context ctx
)
477 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
479 wxFileName
fn(m_dir
, GetReportName(), _T("dmp"));
480 wxCrashReport::SetFileName(fn
.GetFullPath());
482 if ( !(ctx
== Context_Exception
? wxCrashReport::Generate()
483 : wxCrashReport::GenerateNow()) )
486 AddFile(fn
.GetFullName(), _("dump of the process state (binary)"));
491 #endif // wxUSE_CRASHREPORT
493 // ----------------------------------------------------------------------------
495 // ----------------------------------------------------------------------------
497 bool wxDebugReport::Process()
499 if ( !GetFilesCount() )
501 wxLogError(_("Debug report generation has failed."));
508 wxLogError(_("Processing debug report has failed, leaving the files in \"%s\" directory."),
509 GetDirectory().c_str());
519 bool wxDebugReport::DoProcess()
521 wxString msg
= _("*** A debug report has been generated\n");
522 msg
+= wxString::Format(_("*** It can be found in \"%s\"\n"),
523 GetDirectory().c_str());
524 msg
+= _("*** And includes the following files:\n");
527 const size_t count
= GetFilesCount();
528 for ( size_t n
= 0; n
< count
; n
++ )
530 GetFile(n
, &name
, &desc
);
531 msg
+= wxString::Format(_("\t%s: %s\n"), name
.c_str(), desc
.c_str());
534 msg
+= _("\nPlease send this report to the program maintainer, thank you!\n");
536 wxLogMessage(_T("%s"), msg
.c_str());
538 // we have to do this or the report would be deleted, and we don't even
539 // have any way to ask the user if he wants to keep it from here
545 // ============================================================================
546 // wxDebugReport-derived classes
547 // ============================================================================
551 // ----------------------------------------------------------------------------
552 // wxDebugReportCompress
553 // ----------------------------------------------------------------------------
555 bool wxDebugReportCompress::DoProcess()
557 const size_t count
= GetFilesCount();
561 // create the streams
562 wxFileName
fn(GetDirectory(), GetReportName(), _T("zip"));
563 wxFFileOutputStream
os(fn
.GetFullPath(), _T("wb"));
564 wxZipOutputStream
zos(os
, 9);
566 // add all files to the ZIP one
568 for ( size_t n
= 0; n
< count
; n
++ )
570 GetFile(n
, &name
, &desc
);
572 wxZipEntry
*ze
= new wxZipEntry(name
);
573 ze
->SetComment(desc
);
575 if ( !zos
.PutNextEntry(ze
) )
578 wxFileName
filename(fn
.GetPath(), name
);
579 wxFFileInputStream
is(filename
.GetFullPath());
580 if ( !is
.IsOk() || !zos
.Write(is
).IsOk() )
587 m_zipfile
= fn
.GetFullPath();
592 // ----------------------------------------------------------------------------
593 // wxDebugReportUpload
594 // ----------------------------------------------------------------------------
596 wxDebugReportUpload::wxDebugReportUpload(const wxString
& url
,
597 const wxString
& input
,
598 const wxString
& action
,
599 const wxString
& curl
)
604 if ( m_uploadURL
.Last() != _T('/') )
605 m_uploadURL
+= _T('/');
606 m_uploadURL
+= action
;
609 bool wxDebugReportUpload::DoProcess()
611 if ( !wxDebugReportCompress::DoProcess() )
615 wxArrayString output
, errors
;
616 int rc
= wxExecute(wxString::Format
618 _T("%s -F %s=@%s %s"),
620 m_inputField
.c_str(),
621 GetCompressedFileName().c_str(),
628 wxLogError(_("Failed to execute curl, please install it in PATH."));
632 const size_t count
= errors
.GetCount();
635 for ( size_t n
= 0; n
< count
; n
++ )
637 wxLogWarning(_T("%s"), errors
[n
].c_str());
641 wxLogError(_("Failed to upload the debug report (error code %d)."), rc
);
645 if ( OnServerReply(output
) )
652 #endif // wxUSE_ZIPSTREAM
654 #endif // wxUSE_DEBUGREPORT