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
);
261 void wxDebugReport::RemoveFile(const wxString
& name
)
263 const int n
= m_files
.Index(name
);
264 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("No such file in wxDebugReport") );
267 m_descriptions
.RemoveAt(n
);
269 wxRemove(wxFileName(GetDirectory(), name
).GetFullPath());
272 bool wxDebugReport::GetFile(size_t n
, wxString
*name
, wxString
*desc
) const
274 if ( n
>= m_files
.GetCount() )
280 *desc
= m_descriptions
[n
];
285 void wxDebugReport::AddAll(Context context
)
287 #if wxUSE_STACKWALKER
289 #endif // wxUSE_STACKWALKER
291 #if wxUSE_CRASHREPORT
293 #endif // wxUSE_CRASHREPORT
295 #if !wxUSE_STACKWALKER && !wxUSE_CRASHREPORT
296 wxUnusedVar(context
);
300 // ----------------------------------------------------------------------------
301 // adding basic text information about current context
302 // ----------------------------------------------------------------------------
304 #if wxUSE_STACKWALKER
306 bool wxDebugReport::DoAddSystemInfo(wxXmlNode
*nodeSystemInfo
)
308 nodeSystemInfo
->AddProperty(_T("description"), wxGetOsDescription());
313 bool wxDebugReport::DoAddLoadedModules(wxXmlNode
*nodeModules
)
315 wxDynamicLibraryDetailsArray
modules(wxDynamicLibrary::ListLoaded());
316 const size_t count
= modules
.GetCount();
320 for ( size_t n
= 0; n
< count
; n
++ )
322 const wxDynamicLibraryDetails
& info
= modules
[n
];
324 wxXmlNode
*nodeModule
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("module"));
325 nodeModules
->AddChild(nodeModule
);
327 wxString path
= info
.GetPath();
329 path
= info
.GetName();
331 nodeModule
->AddProperty(_T("path"), path
);
335 if ( info
.GetAddress(&addr
, &len
) )
337 HexProperty(nodeModule
, _T("address"), (unsigned long)addr
);
338 HexProperty(nodeModule
, _T("size"), len
);
341 wxString ver
= info
.GetVersion();
344 nodeModule
->AddProperty(_T("version"), ver
);
351 bool wxDebugReport::DoAddExceptionInfo(wxXmlNode
*nodeContext
)
353 #if wxUSE_CRASHREPORT
358 wxXmlNode
*nodeExc
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("exception"));
359 nodeContext
->AddChild(nodeExc
);
361 HexProperty(nodeExc
, _T("code"), c
.code
);
362 nodeExc
->AddProperty(_T("name"), c
.GetExceptionString());
363 HexProperty(nodeExc
, _T("address"), (unsigned long)c
.addr
);
366 wxXmlNode
*nodeRegs
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("registers"));
367 nodeContext
->AddChild(nodeRegs
);
368 HexElement(nodeRegs
, _T("eax"), c
.regs
.eax
);
369 HexElement(nodeRegs
, _T("ebx"), c
.regs
.ebx
);
370 HexElement(nodeRegs
, _T("ecx"), c
.regs
.edx
);
371 HexElement(nodeRegs
, _T("edx"), c
.regs
.edx
);
372 HexElement(nodeRegs
, _T("esi"), c
.regs
.esi
);
373 HexElement(nodeRegs
, _T("edi"), c
.regs
.edi
);
375 HexElement(nodeRegs
, _T("ebp"), c
.regs
.ebp
);
376 HexElement(nodeRegs
, _T("esp"), c
.regs
.esp
);
377 HexElement(nodeRegs
, _T("eip"), c
.regs
.eip
);
379 HexElement(nodeRegs
, _T("cs"), c
.regs
.cs
);
380 HexElement(nodeRegs
, _T("ds"), c
.regs
.ds
);
381 HexElement(nodeRegs
, _T("es"), c
.regs
.es
);
382 HexElement(nodeRegs
, _T("fs"), c
.regs
.fs
);
383 HexElement(nodeRegs
, _T("gs"), c
.regs
.gs
);
384 HexElement(nodeRegs
, _T("ss"), c
.regs
.ss
);
386 HexElement(nodeRegs
, _T("flags"), c
.regs
.flags
);
390 #else // !wxUSE_CRASHREPORT
391 wxUnusedVar(nodeContext
);
394 #endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT
397 bool wxDebugReport::AddContext(wxDebugReport::Context ctx
)
399 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
401 // create XML dump of current context
402 wxXmlDocument xmldoc
;
403 wxXmlNode
*nodeRoot
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("report"));
404 xmldoc
.SetRoot(nodeRoot
);
405 nodeRoot
->AddProperty(_T("version"), _T("1.0"));
406 nodeRoot
->AddProperty(_T("kind"), ctx
== Context_Current
? _T("user")
409 // add system information
410 wxXmlNode
*nodeSystemInfo
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("system"));
411 if ( DoAddSystemInfo(nodeSystemInfo
) )
412 nodeRoot
->AddChild(nodeSystemInfo
);
414 delete nodeSystemInfo
;
416 // add information about the loaded modules
417 wxXmlNode
*nodeModules
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("modules"));
418 if ( DoAddLoadedModules(nodeModules
) )
419 nodeRoot
->AddChild(nodeModules
);
423 // add CPU context information: this only makes sense for exceptions as our
424 // current context is not very interesting otherwise
425 if ( ctx
== Context_Exception
)
427 wxXmlNode
*nodeContext
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("context"));
428 if ( DoAddExceptionInfo(nodeContext
) )
429 nodeRoot
->AddChild(nodeContext
);
434 // add stack traceback
435 #if wxUSE_STACKWALKER
436 wxXmlNode
*nodeStack
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("stack"));
437 XmlStackWalker
sw(nodeStack
);
438 if ( ctx
== Context_Exception
)
440 sw
.WalkFromException();
442 else // Context_Current
448 nodeRoot
->AddChild(nodeStack
);
451 #endif // wxUSE_STACKWALKER
453 // finally let the user add any extra information he needs
454 DoAddCustomContext(nodeRoot
);
457 // save the entire context dump in a file
458 wxFileName
fn(m_dir
, GetReportName(), _T("xml"));
460 if ( !xmldoc
.Save(fn
.GetFullPath()) )
463 AddFile(fn
.GetFullName(), _("process context description"));
468 #endif // wxUSE_STACKWALKER
470 // ----------------------------------------------------------------------------
472 // ----------------------------------------------------------------------------
474 #if wxUSE_CRASHREPORT
476 bool wxDebugReport::AddDump(Context ctx
)
478 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
480 wxFileName
fn(m_dir
, GetReportName(), _T("dmp"));
481 wxCrashReport::SetFileName(fn
.GetFullPath());
483 if ( !(ctx
== Context_Exception
? wxCrashReport::Generate()
484 : wxCrashReport::GenerateNow()) )
487 AddFile(fn
.GetFullName(), _("dump of the process state (binary)"));
492 #endif // wxUSE_CRASHREPORT
494 // ----------------------------------------------------------------------------
496 // ----------------------------------------------------------------------------
498 bool wxDebugReport::Process()
500 if ( !GetFilesCount() )
502 wxLogError(_("Debug report generation has failed."));
509 wxLogError(_("Processing debug report has failed, leaving the files in \"%s\" directory."),
510 GetDirectory().c_str());
520 bool wxDebugReport::DoProcess()
522 wxString msg
= _("*** A debug report has been generated\n");
523 msg
+= wxString::Format(_("*** It can be found in \"%s\"\n"),
524 GetDirectory().c_str());
525 msg
+= _("*** And includes the following files:\n");
528 const size_t count
= GetFilesCount();
529 for ( size_t n
= 0; n
< count
; n
++ )
531 GetFile(n
, &name
, &desc
);
532 msg
+= wxString::Format(_("\t%s: %s\n"), name
.c_str(), desc
.c_str());
535 msg
+= _("\nPlease send this report to the program maintainer, thank you!\n");
537 wxLogMessage(_T("%s"), msg
.c_str());
539 // we have to do this or the report would be deleted, and we don't even
540 // have any way to ask the user if he wants to keep it from here
546 // ============================================================================
547 // wxDebugReport-derived classes
548 // ============================================================================
552 // leave the default name wxZipOutputStreamPtr free for users
553 wxDECLARE_SCOPED_PTR(wxZipOutputStream
, wxDbgZipOutputStreamPtr
)
554 wxDEFINE_SCOPED_PTR(wxZipOutputStream
, wxDbgZipOutputStreamPtr
)
556 // ----------------------------------------------------------------------------
557 // wxDebugReportCompress
558 // ----------------------------------------------------------------------------
560 bool wxDebugReportCompress::DoProcess()
562 const size_t count
= GetFilesCount();
566 // create the streams
567 wxFileName
fn(GetDirectory(), GetReportName(), _T("zip"));
568 wxFFileOutputStream
os(fn
.GetFullPath(), _T("wb"));
570 // create this one on the heap as a workaround since otherwise the mingw
571 // 3.2.3 linker cannot find ~wxZipOutputStream() when building a dll
572 // version of the library.
573 wxDbgZipOutputStreamPtr
zos(new wxZipOutputStream(os
, 9));
575 // add all files to the ZIP one
577 for ( size_t n
= 0; n
< count
; n
++ )
579 GetFile(n
, &name
, &desc
);
581 wxZipEntry
*ze
= new wxZipEntry(name
);
582 ze
->SetComment(desc
);
584 if ( !zos
->PutNextEntry(ze
) )
587 wxFileName
filename(fn
.GetPath(), name
);
588 wxFFileInputStream
is(filename
.GetFullPath());
589 if ( !is
.IsOk() || !zos
->Write(is
).IsOk() )
596 m_zipfile
= fn
.GetFullPath();
601 // ----------------------------------------------------------------------------
602 // wxDebugReportUpload
603 // ----------------------------------------------------------------------------
605 wxDebugReportUpload::wxDebugReportUpload(const wxString
& url
,
606 const wxString
& input
,
607 const wxString
& action
,
608 const wxString
& curl
)
613 if ( m_uploadURL
.Last() != _T('/') )
614 m_uploadURL
+= _T('/');
615 m_uploadURL
+= action
;
618 bool wxDebugReportUpload::DoProcess()
620 if ( !wxDebugReportCompress::DoProcess() )
624 wxArrayString output
, errors
;
625 int rc
= wxExecute(wxString::Format
627 _T("%s -F %s=@%s %s"),
629 m_inputField
.c_str(),
630 GetCompressedFileName().c_str(),
637 wxLogError(_("Failed to execute curl, please install it in PATH."));
641 const size_t count
= errors
.GetCount();
644 for ( size_t n
= 0; n
< count
; n
++ )
646 wxLogWarning(_T("%s"), errors
[n
].c_str());
650 wxLogError(_("Failed to upload the debug report (error code %d)."), rc
);
654 if ( OnServerReply(output
) )
661 #endif // wxUSE_ZIPSTREAM
663 #endif // wxUSE_DEBUGREPORT