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 if ( wxRmDir(m_dir
.fn_str()) != 0 )
229 wxLogSysError(_("Failed to clean up debug report directory \"%s\""),
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 wxString
wxDebugReport::GetReportName() const
242 return wxTheApp
->GetAppName();
247 void wxDebugReport::AddFile(const wxString
& name
, const wxString
& description
)
250 m_descriptions
.Add(description
);
253 void wxDebugReport::RemoveFile(const wxString
& name
)
255 const int n
= m_files
.Index(name
);
256 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("No such file in wxDebugReport") );
259 m_descriptions
.RemoveAt(n
);
261 wxRemove(wxFileName(GetDirectory(), name
).GetFullPath());
264 bool wxDebugReport::GetFile(size_t n
, wxString
*name
, wxString
*desc
) const
266 if ( n
>= m_files
.GetCount() )
272 *desc
= m_descriptions
[n
];
277 void wxDebugReport::AddAll(Context context
)
279 #if wxUSE_STACKWALKER
281 #endif // wxUSE_STACKWALKER
283 #if wxUSE_CRASHREPORT
285 #endif // wxUSE_CRASHREPORT
287 #if !wxUSE_STACKWALKER && !wxUSE_CRASHREPORT
288 wxUnusedVar(context
);
292 // ----------------------------------------------------------------------------
293 // adding basic text information about current context
294 // ----------------------------------------------------------------------------
296 #if wxUSE_STACKWALKER
298 bool wxDebugReport::DoAddSystemInfo(wxXmlNode
*nodeSystemInfo
)
300 nodeSystemInfo
->AddProperty(_T("description"), wxGetOsDescription());
305 bool wxDebugReport::DoAddLoadedModules(wxXmlNode
*nodeModules
)
307 wxDynamicLibraryDetailsArray
modules(wxDynamicLibrary::ListLoaded());
308 const size_t count
= modules
.GetCount();
312 for ( size_t n
= 0; n
< count
; n
++ )
314 const wxDynamicLibraryDetails
& info
= modules
[n
];
316 wxXmlNode
*nodeModule
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("module"));
317 nodeModules
->AddChild(nodeModule
);
319 wxString path
= info
.GetPath();
321 path
= info
.GetName();
323 nodeModule
->AddProperty(_T("path"), path
);
327 if ( info
.GetAddress(&addr
, &len
) )
329 HexProperty(nodeModule
, _T("address"), (unsigned long)addr
);
330 HexProperty(nodeModule
, _T("size"), len
);
333 wxString ver
= info
.GetVersion();
336 nodeModule
->AddProperty(_T("version"), ver
);
343 bool wxDebugReport::DoAddExceptionInfo(wxXmlNode
*nodeContext
)
345 #if wxUSE_CRASHREPORT
350 wxXmlNode
*nodeExc
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("exception"));
351 nodeContext
->AddChild(nodeExc
);
353 HexProperty(nodeExc
, _T("code"), c
.code
);
354 nodeExc
->AddProperty(_T("name"), c
.GetExceptionString());
355 HexProperty(nodeExc
, _T("address"), (unsigned long)c
.addr
);
358 wxXmlNode
*nodeRegs
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("registers"));
359 nodeContext
->AddChild(nodeRegs
);
360 HexElement(nodeRegs
, _T("eax"), c
.regs
.eax
);
361 HexElement(nodeRegs
, _T("ebx"), c
.regs
.ebx
);
362 HexElement(nodeRegs
, _T("ecx"), c
.regs
.edx
);
363 HexElement(nodeRegs
, _T("edx"), c
.regs
.edx
);
364 HexElement(nodeRegs
, _T("esi"), c
.regs
.esi
);
365 HexElement(nodeRegs
, _T("edi"), c
.regs
.edi
);
367 HexElement(nodeRegs
, _T("ebp"), c
.regs
.ebp
);
368 HexElement(nodeRegs
, _T("esp"), c
.regs
.esp
);
369 HexElement(nodeRegs
, _T("eip"), c
.regs
.eip
);
371 HexElement(nodeRegs
, _T("cs"), c
.regs
.cs
);
372 HexElement(nodeRegs
, _T("ds"), c
.regs
.ds
);
373 HexElement(nodeRegs
, _T("es"), c
.regs
.es
);
374 HexElement(nodeRegs
, _T("fs"), c
.regs
.fs
);
375 HexElement(nodeRegs
, _T("gs"), c
.regs
.gs
);
376 HexElement(nodeRegs
, _T("ss"), c
.regs
.ss
);
378 HexElement(nodeRegs
, _T("flags"), c
.regs
.flags
);
382 #else // !wxUSE_CRASHREPORT
383 wxUnusedVar(nodeContext
);
386 #endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT
389 bool wxDebugReport::AddContext(wxDebugReport::Context ctx
)
391 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
393 // create XML dump of current context
394 wxXmlDocument xmldoc
;
395 wxXmlNode
*nodeRoot
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("report"));
396 xmldoc
.SetRoot(nodeRoot
);
397 nodeRoot
->AddProperty(_T("version"), _T("1.0"));
398 nodeRoot
->AddProperty(_T("kind"), ctx
== Context_Curent
? _T("user")
401 // add system information
402 wxXmlNode
*nodeSystemInfo
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("system"));
403 if ( DoAddSystemInfo(nodeSystemInfo
) )
404 nodeRoot
->AddChild(nodeSystemInfo
);
406 delete nodeSystemInfo
;
408 // add information about the loaded modules
409 wxXmlNode
*nodeModules
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("modules"));
410 if ( DoAddLoadedModules(nodeModules
) )
411 nodeRoot
->AddChild(nodeModules
);
415 // add CPU context information: this only makes sense for exceptions as our
416 // current context is not very interesting otherwise
417 if ( ctx
== Context_Exception
)
419 wxXmlNode
*nodeContext
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("context"));
420 if ( DoAddExceptionInfo(nodeContext
) )
421 nodeRoot
->AddChild(nodeContext
);
426 // add stack traceback
427 #if wxUSE_STACKWALKER
428 wxXmlNode
*nodeStack
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("stack"));
429 XmlStackWalker
sw(nodeStack
);
430 if ( ctx
== Context_Exception
)
432 sw
.WalkFromException();
434 else // Context_Curent
440 nodeRoot
->AddChild(nodeStack
);
443 #endif // wxUSE_STACKWALKER
445 // finally let the user add any extra information he needs
446 DoAddCustomContext(nodeRoot
);
449 // save the entire context dump in a file
450 wxFileName
fn(m_dir
, GetReportName(), _T("xml"));
452 if ( !xmldoc
.Save(fn
.GetFullPath()) )
455 AddFile(fn
.GetFullName(), _("process context description"));
460 #endif // wxUSE_STACKWALKER
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
466 #if wxUSE_CRASHREPORT
468 bool wxDebugReport::AddDump(Context ctx
)
470 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
472 wxFileName
fn(m_dir
, GetReportName(), _T("dmp"));
473 wxCrashReport::SetFileName(fn
.GetFullPath());
475 if ( !(ctx
== Context_Exception
? wxCrashReport::Generate()
476 : wxCrashReport::GenerateNow()) )
479 AddFile(fn
.GetFullName(), _("dump of the process state (binary)"));
484 #endif // wxUSE_CRASHREPORT
486 // ----------------------------------------------------------------------------
488 // ----------------------------------------------------------------------------
490 bool wxDebugReport::Process()
492 if ( !GetFilesCount() )
494 wxLogError(_("Debug report generation has failed."));
501 wxLogError(_("Processing debug report has failed, leaving the files in \"%s\" directory."),
502 GetDirectory().c_str());
512 bool wxDebugReport::DoProcess()
514 wxString msg
= _("*** A debug report has been generated\n");
515 msg
+= wxString::Format(_("*** It can be found in \"%s\"\n"),
516 GetDirectory().c_str());
517 msg
+= _("*** And includes the following files:\n");
520 const size_t count
= GetFilesCount();
521 for ( size_t n
= 0; n
< count
; n
++ )
523 GetFile(n
, &name
, &desc
);
524 msg
+= wxString::Format(_("\t%s: %s\n"), name
.c_str(), desc
.c_str());
527 msg
+= _("\nPlease send this report to the program maintainer, thank you!\n");
529 wxLogMessage(_T("%s"), msg
.c_str());
531 // we have to do this or the report would be deleted, and we don't even
532 // have any way to ask the user if he wants to keep it from here
538 // ============================================================================
539 // wxDebugReport-derived classes
540 // ============================================================================
544 // ----------------------------------------------------------------------------
545 // wxDebugReportCompress
546 // ----------------------------------------------------------------------------
548 bool wxDebugReportCompress::DoProcess()
550 const size_t count
= GetFilesCount();
554 // create the streams
555 wxFileName
fn(GetDirectory(), GetReportName(), _T("zip"));
556 wxFFileOutputStream
os(fn
.GetFullPath(), _T("wb"));
557 wxZipOutputStream
zos(os
, 9);
559 // add all files to the ZIP one
561 for ( size_t n
= 0; n
< count
; n
++ )
563 GetFile(n
, &name
, &desc
);
565 wxZipEntry
*ze
= new wxZipEntry(name
);
566 ze
->SetComment(desc
);
568 if ( !zos
.PutNextEntry(ze
) )
571 wxFileName
filename(fn
.GetPath(), name
);
572 wxFFileInputStream
is(filename
.GetFullPath());
573 if ( !is
.IsOk() || !zos
.Write(is
).IsOk() )
580 m_zipfile
= fn
.GetFullPath();
585 // ----------------------------------------------------------------------------
586 // wxDebugReportUpload
587 // ----------------------------------------------------------------------------
589 wxDebugReportUpload::wxDebugReportUpload(const wxString
& url
,
590 const wxString
& input
,
591 const wxString
& action
,
592 const wxString
& curl
)
597 if ( m_uploadURL
.Last() != _T('/') )
598 m_uploadURL
+= _T('/');
599 m_uploadURL
+= action
;
602 bool wxDebugReportUpload::DoProcess()
604 if ( !wxDebugReportCompress::DoProcess() )
608 wxArrayString output
, errors
;
609 int rc
= wxExecute(wxString::Format
611 _T("%s -F %s=@%s %s"),
613 m_inputField
.c_str(),
614 GetCompressedFileName().c_str(),
621 wxLogError(_("Failed to execute curl, please install it in PATH."));
625 const size_t count
= errors
.GetCount();
628 for ( size_t n
= 0; n
< count
; n
++ )
630 wxLogWarning(_T("%s"), errors
[n
].c_str());
634 wxLogError(_("Failed to upload the debug report (error code %d)."), rc
);
638 if ( OnServerReply(output
) )
645 #endif // wxUSE_ZIPSTREAM
647 #endif // wxUSE_DEBUGREPORT