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
58 // ----------------------------------------------------------------------------
59 // XmlStackWalker: stack walker specialization which dumps stack in XML
60 // ----------------------------------------------------------------------------
62 class XmlStackWalker
: public wxStackWalker
65 XmlStackWalker(wxXmlNode
*nodeStack
)
68 m_nodeStack
= nodeStack
;
71 bool IsOk() const { return m_isOk
; }
74 virtual void OnStackFrame(const wxStackFrame
& frame
);
76 wxXmlNode
*m_nodeStack
;
80 #endif // wxUSE_STACKWALKER
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
87 HexProperty(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
89 node
->AddProperty(name
, wxString::Format(_T("%08lx"), value
));
93 NumProperty(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
95 node
->AddProperty(name
, wxString::Format(_T("%lu"), value
));
99 TextElement(wxXmlNode
*node
, const wxChar
*name
, const wxString
& value
)
101 wxXmlNode
*nodeChild
= new wxXmlNode(wxXML_ELEMENT_NODE
, name
);
102 node
->AddChild(nodeChild
);
103 nodeChild
->AddChild(new wxXmlNode(wxXML_TEXT_NODE
, _T(""), value
));
107 HexElement(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
109 TextElement(node
, name
, wxString::Format(_T("%08lx"), value
));
112 #if wxUSE_STACKWALKER
114 // ============================================================================
115 // XmlStackWalker implementation
116 // ============================================================================
118 void XmlStackWalker::OnStackFrame(const wxStackFrame
& frame
)
122 wxXmlNode
*nodeFrame
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("frame"));
123 m_nodeStack
->AddChild(nodeFrame
);
125 NumProperty(nodeFrame
, _T("level"), frame
.GetLevel());
126 wxString func
= frame
.GetName();
129 nodeFrame
->AddProperty(_T("function"), func
);
130 HexProperty(nodeFrame
, _T("offset"), frame
.GetOffset());
133 if ( frame
.HasSourceLocation() )
135 nodeFrame
->AddProperty(_T("file"), frame
.GetFileName());
136 NumProperty(nodeFrame
, _T("line"), frame
.GetLine());
139 const size_t nParams
= frame
.GetParamCount();
142 wxXmlNode
*nodeParams
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("parameters"));
143 nodeFrame
->AddChild(nodeParams
);
145 for ( size_t n
= 0; n
< nParams
; n
++ )
148 nodeParam
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("parameter"));
149 nodeParams
->AddChild(nodeParam
);
151 NumProperty(nodeParam
, _T("number"), n
);
153 wxString type
, name
, value
;
154 if ( !frame
.GetParam(n
, &type
, &name
, &value
) )
158 TextElement(nodeParam
, _T("type"), type
);
161 TextElement(nodeParam
, _T("name"), name
);
163 if ( !value
.empty() )
164 TextElement(nodeParam
, _T("value"), value
);
169 #endif // wxUSE_STACKWALKER
171 // ============================================================================
172 // wxDebugReport implementation
173 // ============================================================================
175 // ----------------------------------------------------------------------------
176 // initialization and cleanup
177 // ----------------------------------------------------------------------------
179 wxDebugReport::wxDebugReport()
181 // get a temporary directory name
182 wxString appname
= GetReportName();
184 // we can't use CreateTempFileName() because it creates a file, not a
185 // directory, so do our best to create a unique name ourselves
187 // of course, this doesn't protect us against malicious users...
189 fn
.AssignTempFileName(appname
);
190 m_dir
.Printf(_T("%s%c%s_dbgrpt-%lu-%s"),
191 fn
.GetPath().c_str(), wxFILE_SEP_PATH
, appname
.c_str(),
193 wxDateTime::Now().Format(_T("%Y%m%dT%H%M%S")).c_str());
195 // as we are going to save the process state there use restrictive
197 if ( !wxMkdir(m_dir
, 0700) )
199 wxLogSysError(_("Failed to create directory \"%s\""), m_dir
.c_str());
200 wxLogError(_("Debug report couldn't be created."));
206 wxDebugReport::~wxDebugReport()
208 if ( !m_dir
.empty() )
210 // remove all files in this directory
213 for ( bool cont
= dir
.GetFirst(&file
); cont
; cont
= dir
.GetNext(&file
) )
215 if ( wxRemove(wxFileName(m_dir
, file
).GetFullPath()) != 0 )
217 wxLogSysError(_("Failed to remove debug report file \"%s\""),
225 if ( !m_dir
.empty() )
227 // Temp fix: what should this be? eVC++ doesn't like wxRmDir
229 if ( wxRmdir(m_dir
.fn_str()) != 0 )
231 if ( wxRmDir(m_dir
.fn_str()) != 0 )
234 wxLogSysError(_("Failed to clean up debug report directory \"%s\""),
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 wxString
wxDebugReport::GetReportName() const
247 return wxTheApp
->GetAppName();
252 void wxDebugReport::AddFile(const wxString
& name
, const wxString
& description
)
255 m_descriptions
.Add(description
);
258 void wxDebugReport::RemoveFile(const wxString
& name
)
260 const int n
= m_files
.Index(name
);
261 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("No such file in wxDebugReport") );
264 m_descriptions
.RemoveAt(n
);
266 wxRemove(wxFileName(GetDirectory(), name
).GetFullPath());
269 bool wxDebugReport::GetFile(size_t n
, wxString
*name
, wxString
*desc
) const
271 if ( n
>= m_files
.GetCount() )
277 *desc
= m_descriptions
[n
];
282 void wxDebugReport::AddAll(Context context
)
284 #if wxUSE_STACKWALKER
286 #endif // wxUSE_STACKWALKER
288 #if wxUSE_CRASHREPORT
290 #endif // wxUSE_CRASHREPORT
292 #if !wxUSE_STACKWALKER && !wxUSE_CRASHREPORT
293 wxUnusedVar(context
);
297 // ----------------------------------------------------------------------------
298 // adding basic text information about current context
299 // ----------------------------------------------------------------------------
301 #if wxUSE_STACKWALKER
303 bool wxDebugReport::DoAddSystemInfo(wxXmlNode
*nodeSystemInfo
)
305 nodeSystemInfo
->AddProperty(_T("description"), wxGetOsDescription());
310 bool wxDebugReport::DoAddLoadedModules(wxXmlNode
*nodeModules
)
312 wxDynamicLibraryDetailsArray
modules(wxDynamicLibrary::ListLoaded());
313 const size_t count
= modules
.GetCount();
317 for ( size_t n
= 0; n
< count
; n
++ )
319 const wxDynamicLibraryDetails
& info
= modules
[n
];
321 wxXmlNode
*nodeModule
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("module"));
322 nodeModules
->AddChild(nodeModule
);
324 wxString path
= info
.GetPath();
326 path
= info
.GetName();
328 nodeModule
->AddProperty(_T("path"), path
);
332 if ( info
.GetAddress(&addr
, &len
) )
334 HexProperty(nodeModule
, _T("address"), (unsigned long)addr
);
335 HexProperty(nodeModule
, _T("size"), len
);
338 wxString ver
= info
.GetVersion();
341 nodeModule
->AddProperty(_T("version"), ver
);
348 bool wxDebugReport::DoAddExceptionInfo(wxXmlNode
*nodeContext
)
350 #if wxUSE_CRASHREPORT
355 wxXmlNode
*nodeExc
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("exception"));
356 nodeContext
->AddChild(nodeExc
);
358 HexProperty(nodeExc
, _T("code"), c
.code
);
359 nodeExc
->AddProperty(_T("name"), c
.GetExceptionString());
360 HexProperty(nodeExc
, _T("address"), (unsigned long)c
.addr
);
363 wxXmlNode
*nodeRegs
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("registers"));
364 nodeContext
->AddChild(nodeRegs
);
365 HexElement(nodeRegs
, _T("eax"), c
.regs
.eax
);
366 HexElement(nodeRegs
, _T("ebx"), c
.regs
.ebx
);
367 HexElement(nodeRegs
, _T("ecx"), c
.regs
.edx
);
368 HexElement(nodeRegs
, _T("edx"), c
.regs
.edx
);
369 HexElement(nodeRegs
, _T("esi"), c
.regs
.esi
);
370 HexElement(nodeRegs
, _T("edi"), c
.regs
.edi
);
372 HexElement(nodeRegs
, _T("ebp"), c
.regs
.ebp
);
373 HexElement(nodeRegs
, _T("esp"), c
.regs
.esp
);
374 HexElement(nodeRegs
, _T("eip"), c
.regs
.eip
);
376 HexElement(nodeRegs
, _T("cs"), c
.regs
.cs
);
377 HexElement(nodeRegs
, _T("ds"), c
.regs
.ds
);
378 HexElement(nodeRegs
, _T("es"), c
.regs
.es
);
379 HexElement(nodeRegs
, _T("fs"), c
.regs
.fs
);
380 HexElement(nodeRegs
, _T("gs"), c
.regs
.gs
);
381 HexElement(nodeRegs
, _T("ss"), c
.regs
.ss
);
383 HexElement(nodeRegs
, _T("flags"), c
.regs
.flags
);
387 #else // !wxUSE_CRASHREPORT
388 wxUnusedVar(nodeContext
);
391 #endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT
394 bool wxDebugReport::AddContext(wxDebugReport::Context ctx
)
396 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
398 // create XML dump of current context
399 wxXmlDocument xmldoc
;
400 wxXmlNode
*nodeRoot
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("report"));
401 xmldoc
.SetRoot(nodeRoot
);
402 nodeRoot
->AddProperty(_T("version"), _T("1.0"));
403 nodeRoot
->AddProperty(_T("kind"), ctx
== Context_Curent
? _T("user")
406 // add system information
407 wxXmlNode
*nodeSystemInfo
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("system"));
408 if ( DoAddSystemInfo(nodeSystemInfo
) )
409 nodeRoot
->AddChild(nodeSystemInfo
);
411 delete nodeSystemInfo
;
413 // add information about the loaded modules
414 wxXmlNode
*nodeModules
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("modules"));
415 if ( DoAddLoadedModules(nodeModules
) )
416 nodeRoot
->AddChild(nodeModules
);
420 // add CPU context information: this only makes sense for exceptions as our
421 // current context is not very interesting otherwise
422 if ( ctx
== Context_Exception
)
424 wxXmlNode
*nodeContext
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("context"));
425 if ( DoAddExceptionInfo(nodeContext
) )
426 nodeRoot
->AddChild(nodeContext
);
431 // add stack traceback
432 #if wxUSE_STACKWALKER
433 wxXmlNode
*nodeStack
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("stack"));
434 XmlStackWalker
sw(nodeStack
);
435 if ( ctx
== Context_Exception
)
437 sw
.WalkFromException();
439 else // Context_Curent
445 nodeRoot
->AddChild(nodeStack
);
448 #endif // wxUSE_STACKWALKER
450 // finally let the user add any extra information he needs
451 DoAddCustomContext(nodeRoot
);
454 // save the entire context dump in a file
455 wxFileName
fn(m_dir
, GetReportName(), _T("xml"));
457 if ( !xmldoc
.Save(fn
.GetFullPath()) )
460 AddFile(fn
.GetFullName(), _("process context description"));
465 #endif // wxUSE_STACKWALKER
467 // ----------------------------------------------------------------------------
469 // ----------------------------------------------------------------------------
471 #if wxUSE_CRASHREPORT
473 bool wxDebugReport::AddDump(Context ctx
)
475 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
477 wxFileName
fn(m_dir
, GetReportName(), _T("dmp"));
478 wxCrashReport::SetFileName(fn
.GetFullPath());
480 if ( !(ctx
== Context_Exception
? wxCrashReport::Generate()
481 : wxCrashReport::GenerateNow()) )
484 AddFile(fn
.GetFullName(), _("dump of the process state (binary)"));
489 #endif // wxUSE_CRASHREPORT
491 // ----------------------------------------------------------------------------
493 // ----------------------------------------------------------------------------
495 bool wxDebugReport::Process()
497 if ( !GetFilesCount() )
499 wxLogError(_("Debug report generation has failed."));
506 wxLogError(_("Processing debug report has failed, leaving the files in \"%s\" directory."),
507 GetDirectory().c_str());
517 bool wxDebugReport::DoProcess()
519 wxString msg
= _("*** A debug report has been generated\n");
520 msg
+= wxString::Format(_("*** It can be found in \"%s\"\n"),
521 GetDirectory().c_str());
522 msg
+= _("*** And includes the following files:\n");
525 const size_t count
= GetFilesCount();
526 for ( size_t n
= 0; n
< count
; n
++ )
528 GetFile(n
, &name
, &desc
);
529 msg
+= wxString::Format(_("\t%s: %s\n"), name
.c_str(), desc
.c_str());
532 msg
+= _("\nPlease send this report to the program maintainer, thank you!\n");
534 wxLogMessage(_T("%s"), msg
.c_str());
536 // we have to do this or the report would be deleted, and we don't even
537 // have any way to ask the user if he wants to keep it from here
543 // ============================================================================
544 // wxDebugReport-derived classes
545 // ============================================================================
549 // ----------------------------------------------------------------------------
550 // wxDebugReportCompress
551 // ----------------------------------------------------------------------------
553 bool wxDebugReportCompress::DoProcess()
555 const size_t count
= GetFilesCount();
559 // create the streams
560 wxFileName
fn(GetDirectory(), GetReportName(), _T("zip"));
561 wxFFileOutputStream
os(fn
.GetFullPath(), _T("wb"));
562 wxZipOutputStream
zos(os
, 9);
564 // add all files to the ZIP one
566 for ( size_t n
= 0; n
< count
; n
++ )
568 GetFile(n
, &name
, &desc
);
570 wxZipEntry
*ze
= new wxZipEntry(name
);
571 ze
->SetComment(desc
);
573 if ( !zos
.PutNextEntry(ze
) )
576 wxFileName
filename(fn
.GetPath(), name
);
577 wxFFileInputStream
is(filename
.GetFullPath());
578 if ( !is
.IsOk() || !zos
.Write(is
).IsOk() )
585 m_zipfile
= fn
.GetFullPath();
590 // ----------------------------------------------------------------------------
591 // wxDebugReportUpload
592 // ----------------------------------------------------------------------------
594 wxDebugReportUpload::wxDebugReportUpload(const wxString
& url
,
595 const wxString
& input
,
596 const wxString
& action
,
597 const wxString
& curl
)
602 if ( m_uploadURL
.Last() != _T('/') )
603 m_uploadURL
+= _T('/');
604 m_uploadURL
+= action
;
607 bool wxDebugReportUpload::DoProcess()
609 if ( !wxDebugReportCompress::DoProcess() )
613 wxArrayString output
, errors
;
614 int rc
= wxExecute(wxString::Format
616 _T("%s -F %s=@%s %s"),
618 m_inputField
.c_str(),
619 GetCompressedFileName().c_str(),
626 wxLogError(_("Failed to execute curl, please install it in PATH."));
630 const size_t count
= errors
.GetCount();
633 for ( size_t n
= 0; n
< count
; n
++ )
635 wxLogWarning(_T("%s"), errors
[n
].c_str());
639 wxLogError(_("Failed to upload the debug report (error code %d)."), rc
);
643 if ( OnServerReply(output
) )
650 #endif // wxUSE_ZIPSTREAM
652 #endif // wxUSE_DEBUGREPORT