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();
255 wxDebugReport::AddFile(const wxString
& filename
, const wxString
& description
)
258 wxFileName
fn(filename
);
259 if ( fn
.IsAbsolute() )
261 // we need to copy the file to the debug report directory: give it the
263 name
= fn
.GetFullName();
264 wxCopyFile(fn
.GetFullPath(),
265 wxFileName(GetDirectory(), name
).GetFullPath());
267 else // file relative to the report directory
271 wxASSERT_MSG( wxFileName(GetDirectory(), name
).FileExists(),
272 _T("file should exist in debug report directory") );
276 m_descriptions
.Add(description
);
280 wxDebugReport::AddText(const wxString
& filename
,
281 const wxString
& text
,
282 const wxString
& description
)
284 wxASSERT_MSG( !wxFileName(filename
).IsAbsolute(),
285 _T("filename should be relative to debug report directory") );
287 wxFileName
fn(GetDirectory(), filename
);
288 wxFFile
file(fn
.GetFullPath(), _T("w"));
289 if ( !file
.IsOpened() || !file
.Write(text
) )
292 AddFile(filename
, description
);
297 void wxDebugReport::RemoveFile(const wxString
& name
)
299 const int n
= m_files
.Index(name
);
300 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("No such file in wxDebugReport") );
303 m_descriptions
.RemoveAt(n
);
305 wxRemove(wxFileName(GetDirectory(), name
).GetFullPath());
308 bool wxDebugReport::GetFile(size_t n
, wxString
*name
, wxString
*desc
) const
310 if ( n
>= m_files
.GetCount() )
316 *desc
= m_descriptions
[n
];
321 void wxDebugReport::AddAll(Context context
)
323 #if wxUSE_STACKWALKER
325 #endif // wxUSE_STACKWALKER
327 #if wxUSE_CRASHREPORT
329 #endif // wxUSE_CRASHREPORT
331 #if !wxUSE_STACKWALKER && !wxUSE_CRASHREPORT
332 wxUnusedVar(context
);
336 // ----------------------------------------------------------------------------
337 // adding basic text information about current context
338 // ----------------------------------------------------------------------------
340 #if wxUSE_STACKWALKER
342 bool wxDebugReport::DoAddSystemInfo(wxXmlNode
*nodeSystemInfo
)
344 nodeSystemInfo
->AddProperty(_T("description"), wxGetOsDescription());
349 bool wxDebugReport::DoAddLoadedModules(wxXmlNode
*nodeModules
)
351 wxDynamicLibraryDetailsArray
modules(wxDynamicLibrary::ListLoaded());
352 const size_t count
= modules
.GetCount();
356 for ( size_t n
= 0; n
< count
; n
++ )
358 const wxDynamicLibraryDetails
& info
= modules
[n
];
360 wxXmlNode
*nodeModule
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("module"));
361 nodeModules
->AddChild(nodeModule
);
363 wxString path
= info
.GetPath();
365 path
= info
.GetName();
367 nodeModule
->AddProperty(_T("path"), path
);
371 if ( info
.GetAddress(&addr
, &len
) )
373 HexProperty(nodeModule
, _T("address"), (unsigned long)addr
);
374 HexProperty(nodeModule
, _T("size"), len
);
377 wxString ver
= info
.GetVersion();
380 nodeModule
->AddProperty(_T("version"), ver
);
387 bool wxDebugReport::DoAddExceptionInfo(wxXmlNode
*nodeContext
)
389 #if wxUSE_CRASHREPORT
394 wxXmlNode
*nodeExc
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("exception"));
395 nodeContext
->AddChild(nodeExc
);
397 HexProperty(nodeExc
, _T("code"), c
.code
);
398 nodeExc
->AddProperty(_T("name"), c
.GetExceptionString());
399 HexProperty(nodeExc
, _T("address"), (unsigned long)c
.addr
);
402 wxXmlNode
*nodeRegs
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("registers"));
403 nodeContext
->AddChild(nodeRegs
);
404 HexElement(nodeRegs
, _T("eax"), c
.regs
.eax
);
405 HexElement(nodeRegs
, _T("ebx"), c
.regs
.ebx
);
406 HexElement(nodeRegs
, _T("ecx"), c
.regs
.edx
);
407 HexElement(nodeRegs
, _T("edx"), c
.regs
.edx
);
408 HexElement(nodeRegs
, _T("esi"), c
.regs
.esi
);
409 HexElement(nodeRegs
, _T("edi"), c
.regs
.edi
);
411 HexElement(nodeRegs
, _T("ebp"), c
.regs
.ebp
);
412 HexElement(nodeRegs
, _T("esp"), c
.regs
.esp
);
413 HexElement(nodeRegs
, _T("eip"), c
.regs
.eip
);
415 HexElement(nodeRegs
, _T("cs"), c
.regs
.cs
);
416 HexElement(nodeRegs
, _T("ds"), c
.regs
.ds
);
417 HexElement(nodeRegs
, _T("es"), c
.regs
.es
);
418 HexElement(nodeRegs
, _T("fs"), c
.regs
.fs
);
419 HexElement(nodeRegs
, _T("gs"), c
.regs
.gs
);
420 HexElement(nodeRegs
, _T("ss"), c
.regs
.ss
);
422 HexElement(nodeRegs
, _T("flags"), c
.regs
.flags
);
426 #else // !wxUSE_CRASHREPORT
427 wxUnusedVar(nodeContext
);
430 #endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT
433 bool wxDebugReport::AddContext(wxDebugReport::Context ctx
)
435 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
437 // create XML dump of current context
438 wxXmlDocument xmldoc
;
439 wxXmlNode
*nodeRoot
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("report"));
440 xmldoc
.SetRoot(nodeRoot
);
441 nodeRoot
->AddProperty(_T("version"), _T("1.0"));
442 nodeRoot
->AddProperty(_T("kind"), ctx
== Context_Current
? _T("user")
445 // add system information
446 wxXmlNode
*nodeSystemInfo
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("system"));
447 if ( DoAddSystemInfo(nodeSystemInfo
) )
448 nodeRoot
->AddChild(nodeSystemInfo
);
450 delete nodeSystemInfo
;
452 // add information about the loaded modules
453 wxXmlNode
*nodeModules
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("modules"));
454 if ( DoAddLoadedModules(nodeModules
) )
455 nodeRoot
->AddChild(nodeModules
);
459 // add CPU context information: this only makes sense for exceptions as our
460 // current context is not very interesting otherwise
461 if ( ctx
== Context_Exception
)
463 wxXmlNode
*nodeContext
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("context"));
464 if ( DoAddExceptionInfo(nodeContext
) )
465 nodeRoot
->AddChild(nodeContext
);
470 // add stack traceback
471 #if wxUSE_STACKWALKER
472 wxXmlNode
*nodeStack
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("stack"));
473 XmlStackWalker
sw(nodeStack
);
474 if ( ctx
== Context_Exception
)
476 sw
.WalkFromException();
478 else // Context_Current
484 nodeRoot
->AddChild(nodeStack
);
487 #endif // wxUSE_STACKWALKER
489 // finally let the user add any extra information he needs
490 DoAddCustomContext(nodeRoot
);
493 // save the entire context dump in a file
494 wxFileName
fn(m_dir
, GetReportName(), _T("xml"));
496 if ( !xmldoc
.Save(fn
.GetFullPath()) )
499 AddFile(fn
.GetFullName(), _("process context description"));
504 #endif // wxUSE_STACKWALKER
506 // ----------------------------------------------------------------------------
508 // ----------------------------------------------------------------------------
510 #if wxUSE_CRASHREPORT
512 bool wxDebugReport::AddDump(Context ctx
)
514 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
516 wxFileName
fn(m_dir
, GetReportName(), _T("dmp"));
517 wxCrashReport::SetFileName(fn
.GetFullPath());
519 if ( !(ctx
== Context_Exception
? wxCrashReport::Generate()
520 : wxCrashReport::GenerateNow()) )
523 AddFile(fn
.GetFullName(), _("dump of the process state (binary)"));
528 #endif // wxUSE_CRASHREPORT
530 // ----------------------------------------------------------------------------
532 // ----------------------------------------------------------------------------
534 bool wxDebugReport::Process()
536 if ( !GetFilesCount() )
538 wxLogError(_("Debug report generation has failed."));
545 wxLogError(_("Processing debug report has failed, leaving the files in \"%s\" directory."),
546 GetDirectory().c_str());
556 bool wxDebugReport::DoProcess()
558 wxString msg
= _("*** A debug report has been generated\n");
559 msg
+= wxString::Format(_("*** It can be found in \"%s\"\n"),
560 GetDirectory().c_str());
561 msg
+= _("*** And includes the following files:\n");
564 const size_t count
= GetFilesCount();
565 for ( size_t n
= 0; n
< count
; n
++ )
567 GetFile(n
, &name
, &desc
);
568 msg
+= wxString::Format(_("\t%s: %s\n"), name
.c_str(), desc
.c_str());
571 msg
+= _("\nPlease send this report to the program maintainer, thank you!\n");
573 wxLogMessage(_T("%s"), msg
.c_str());
575 // we have to do this or the report would be deleted, and we don't even
576 // have any way to ask the user if he wants to keep it from here
582 // ============================================================================
583 // wxDebugReport-derived classes
584 // ============================================================================
588 // ----------------------------------------------------------------------------
589 // wxDebugReportCompress
590 // ----------------------------------------------------------------------------
592 bool wxDebugReportCompress::DoProcess()
594 const size_t count
= GetFilesCount();
598 // create the streams
599 wxFileName
fn(GetDirectory(), GetReportName(), _T("zip"));
600 wxFFileOutputStream
os(fn
.GetFullPath(), _T("wb"));
601 wxZipOutputStream
zos(os
, 9);
603 // add all files to the ZIP one
605 for ( size_t n
= 0; n
< count
; n
++ )
607 GetFile(n
, &name
, &desc
);
609 wxZipEntry
*ze
= new wxZipEntry(name
);
610 ze
->SetComment(desc
);
612 if ( !zos
.PutNextEntry(ze
) )
615 wxFileName
filename(fn
.GetPath(), name
);
616 wxFFileInputStream
is(filename
.GetFullPath());
617 if ( !is
.IsOk() || !zos
.Write(is
).IsOk() )
624 m_zipfile
= fn
.GetFullPath();
629 // ----------------------------------------------------------------------------
630 // wxDebugReportUpload
631 // ----------------------------------------------------------------------------
633 wxDebugReportUpload::wxDebugReportUpload(const wxString
& url
,
634 const wxString
& input
,
635 const wxString
& action
,
636 const wxString
& curl
)
641 if ( m_uploadURL
.Last() != _T('/') )
642 m_uploadURL
+= _T('/');
643 m_uploadURL
+= action
;
646 bool wxDebugReportUpload::DoProcess()
648 if ( !wxDebugReportCompress::DoProcess() )
652 wxArrayString output
, errors
;
653 int rc
= wxExecute(wxString::Format
655 _T("%s -F %s=@%s %s"),
657 m_inputField
.c_str(),
658 GetCompressedFileName().c_str(),
665 wxLogError(_("Failed to execute curl, please install it in PATH."));
669 const size_t count
= errors
.GetCount();
672 for ( size_t n
= 0; n
< count
; n
++ )
674 wxLogWarning(_T("%s"), errors
[n
].c_str());
678 wxLogError(_("Failed to upload the debug report (error code %d)."), rc
);
682 if ( OnServerReply(output
) )
689 #endif // wxUSE_ZIPSTREAM
691 #endif // wxUSE_DEBUGREPORT