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"
34 #include "wx/debugrpt.h"
36 #include "wx/filename.h"
38 #include "wx/dynlib.h"
40 #include "wx/xml/xml.h"
43 #include "wx/stackwalk.h"
47 #include "wx/msw/crashrpt.h"
51 #include "wx/wfstream.h"
52 #include "wx/zipstrm.h"
53 #endif // wxUSE_ZIPSTREAM
57 // ----------------------------------------------------------------------------
58 // XmlStackWalker: stack walker specialization which dumps stack in XML
59 // ----------------------------------------------------------------------------
61 class XmlStackWalker
: public wxStackWalker
64 XmlStackWalker(wxXmlNode
*nodeStack
)
67 m_nodeStack
= nodeStack
;
70 bool IsOk() const { return m_isOk
; }
73 virtual void OnStackFrame(const wxStackFrame
& frame
);
75 wxXmlNode
*m_nodeStack
;
79 #endif // wxUSE_STACKWALKER
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
86 HexProperty(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
88 node
->AddProperty(name
, wxString::Format(_T("%08lx"), value
));
92 NumProperty(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
94 node
->AddProperty(name
, wxString::Format(_T("%lu"), value
));
98 TextElement(wxXmlNode
*node
, const wxChar
*name
, const wxString
& value
)
100 wxXmlNode
*nodeChild
= new wxXmlNode(wxXML_ELEMENT_NODE
, name
);
101 node
->AddChild(nodeChild
);
102 nodeChild
->AddChild(new wxXmlNode(wxXML_TEXT_NODE
, _T(""), value
));
106 HexElement(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
108 TextElement(node
, name
, wxString::Format(_T("%08lx"), value
));
111 #if wxUSE_STACKWALKER
113 // ============================================================================
114 // XmlStackWalker implementation
115 // ============================================================================
117 void XmlStackWalker::OnStackFrame(const wxStackFrame
& frame
)
121 wxXmlNode
*nodeFrame
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("frame"));
122 m_nodeStack
->AddChild(nodeFrame
);
124 NumProperty(nodeFrame
, _T("level"), frame
.GetLevel());
125 wxString func
= frame
.GetName();
128 nodeFrame
->AddProperty(_T("function"), func
);
129 HexProperty(nodeFrame
, _T("offset"), frame
.GetOffset());
132 if ( frame
.HasSourceLocation() )
134 nodeFrame
->AddProperty(_T("file"), frame
.GetFileName());
135 NumProperty(nodeFrame
, _T("line"), frame
.GetLine());
138 const size_t nParams
= frame
.GetParamCount();
141 wxXmlNode
*nodeParams
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("parameters"));
142 nodeFrame
->AddChild(nodeParams
);
144 for ( size_t n
= 0; n
< nParams
; n
++ )
147 nodeParam
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("parameter"));
148 nodeParams
->AddChild(nodeParam
);
150 NumProperty(nodeParam
, _T("number"), n
);
152 wxString type
, name
, value
;
153 if ( !frame
.GetParam(n
, &type
, &name
, &value
) )
157 TextElement(nodeParam
, _T("type"), type
);
160 TextElement(nodeParam
, _T("name"), name
);
162 if ( !value
.empty() )
163 TextElement(nodeParam
, _T("value"), value
);
168 #endif // wxUSE_STACKWALKER
170 // ============================================================================
171 // wxDebugReport implementation
172 // ============================================================================
174 // ----------------------------------------------------------------------------
175 // initialization and cleanup
176 // ----------------------------------------------------------------------------
178 wxDebugReport::wxDebugReport()
180 // get a temporary directory name
181 wxString appname
= GetReportName();
183 // we can't use CreateTempFileName() because it creates a file, not a
184 // directory, so do our best to create a unique name ourselves
186 // of course, this doesn't protect us against malicious users...
188 fn
.AssignTempFileName(appname
);
189 m_dir
.Printf(_T("%s%c%s_dbgrpt-%lu-%s"),
190 fn
.GetPath().c_str(), wxFILE_SEP_PATH
, appname
.c_str(),
192 wxDateTime::Now().Format(_T("%Y%m%dT%H%M%S")).c_str());
194 // as we are going to save the process state there use restrictive
196 if ( !wxMkdir(m_dir
, 0700) )
198 wxLogSysError(_("Failed to create directory \"%s\""), m_dir
.c_str());
199 wxLogError(_("Debug report couldn't be created."));
205 wxDebugReport::~wxDebugReport()
207 if ( !m_dir
.empty() )
209 // remove all files in this directory
212 for ( bool cont
= dir
.GetFirst(&file
); cont
; cont
= dir
.GetNext(&file
) )
214 if ( wxRemove(wxFileName(m_dir
, file
).GetFullPath()) != 0 )
216 wxLogSysError(_("Failed to remove debug report file \"%s\""),
224 if ( !m_dir
.empty() )
226 if ( wxRmDir(m_dir
.fn_str()) != 0 )
228 wxLogSysError(_("Failed to clean up debug report directory \"%s\""),
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
238 wxString
wxDebugReport::GetReportName() const
241 return wxTheApp
->GetAppName();
246 void wxDebugReport::AddFile(const wxString
& name
, const wxString
& description
)
249 m_descriptions
.Add(description
);
252 void wxDebugReport::RemoveFile(const wxString
& name
)
254 const int n
= m_files
.Index(name
);
255 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("No such file in wxDebugReport") );
258 m_descriptions
.RemoveAt(n
);
260 wxRemove(wxFileName(GetDirectory(), name
).GetFullPath());
263 bool wxDebugReport::GetFile(size_t n
, wxString
*name
, wxString
*desc
) const
265 if ( n
>= m_files
.GetCount() )
271 *desc
= m_descriptions
[n
];
276 void wxDebugReport::AddAll(Context context
)
278 #if wxUSE_STACKWALKER
280 #endif // wxUSE_STACKWALKER
282 #if wxUSE_CRASHREPORT
284 #endif // wxUSE_CRASHREPORT
286 #if !wxUSE_STACKWALKER && !wxUSE_CRASHREPORT
287 wxUnusedVar(context
);
291 // ----------------------------------------------------------------------------
292 // adding basic text information about current context
293 // ----------------------------------------------------------------------------
295 #if wxUSE_STACKWALKER
297 bool wxDebugReport::DoAddSystemInfo(wxXmlNode
*nodeSystemInfo
)
299 nodeSystemInfo
->AddProperty(_T("description"), wxGetOsDescription());
304 bool wxDebugReport::DoAddLoadedModules(wxXmlNode
*nodeModules
)
306 wxDynamicLibraryDetailsArray
modules(wxDynamicLibrary::ListLoaded());
307 const size_t count
= modules
.GetCount();
311 for ( size_t n
= 0; n
< count
; n
++ )
313 const wxDynamicLibraryDetails
& info
= modules
[n
];
315 wxXmlNode
*nodeModule
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("module"));
316 nodeModules
->AddChild(nodeModule
);
318 wxString path
= info
.GetPath();
320 path
= info
.GetName();
322 nodeModule
->AddProperty(_T("path"), path
);
326 if ( info
.GetAddress(&addr
, &len
) )
328 HexProperty(nodeModule
, _T("address"), (unsigned long)addr
);
329 HexProperty(nodeModule
, _T("size"), len
);
332 wxString ver
= info
.GetVersion();
335 nodeModule
->AddProperty(_T("version"), ver
);
342 bool wxDebugReport::DoAddExceptionInfo(wxXmlNode
*nodeContext
)
344 #if wxUSE_CRASHREPORT
349 wxXmlNode
*nodeExc
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("exception"));
350 nodeContext
->AddChild(nodeExc
);
352 HexProperty(nodeExc
, _T("code"), c
.code
);
353 nodeExc
->AddProperty(_T("name"), c
.GetExceptionString());
354 HexProperty(nodeExc
, _T("address"), (unsigned long)c
.addr
);
357 wxXmlNode
*nodeRegs
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("registers"));
358 nodeContext
->AddChild(nodeRegs
);
359 HexElement(nodeRegs
, _T("eax"), c
.regs
.eax
);
360 HexElement(nodeRegs
, _T("ebx"), c
.regs
.ebx
);
361 HexElement(nodeRegs
, _T("ecx"), c
.regs
.edx
);
362 HexElement(nodeRegs
, _T("edx"), c
.regs
.edx
);
363 HexElement(nodeRegs
, _T("esi"), c
.regs
.esi
);
364 HexElement(nodeRegs
, _T("edi"), c
.regs
.edi
);
366 HexElement(nodeRegs
, _T("ebp"), c
.regs
.ebp
);
367 HexElement(nodeRegs
, _T("esp"), c
.regs
.esp
);
368 HexElement(nodeRegs
, _T("eip"), c
.regs
.eip
);
370 HexElement(nodeRegs
, _T("cs"), c
.regs
.cs
);
371 HexElement(nodeRegs
, _T("ds"), c
.regs
.ds
);
372 HexElement(nodeRegs
, _T("es"), c
.regs
.es
);
373 HexElement(nodeRegs
, _T("fs"), c
.regs
.fs
);
374 HexElement(nodeRegs
, _T("gs"), c
.regs
.gs
);
375 HexElement(nodeRegs
, _T("ss"), c
.regs
.ss
);
377 HexElement(nodeRegs
, _T("flags"), c
.regs
.flags
);
381 #else // !wxUSE_CRASHREPORT
382 wxUnusedVar(nodeContext
);
385 #endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT
388 bool wxDebugReport::AddContext(wxDebugReport::Context ctx
)
390 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
392 // create XML dump of current context
393 wxXmlDocument xmldoc
;
394 wxXmlNode
*nodeRoot
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("report"));
395 xmldoc
.SetRoot(nodeRoot
);
396 nodeRoot
->AddProperty(_T("version"), _T("1.0"));
397 nodeRoot
->AddProperty(_T("kind"), ctx
== Context_Curent
? _T("user")
400 // add system information
401 wxXmlNode
*nodeSystemInfo
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("system"));
402 if ( DoAddSystemInfo(nodeSystemInfo
) )
403 nodeRoot
->AddChild(nodeSystemInfo
);
405 delete nodeSystemInfo
;
407 // add information about the loaded modules
408 wxXmlNode
*nodeModules
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("modules"));
409 if ( DoAddLoadedModules(nodeModules
) )
410 nodeRoot
->AddChild(nodeModules
);
414 // add CPU context information: this only makes sense for exceptions as our
415 // current context is not very interesting otherwise
416 if ( ctx
== Context_Exception
)
418 wxXmlNode
*nodeContext
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("context"));
419 if ( DoAddExceptionInfo(nodeContext
) )
420 nodeRoot
->AddChild(nodeContext
);
425 // add stack traceback
426 #if wxUSE_STACKWALKER
427 wxXmlNode
*nodeStack
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("stack"));
428 XmlStackWalker
sw(nodeStack
);
429 if ( ctx
== Context_Exception
)
431 sw
.WalkFromException();
433 else // Context_Curent
439 nodeRoot
->AddChild(nodeStack
);
442 #endif // wxUSE_STACKWALKER
444 // finally let the user add any extra information he needs
445 DoAddCustomContext(nodeRoot
);
448 // save the entire context dump in a file
449 wxFileName
fn(m_dir
, GetReportName(), _T("xml"));
451 if ( !xmldoc
.Save(fn
.GetFullPath()) )
454 AddFile(fn
.GetFullName(), _("process context description"));
459 #endif // wxUSE_STACKWALKER
461 // ----------------------------------------------------------------------------
463 // ----------------------------------------------------------------------------
465 #if wxUSE_CRASHREPORT
467 bool wxDebugReport::AddDump(Context ctx
)
469 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
471 wxFileName
fn(m_dir
, GetReportName(), _T("dmp"));
472 wxCrashReport::SetFileName(fn
.GetFullPath());
474 if ( !(ctx
== Context_Exception
? wxCrashReport::Generate()
475 : wxCrashReport::GenerateNow()) )
478 AddFile(fn
.GetFullName(), _("dump of the process state (binary)"));
483 #endif // wxUSE_CRASHREPORT
485 // ----------------------------------------------------------------------------
487 // ----------------------------------------------------------------------------
489 bool wxDebugReport::Process()
491 if ( !GetFilesCount() )
493 wxLogError(_("Debug report generation has failed."));
500 wxLogError(_("Processing debug report has failed, leaving the files in \"%s\" directory."),
501 GetDirectory().c_str());
511 bool wxDebugReport::DoProcess()
513 wxString msg
= _("*** A debug report has been generated\n");
514 msg
+= wxString::Format(_("*** It can be found in \"%s\"\n"),
515 GetDirectory().c_str());
516 msg
+= _("*** And includes the following files:\n");
519 const size_t count
= GetFilesCount();
520 for ( size_t n
= 0; n
< count
; n
++ )
522 GetFile(n
, &name
, &desc
);
523 msg
+= wxString::Format(_("\t%s: %s\n"), name
.c_str(), desc
.c_str());
526 msg
+= _("\nPlease send this report to the program maintainer, thank you!\n");
528 wxLogMessage(_T("%s"), msg
.c_str());
530 // we have to do this or the report would be deleted, and we don't even
531 // have any way to ask the user if he wants to keep it from here
537 // ============================================================================
538 // wxDebugReport-derived classes
539 // ============================================================================
543 // ----------------------------------------------------------------------------
544 // wxDebugReportCompress
545 // ----------------------------------------------------------------------------
547 bool wxDebugReportCompress::DoProcess()
549 const size_t count
= GetFilesCount();
553 // create the streams
554 wxFileName
fn(GetDirectory(), GetReportName(), _T("zip"));
555 wxFFileOutputStream
os(fn
.GetFullPath(), _T("wb"));
556 wxZipOutputStream
zos(os
, 9);
558 // add all files to the ZIP one
560 for ( size_t n
= 0; n
< count
; n
++ )
562 GetFile(n
, &name
, &desc
);
564 wxZipEntry
*ze
= new wxZipEntry(name
);
565 ze
->SetComment(desc
);
567 if ( !zos
.PutNextEntry(ze
) )
570 wxFileName
filename(fn
.GetPath(), name
);
571 wxFFileInputStream
is(filename
.GetFullPath());
572 if ( !is
.IsOk() || !zos
.Write(is
).IsOk() )
579 m_zipfile
= fn
.GetFullPath();
584 // ----------------------------------------------------------------------------
585 // wxDebugReportUpload
586 // ----------------------------------------------------------------------------
588 wxDebugReportUpload::wxDebugReportUpload(const wxString
& url
,
589 const wxString
& input
,
590 const wxString
& action
,
591 const wxString
& curl
)
596 if ( m_uploadURL
.Last() != _T('/') )
597 m_uploadURL
+= _T('/');
598 m_uploadURL
+= action
;
601 bool wxDebugReportUpload::DoProcess()
603 if ( !wxDebugReportCompress::DoProcess() )
607 wxArrayString output
, errors
;
608 int rc
= wxExecute(wxString::Format
610 _T("%s -F %s=@%s %s"),
612 m_inputField
.c_str(),
613 GetCompressedFileName().c_str(),
620 wxLogError(_("Failed to execute curl, please install it in PATH."));
624 const size_t count
= errors
.GetCount();
627 for ( size_t n
= 0; n
< count
; n
++ )
629 wxLogWarning(_T("%s"), errors
[n
].c_str());
633 wxLogError(_("Failed to upload the debug report (error code %d)."), rc
);
637 if ( OnServerReply(output
) )
644 #endif // wxUSE_ZIPSTREAM
646 #endif // wxUSE_DEBUGREPORT