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"
33 #if wxUSE_DEBUGREPORT && wxUSE_XML
35 #include "wx/debugrpt.h"
38 #include "wx/filename.h"
40 #include "wx/dynlib.h"
42 #include "wx/xml/xml.h"
45 #include "wx/stackwalk.h"
49 #include "wx/msw/crashrpt.h"
53 #include "wx/wfstream.h"
54 #include "wx/zipstrm.h"
55 #endif // wxUSE_ZIPSTREAM
57 WX_CHECK_BUILD_OPTIONS("wxQA")
59 // ----------------------------------------------------------------------------
60 // XmlStackWalker: stack walker specialization which dumps stack in XML
61 // ----------------------------------------------------------------------------
65 class XmlStackWalker
: public wxStackWalker
68 XmlStackWalker(wxXmlNode
*nodeStack
)
71 m_nodeStack
= nodeStack
;
74 bool IsOk() const { return m_isOk
; }
77 virtual void OnStackFrame(const wxStackFrame
& frame
);
79 wxXmlNode
*m_nodeStack
;
83 #endif // wxUSE_STACKWALKER
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
90 HexProperty(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
92 node
->AddProperty(name
, wxString::Format(_T("%08lx"), value
));
96 NumProperty(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
98 node
->AddProperty(name
, wxString::Format(_T("%lu"), value
));
102 TextElement(wxXmlNode
*node
, const wxChar
*name
, const wxString
& value
)
104 wxXmlNode
*nodeChild
= new wxXmlNode(wxXML_ELEMENT_NODE
, name
);
105 node
->AddChild(nodeChild
);
106 nodeChild
->AddChild(new wxXmlNode(wxXML_TEXT_NODE
, wxEmptyString
, value
));
109 #if wxUSE_CRASHREPORT && defined(__INTEL__)
112 HexElement(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
114 TextElement(node
, name
, wxString::Format(_T("%08lx"), value
));
117 #endif // wxUSE_CRASHREPORT
119 #if wxUSE_STACKWALKER
121 // ============================================================================
122 // XmlStackWalker implementation
123 // ============================================================================
125 void XmlStackWalker::OnStackFrame(const wxStackFrame
& frame
)
129 wxXmlNode
*nodeFrame
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("frame"));
130 m_nodeStack
->AddChild(nodeFrame
);
132 NumProperty(nodeFrame
, _T("level"), frame
.GetLevel());
133 wxString func
= frame
.GetName();
136 nodeFrame
->AddProperty(_T("function"), func
);
137 HexProperty(nodeFrame
, _T("offset"), frame
.GetOffset());
140 if ( frame
.HasSourceLocation() )
142 nodeFrame
->AddProperty(_T("file"), frame
.GetFileName());
143 NumProperty(nodeFrame
, _T("line"), frame
.GetLine());
146 const size_t nParams
= frame
.GetParamCount();
149 wxXmlNode
*nodeParams
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("parameters"));
150 nodeFrame
->AddChild(nodeParams
);
152 for ( size_t n
= 0; n
< nParams
; n
++ )
155 nodeParam
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("parameter"));
156 nodeParams
->AddChild(nodeParam
);
158 NumProperty(nodeParam
, _T("number"), n
);
160 wxString type
, name
, value
;
161 if ( !frame
.GetParam(n
, &type
, &name
, &value
) )
165 TextElement(nodeParam
, _T("type"), type
);
168 TextElement(nodeParam
, _T("name"), name
);
170 if ( !value
.empty() )
171 TextElement(nodeParam
, _T("value"), value
);
176 #endif // wxUSE_STACKWALKER
178 // ============================================================================
179 // wxDebugReport implementation
180 // ============================================================================
182 // ----------------------------------------------------------------------------
183 // initialization and cleanup
184 // ----------------------------------------------------------------------------
186 wxDebugReport::wxDebugReport()
188 // get a temporary directory name
189 wxString appname
= GetReportName();
191 // we can't use CreateTempFileName() because it creates a file, not a
192 // directory, so do our best to create a unique name ourselves
194 // of course, this doesn't protect us against malicious users...
196 fn
.AssignTempFileName(appname
);
197 m_dir
.Printf(_T("%s%c%s_dbgrpt-%lu-%s"),
198 fn
.GetPath().c_str(), wxFILE_SEP_PATH
, appname
.c_str(),
200 wxDateTime::Now().Format(_T("%Y%m%dT%H%M%S")).c_str());
202 // as we are going to save the process state there use restrictive
204 if ( !wxMkdir(m_dir
, 0700) )
206 wxLogSysError(_("Failed to create directory \"%s\""), m_dir
.c_str());
207 wxLogError(_("Debug report couldn't be created."));
213 wxDebugReport::~wxDebugReport()
215 if ( !m_dir
.empty() )
217 // remove all files in this directory
220 for ( bool cont
= dir
.GetFirst(&file
); cont
; cont
= dir
.GetNext(&file
) )
222 if ( wxRemove(wxFileName(m_dir
, file
).GetFullPath()) != 0 )
224 wxLogSysError(_("Failed to remove debug report file \"%s\""),
232 if ( !m_dir
.empty() )
234 // Temp fix: what should this be? eVC++ doesn't like wxRmDir
236 if ( wxRmdir(m_dir
.fn_str()) != 0 )
238 if ( wxRmDir(m_dir
.fn_str()) != 0 )
241 wxLogSysError(_("Failed to clean up debug report directory \"%s\""),
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 wxString
wxDebugReport::GetReportName() const
254 return wxTheApp
->GetAppName();
260 wxDebugReport::AddFile(const wxString
& filename
, const wxString
& description
)
263 wxFileName
fn(filename
);
264 if ( fn
.IsAbsolute() )
266 // we need to copy the file to the debug report directory: give it the
268 name
= fn
.GetFullName();
269 wxCopyFile(fn
.GetFullPath(),
270 wxFileName(GetDirectory(), name
).GetFullPath());
272 else // file relative to the report directory
276 wxASSERT_MSG( wxFileName(GetDirectory(), name
).FileExists(),
277 _T("file should exist in debug report directory") );
281 m_descriptions
.Add(description
);
285 wxDebugReport::AddText(const wxString
& filename
,
286 const wxString
& text
,
287 const wxString
& description
)
289 wxASSERT_MSG( !wxFileName(filename
).IsAbsolute(),
290 _T("filename should be relative to debug report directory") );
292 wxFileName
fn(GetDirectory(), filename
);
293 wxFFile
file(fn
.GetFullPath(), _T("w"));
294 if ( !file
.IsOpened() || !file
.Write(text
) )
297 AddFile(filename
, description
);
302 void wxDebugReport::RemoveFile(const wxString
& name
)
304 const int n
= m_files
.Index(name
);
305 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("No such file in wxDebugReport") );
308 m_descriptions
.RemoveAt(n
);
310 wxRemove(wxFileName(GetDirectory(), name
).GetFullPath());
313 bool wxDebugReport::GetFile(size_t n
, wxString
*name
, wxString
*desc
) const
315 if ( n
>= m_files
.GetCount() )
321 *desc
= m_descriptions
[n
];
326 void wxDebugReport::AddAll(Context context
)
328 #if wxUSE_STACKWALKER
330 #endif // wxUSE_STACKWALKER
332 #if wxUSE_CRASHREPORT
334 #endif // wxUSE_CRASHREPORT
336 #if !wxUSE_STACKWALKER && !wxUSE_CRASHREPORT
337 wxUnusedVar(context
);
341 // ----------------------------------------------------------------------------
342 // adding basic text information about current context
343 // ----------------------------------------------------------------------------
345 #if wxUSE_STACKWALKER
347 bool wxDebugReport::DoAddSystemInfo(wxXmlNode
*nodeSystemInfo
)
349 nodeSystemInfo
->AddProperty(_T("description"), wxGetOsDescription());
354 bool wxDebugReport::DoAddLoadedModules(wxXmlNode
*nodeModules
)
356 wxDynamicLibraryDetailsArray
modules(wxDynamicLibrary::ListLoaded());
357 const size_t count
= modules
.GetCount();
361 for ( size_t n
= 0; n
< count
; n
++ )
363 const wxDynamicLibraryDetails
& info
= modules
[n
];
365 wxXmlNode
*nodeModule
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("module"));
366 nodeModules
->AddChild(nodeModule
);
368 wxString path
= info
.GetPath();
370 path
= info
.GetName();
372 nodeModule
->AddProperty(_T("path"), path
);
376 if ( info
.GetAddress(&addr
, &len
) )
378 HexProperty(nodeModule
, _T("address"), wxPtrToUInt(addr
));
379 HexProperty(nodeModule
, _T("size"), len
);
382 wxString ver
= info
.GetVersion();
385 nodeModule
->AddProperty(_T("version"), ver
);
392 bool wxDebugReport::DoAddExceptionInfo(wxXmlNode
*nodeContext
)
394 #if wxUSE_CRASHREPORT
399 wxXmlNode
*nodeExc
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("exception"));
400 nodeContext
->AddChild(nodeExc
);
402 HexProperty(nodeExc
, _T("code"), c
.code
);
403 nodeExc
->AddProperty(_T("name"), c
.GetExceptionString());
404 HexProperty(nodeExc
, _T("address"), wxPtrToUInt(c
.addr
));
407 wxXmlNode
*nodeRegs
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("registers"));
408 nodeContext
->AddChild(nodeRegs
);
409 HexElement(nodeRegs
, _T("eax"), c
.regs
.eax
);
410 HexElement(nodeRegs
, _T("ebx"), c
.regs
.ebx
);
411 HexElement(nodeRegs
, _T("ecx"), c
.regs
.edx
);
412 HexElement(nodeRegs
, _T("edx"), c
.regs
.edx
);
413 HexElement(nodeRegs
, _T("esi"), c
.regs
.esi
);
414 HexElement(nodeRegs
, _T("edi"), c
.regs
.edi
);
416 HexElement(nodeRegs
, _T("ebp"), c
.regs
.ebp
);
417 HexElement(nodeRegs
, _T("esp"), c
.regs
.esp
);
418 HexElement(nodeRegs
, _T("eip"), c
.regs
.eip
);
420 HexElement(nodeRegs
, _T("cs"), c
.regs
.cs
);
421 HexElement(nodeRegs
, _T("ds"), c
.regs
.ds
);
422 HexElement(nodeRegs
, _T("es"), c
.regs
.es
);
423 HexElement(nodeRegs
, _T("fs"), c
.regs
.fs
);
424 HexElement(nodeRegs
, _T("gs"), c
.regs
.gs
);
425 HexElement(nodeRegs
, _T("ss"), c
.regs
.ss
);
427 HexElement(nodeRegs
, _T("flags"), c
.regs
.flags
);
431 #else // !wxUSE_CRASHREPORT
432 wxUnusedVar(nodeContext
);
435 #endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT
438 bool wxDebugReport::AddContext(wxDebugReport::Context ctx
)
440 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
442 // create XML dump of current context
443 wxXmlDocument xmldoc
;
444 wxXmlNode
*nodeRoot
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("report"));
445 xmldoc
.SetRoot(nodeRoot
);
446 nodeRoot
->AddProperty(_T("version"), _T("1.0"));
447 nodeRoot
->AddProperty(_T("kind"), ctx
== Context_Current
? _T("user")
450 // add system information
451 wxXmlNode
*nodeSystemInfo
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("system"));
452 if ( DoAddSystemInfo(nodeSystemInfo
) )
453 nodeRoot
->AddChild(nodeSystemInfo
);
455 delete nodeSystemInfo
;
457 // add information about the loaded modules
458 wxXmlNode
*nodeModules
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("modules"));
459 if ( DoAddLoadedModules(nodeModules
) )
460 nodeRoot
->AddChild(nodeModules
);
464 // add CPU context information: this only makes sense for exceptions as our
465 // current context is not very interesting otherwise
466 if ( ctx
== Context_Exception
)
468 wxXmlNode
*nodeContext
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("context"));
469 if ( DoAddExceptionInfo(nodeContext
) )
470 nodeRoot
->AddChild(nodeContext
);
475 // add stack traceback
476 #if wxUSE_STACKWALKER
477 wxXmlNode
*nodeStack
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("stack"));
478 XmlStackWalker
sw(nodeStack
);
479 if ( ctx
== Context_Exception
)
481 sw
.WalkFromException();
483 else // Context_Current
489 nodeRoot
->AddChild(nodeStack
);
492 #endif // wxUSE_STACKWALKER
494 // finally let the user add any extra information he needs
495 DoAddCustomContext(nodeRoot
);
498 // save the entire context dump in a file
499 wxFileName
fn(m_dir
, GetReportName(), _T("xml"));
501 if ( !xmldoc
.Save(fn
.GetFullPath()) )
504 AddFile(fn
.GetFullName(), _("process context description"));
509 #endif // wxUSE_STACKWALKER
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
515 #if wxUSE_CRASHREPORT
517 bool wxDebugReport::AddDump(Context ctx
)
519 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
521 wxFileName
fn(m_dir
, GetReportName(), _T("dmp"));
522 wxCrashReport::SetFileName(fn
.GetFullPath());
524 if ( !(ctx
== Context_Exception
? wxCrashReport::Generate()
525 : wxCrashReport::GenerateNow()) )
528 AddFile(fn
.GetFullName(), _("dump of the process state (binary)"));
533 #endif // wxUSE_CRASHREPORT
535 // ----------------------------------------------------------------------------
537 // ----------------------------------------------------------------------------
539 bool wxDebugReport::Process()
541 if ( !GetFilesCount() )
543 wxLogError(_("Debug report generation has failed."));
550 wxLogError(_("Processing debug report has failed, leaving the files in \"%s\" directory."),
551 GetDirectory().c_str());
561 bool wxDebugReport::DoProcess()
563 wxString msg
= _("*** A debug report has been generated\n");
564 msg
+= wxString::Format(_("*** It can be found in \"%s\"\n"),
565 GetDirectory().c_str());
566 msg
+= _("*** And includes the following files:\n");
569 const size_t count
= GetFilesCount();
570 for ( size_t n
= 0; n
< count
; n
++ )
572 GetFile(n
, &name
, &desc
);
573 msg
+= wxString::Format(_("\t%s: %s\n"), name
.c_str(), desc
.c_str());
576 msg
+= _("\nPlease send this report to the program maintainer, thank you!\n");
578 wxLogMessage(_T("%s"), msg
.c_str());
580 // we have to do this or the report would be deleted, and we don't even
581 // have any way to ask the user if he wants to keep it from here
587 // ============================================================================
588 // wxDebugReport-derived classes
589 // ============================================================================
593 // ----------------------------------------------------------------------------
594 // wxDebugReportCompress
595 // ----------------------------------------------------------------------------
597 bool wxDebugReportCompress::DoProcess()
599 const size_t count
= GetFilesCount();
603 // create the streams
604 wxFileName
fn(GetDirectory(), GetReportName(), _T("zip"));
605 wxFFileOutputStream
os(fn
.GetFullPath(), _T("wb"));
606 wxZipOutputStream
zos(os
, 9);
608 // add all files to the ZIP one
610 for ( size_t n
= 0; n
< count
; n
++ )
612 GetFile(n
, &name
, &desc
);
614 wxZipEntry
*ze
= new wxZipEntry(name
);
615 ze
->SetComment(desc
);
617 if ( !zos
.PutNextEntry(ze
) )
620 wxFileName
filename(fn
.GetPath(), name
);
621 wxFFileInputStream
is(filename
.GetFullPath());
622 if ( !is
.IsOk() || !zos
.Write(is
).IsOk() )
629 m_zipfile
= fn
.GetFullPath();
634 // ----------------------------------------------------------------------------
635 // wxDebugReportUpload
636 // ----------------------------------------------------------------------------
638 wxDebugReportUpload::wxDebugReportUpload(const wxString
& url
,
639 const wxString
& input
,
640 const wxString
& action
,
641 const wxString
& curl
)
646 if ( m_uploadURL
.Last() != _T('/') )
647 m_uploadURL
+= _T('/');
648 m_uploadURL
+= action
;
651 bool wxDebugReportUpload::DoProcess()
653 if ( !wxDebugReportCompress::DoProcess() )
657 wxArrayString output
, errors
;
658 int rc
= wxExecute(wxString::Format
660 _T("%s -F %s=@\"%s\" %s"),
662 m_inputField
.c_str(),
663 GetCompressedFileName().c_str(),
670 wxLogError(_("Failed to execute curl, please install it in PATH."));
674 const size_t count
= errors
.GetCount();
677 for ( size_t n
= 0; n
< count
; n
++ )
679 wxLogWarning(_T("%s"), errors
[n
].c_str());
683 wxLogError(_("Failed to upload the debug report (error code %d)."), rc
);
687 if ( OnServerReply(output
) )
694 #endif // wxUSE_ZIPSTREAM
696 #endif // wxUSE_DEBUGREPORT