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 #include "wx/ptr_scpd.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
, _T(""), value
));
110 HexElement(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
112 TextElement(node
, name
, wxString::Format(_T("%08lx"), value
));
115 #if wxUSE_STACKWALKER
117 // ============================================================================
118 // XmlStackWalker implementation
119 // ============================================================================
121 void XmlStackWalker::OnStackFrame(const wxStackFrame
& frame
)
125 wxXmlNode
*nodeFrame
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("frame"));
126 m_nodeStack
->AddChild(nodeFrame
);
128 NumProperty(nodeFrame
, _T("level"), frame
.GetLevel());
129 wxString func
= frame
.GetName();
132 nodeFrame
->AddProperty(_T("function"), func
);
133 HexProperty(nodeFrame
, _T("offset"), frame
.GetOffset());
136 if ( frame
.HasSourceLocation() )
138 nodeFrame
->AddProperty(_T("file"), frame
.GetFileName());
139 NumProperty(nodeFrame
, _T("line"), frame
.GetLine());
142 const size_t nParams
= frame
.GetParamCount();
145 wxXmlNode
*nodeParams
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("parameters"));
146 nodeFrame
->AddChild(nodeParams
);
148 for ( size_t n
= 0; n
< nParams
; n
++ )
151 nodeParam
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("parameter"));
152 nodeParams
->AddChild(nodeParam
);
154 NumProperty(nodeParam
, _T("number"), n
);
156 wxString type
, name
, value
;
157 if ( !frame
.GetParam(n
, &type
, &name
, &value
) )
161 TextElement(nodeParam
, _T("type"), type
);
164 TextElement(nodeParam
, _T("name"), name
);
166 if ( !value
.empty() )
167 TextElement(nodeParam
, _T("value"), value
);
172 #endif // wxUSE_STACKWALKER
174 // ============================================================================
175 // wxDebugReport implementation
176 // ============================================================================
178 // ----------------------------------------------------------------------------
179 // initialization and cleanup
180 // ----------------------------------------------------------------------------
182 wxDebugReport::wxDebugReport()
184 // get a temporary directory name
185 wxString appname
= GetReportName();
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
190 // of course, this doesn't protect us against malicious users...
192 fn
.AssignTempFileName(appname
);
193 m_dir
.Printf(_T("%s%c%s_dbgrpt-%lu-%s"),
194 fn
.GetPath().c_str(), wxFILE_SEP_PATH
, appname
.c_str(),
196 wxDateTime::Now().Format(_T("%Y%m%dT%H%M%S")).c_str());
198 // as we are going to save the process state there use restrictive
200 if ( !wxMkdir(m_dir
, 0700) )
202 wxLogSysError(_("Failed to create directory \"%s\""), m_dir
.c_str());
203 wxLogError(_("Debug report couldn't be created."));
209 wxDebugReport::~wxDebugReport()
211 if ( !m_dir
.empty() )
213 // remove all files in this directory
216 for ( bool cont
= dir
.GetFirst(&file
); cont
; cont
= dir
.GetNext(&file
) )
218 if ( wxRemove(wxFileName(m_dir
, file
).GetFullPath()) != 0 )
220 wxLogSysError(_("Failed to remove debug report file \"%s\""),
228 if ( !m_dir
.empty() )
230 // Temp fix: what should this be? eVC++ doesn't like wxRmDir
232 if ( wxRmdir(m_dir
.fn_str()) != 0 )
234 if ( wxRmDir(m_dir
.fn_str()) != 0 )
237 wxLogSysError(_("Failed to clean up debug report directory \"%s\""),
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 wxString
wxDebugReport::GetReportName() const
250 return wxTheApp
->GetAppName();
255 void wxDebugReport::AddFile(const wxString
& name
, const wxString
& description
)
258 m_descriptions
.Add(description
);
262 wxDebugReport::AddText(const wxString
& name
,
263 const wxString
& text
,
264 const wxString
& description
)
266 wxFileName
fn(GetDirectory(), name
);
267 wxFFile
file(fn
.GetFullPath(), _T("w"));
268 if ( !file
.IsOpened() || !file
.Write(text
) )
271 AddFile(name
, description
);
276 void wxDebugReport::RemoveFile(const wxString
& name
)
278 const int n
= m_files
.Index(name
);
279 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("No such file in wxDebugReport") );
282 m_descriptions
.RemoveAt(n
);
284 wxRemove(wxFileName(GetDirectory(), name
).GetFullPath());
287 bool wxDebugReport::GetFile(size_t n
, wxString
*name
, wxString
*desc
) const
289 if ( n
>= m_files
.GetCount() )
295 *desc
= m_descriptions
[n
];
300 void wxDebugReport::AddAll(Context context
)
302 #if wxUSE_STACKWALKER
304 #endif // wxUSE_STACKWALKER
306 #if wxUSE_CRASHREPORT
308 #endif // wxUSE_CRASHREPORT
310 #if !wxUSE_STACKWALKER && !wxUSE_CRASHREPORT
311 wxUnusedVar(context
);
315 // ----------------------------------------------------------------------------
316 // adding basic text information about current context
317 // ----------------------------------------------------------------------------
319 #if wxUSE_STACKWALKER
321 bool wxDebugReport::DoAddSystemInfo(wxXmlNode
*nodeSystemInfo
)
323 nodeSystemInfo
->AddProperty(_T("description"), wxGetOsDescription());
328 bool wxDebugReport::DoAddLoadedModules(wxXmlNode
*nodeModules
)
330 wxDynamicLibraryDetailsArray
modules(wxDynamicLibrary::ListLoaded());
331 const size_t count
= modules
.GetCount();
335 for ( size_t n
= 0; n
< count
; n
++ )
337 const wxDynamicLibraryDetails
& info
= modules
[n
];
339 wxXmlNode
*nodeModule
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("module"));
340 nodeModules
->AddChild(nodeModule
);
342 wxString path
= info
.GetPath();
344 path
= info
.GetName();
346 nodeModule
->AddProperty(_T("path"), path
);
350 if ( info
.GetAddress(&addr
, &len
) )
352 HexProperty(nodeModule
, _T("address"), (unsigned long)addr
);
353 HexProperty(nodeModule
, _T("size"), len
);
356 wxString ver
= info
.GetVersion();
359 nodeModule
->AddProperty(_T("version"), ver
);
366 bool wxDebugReport::DoAddExceptionInfo(wxXmlNode
*nodeContext
)
368 #if wxUSE_CRASHREPORT
373 wxXmlNode
*nodeExc
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("exception"));
374 nodeContext
->AddChild(nodeExc
);
376 HexProperty(nodeExc
, _T("code"), c
.code
);
377 nodeExc
->AddProperty(_T("name"), c
.GetExceptionString());
378 HexProperty(nodeExc
, _T("address"), (unsigned long)c
.addr
);
381 wxXmlNode
*nodeRegs
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("registers"));
382 nodeContext
->AddChild(nodeRegs
);
383 HexElement(nodeRegs
, _T("eax"), c
.regs
.eax
);
384 HexElement(nodeRegs
, _T("ebx"), c
.regs
.ebx
);
385 HexElement(nodeRegs
, _T("ecx"), c
.regs
.edx
);
386 HexElement(nodeRegs
, _T("edx"), c
.regs
.edx
);
387 HexElement(nodeRegs
, _T("esi"), c
.regs
.esi
);
388 HexElement(nodeRegs
, _T("edi"), c
.regs
.edi
);
390 HexElement(nodeRegs
, _T("ebp"), c
.regs
.ebp
);
391 HexElement(nodeRegs
, _T("esp"), c
.regs
.esp
);
392 HexElement(nodeRegs
, _T("eip"), c
.regs
.eip
);
394 HexElement(nodeRegs
, _T("cs"), c
.regs
.cs
);
395 HexElement(nodeRegs
, _T("ds"), c
.regs
.ds
);
396 HexElement(nodeRegs
, _T("es"), c
.regs
.es
);
397 HexElement(nodeRegs
, _T("fs"), c
.regs
.fs
);
398 HexElement(nodeRegs
, _T("gs"), c
.regs
.gs
);
399 HexElement(nodeRegs
, _T("ss"), c
.regs
.ss
);
401 HexElement(nodeRegs
, _T("flags"), c
.regs
.flags
);
405 #else // !wxUSE_CRASHREPORT
406 wxUnusedVar(nodeContext
);
409 #endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT
412 bool wxDebugReport::AddContext(wxDebugReport::Context ctx
)
414 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
416 // create XML dump of current context
417 wxXmlDocument xmldoc
;
418 wxXmlNode
*nodeRoot
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("report"));
419 xmldoc
.SetRoot(nodeRoot
);
420 nodeRoot
->AddProperty(_T("version"), _T("1.0"));
421 nodeRoot
->AddProperty(_T("kind"), ctx
== Context_Current
? _T("user")
424 // add system information
425 wxXmlNode
*nodeSystemInfo
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("system"));
426 if ( DoAddSystemInfo(nodeSystemInfo
) )
427 nodeRoot
->AddChild(nodeSystemInfo
);
429 delete nodeSystemInfo
;
431 // add information about the loaded modules
432 wxXmlNode
*nodeModules
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("modules"));
433 if ( DoAddLoadedModules(nodeModules
) )
434 nodeRoot
->AddChild(nodeModules
);
438 // add CPU context information: this only makes sense for exceptions as our
439 // current context is not very interesting otherwise
440 if ( ctx
== Context_Exception
)
442 wxXmlNode
*nodeContext
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("context"));
443 if ( DoAddExceptionInfo(nodeContext
) )
444 nodeRoot
->AddChild(nodeContext
);
449 // add stack traceback
450 #if wxUSE_STACKWALKER
451 wxXmlNode
*nodeStack
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("stack"));
452 XmlStackWalker
sw(nodeStack
);
453 if ( ctx
== Context_Exception
)
455 sw
.WalkFromException();
457 else // Context_Current
463 nodeRoot
->AddChild(nodeStack
);
466 #endif // wxUSE_STACKWALKER
468 // finally let the user add any extra information he needs
469 DoAddCustomContext(nodeRoot
);
472 // save the entire context dump in a file
473 wxFileName
fn(m_dir
, GetReportName(), _T("xml"));
475 if ( !xmldoc
.Save(fn
.GetFullPath()) )
478 AddFile(fn
.GetFullName(), _("process context description"));
483 #endif // wxUSE_STACKWALKER
485 // ----------------------------------------------------------------------------
487 // ----------------------------------------------------------------------------
489 #if wxUSE_CRASHREPORT
491 bool wxDebugReport::AddDump(Context ctx
)
493 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
495 wxFileName
fn(m_dir
, GetReportName(), _T("dmp"));
496 wxCrashReport::SetFileName(fn
.GetFullPath());
498 if ( !(ctx
== Context_Exception
? wxCrashReport::Generate()
499 : wxCrashReport::GenerateNow()) )
502 AddFile(fn
.GetFullName(), _("dump of the process state (binary)"));
507 #endif // wxUSE_CRASHREPORT
509 // ----------------------------------------------------------------------------
511 // ----------------------------------------------------------------------------
513 bool wxDebugReport::Process()
515 if ( !GetFilesCount() )
517 wxLogError(_("Debug report generation has failed."));
524 wxLogError(_("Processing debug report has failed, leaving the files in \"%s\" directory."),
525 GetDirectory().c_str());
535 bool wxDebugReport::DoProcess()
537 wxString msg
= _("*** A debug report has been generated\n");
538 msg
+= wxString::Format(_("*** It can be found in \"%s\"\n"),
539 GetDirectory().c_str());
540 msg
+= _("*** And includes the following files:\n");
543 const size_t count
= GetFilesCount();
544 for ( size_t n
= 0; n
< count
; n
++ )
546 GetFile(n
, &name
, &desc
);
547 msg
+= wxString::Format(_("\t%s: %s\n"), name
.c_str(), desc
.c_str());
550 msg
+= _("\nPlease send this report to the program maintainer, thank you!\n");
552 wxLogMessage(_T("%s"), msg
.c_str());
554 // we have to do this or the report would be deleted, and we don't even
555 // have any way to ask the user if he wants to keep it from here
561 // ============================================================================
562 // wxDebugReport-derived classes
563 // ============================================================================
567 // leave the default name wxZipOutputStreamPtr free for users
568 wxDECLARE_SCOPED_PTR(wxZipOutputStream
, wxDbgZipOutputStreamPtr
)
569 wxDEFINE_SCOPED_PTR(wxZipOutputStream
, wxDbgZipOutputStreamPtr
)
571 // ----------------------------------------------------------------------------
572 // wxDebugReportCompress
573 // ----------------------------------------------------------------------------
575 bool wxDebugReportCompress::DoProcess()
577 const size_t count
= GetFilesCount();
581 // create the streams
582 wxFileName
fn(GetDirectory(), GetReportName(), _T("zip"));
583 wxFFileOutputStream
os(fn
.GetFullPath(), _T("wb"));
585 // create this one on the heap as a workaround since otherwise the mingw
586 // 3.2.3 linker cannot find ~wxZipOutputStream() when building a dll
587 // version of the library.
588 wxDbgZipOutputStreamPtr
zos(new wxZipOutputStream(os
, 9));
590 // add all files to the ZIP one
592 for ( size_t n
= 0; n
< count
; n
++ )
594 GetFile(n
, &name
, &desc
);
596 wxZipEntry
*ze
= new wxZipEntry(name
);
597 ze
->SetComment(desc
);
599 if ( !zos
->PutNextEntry(ze
) )
602 wxFileName
filename(fn
.GetPath(), name
);
603 wxFFileInputStream
is(filename
.GetFullPath());
604 if ( !is
.IsOk() || !zos
->Write(is
).IsOk() )
611 m_zipfile
= fn
.GetFullPath();
616 // ----------------------------------------------------------------------------
617 // wxDebugReportUpload
618 // ----------------------------------------------------------------------------
620 wxDebugReportUpload::wxDebugReportUpload(const wxString
& url
,
621 const wxString
& input
,
622 const wxString
& action
,
623 const wxString
& curl
)
628 if ( m_uploadURL
.Last() != _T('/') )
629 m_uploadURL
+= _T('/');
630 m_uploadURL
+= action
;
633 bool wxDebugReportUpload::DoProcess()
635 if ( !wxDebugReportCompress::DoProcess() )
639 wxArrayString output
, errors
;
640 int rc
= wxExecute(wxString::Format
642 _T("%s -F %s=@%s %s"),
644 m_inputField
.c_str(),
645 GetCompressedFileName().c_str(),
652 wxLogError(_("Failed to execute curl, please install it in PATH."));
656 const size_t count
= errors
.GetCount();
659 for ( size_t n
= 0; n
< count
; n
++ )
661 wxLogWarning(_T("%s"), errors
[n
].c_str());
665 wxLogError(_("Failed to upload the debug report (error code %d)."), rc
);
669 if ( OnServerReply(output
) )
676 #endif // wxUSE_ZIPSTREAM
678 #endif // wxUSE_DEBUGREPORT