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"
33 #if wxUSE_DEBUGREPORT && wxUSE_XML
35 #include "wx/debugrpt.h"
38 #include "wx/filename.h"
40 #include "wx/dynlib.h"
42 #include "wx/xml/xml.h"
45 #include "wx/stackwalk.h"
49 #include "wx/msw/crashrpt.h"
53 #include "wx/wfstream.h"
54 #include "wx/zipstrm.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 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
88 HexProperty(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
90 node
->AddProperty(name
, wxString::Format(_T("%08lx"), value
));
94 NumProperty(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
96 node
->AddProperty(name
, wxString::Format(_T("%lu"), value
));
100 TextElement(wxXmlNode
*node
, const wxChar
*name
, const wxString
& value
)
102 wxXmlNode
*nodeChild
= new wxXmlNode(wxXML_ELEMENT_NODE
, name
);
103 node
->AddChild(nodeChild
);
104 nodeChild
->AddChild(new wxXmlNode(wxXML_TEXT_NODE
, wxEmptyString
, value
));
107 #if wxUSE_CRASHREPORT && defined(__INTEL__)
110 HexElement(wxXmlNode
*node
, const wxChar
*name
, unsigned long value
)
112 TextElement(node
, name
, wxString::Format(_T("%08lx"), value
));
115 #endif // wxUSE_CRASHREPORT
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
);
194 m_dir
.Printf(_T("%s%c%s_dbgrpt-%lu-%s"),
195 fn
.GetPath().c_str(), wxFILE_SEP_PATH
, appname
.c_str(),
197 wxDateTime::Now().Format(_T("%Y%m%dT%H%M%S")).c_str());
199 m_dir
.Printf(_T("%s%c%s_dbgrpt-%lu"),
200 fn
.GetPath().c_str(), wxFILE_SEP_PATH
, appname
.c_str(),
204 // as we are going to save the process state there use restrictive
206 if ( !wxMkdir(m_dir
, 0700) )
208 wxLogSysError(_("Failed to create directory \"%s\""), m_dir
.c_str());
209 wxLogError(_("Debug report couldn't be created."));
215 wxDebugReport::~wxDebugReport()
217 if ( !m_dir
.empty() )
219 // remove all files in this directory
222 for ( bool cont
= dir
.GetFirst(&file
); cont
; cont
= dir
.GetNext(&file
) )
224 if ( wxRemove(wxFileName(m_dir
, file
).GetFullPath()) != 0 )
226 wxLogSysError(_("Failed to remove debug report file \"%s\""),
234 if ( !m_dir
.empty() )
236 // Temp fix: what should this be? eVC++ doesn't like wxRmDir
238 if ( wxRmdir(m_dir
.fn_str()) != 0 )
240 if ( wxRmDir(m_dir
.fn_str()) != 0 )
243 wxLogSysError(_("Failed to clean up debug report directory \"%s\""),
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 wxString
wxDebugReport::GetReportName() const
256 return wxTheApp
->GetAppName();
262 wxDebugReport::AddFile(const wxString
& filename
, const wxString
& description
)
265 wxFileName
fn(filename
);
266 if ( fn
.IsAbsolute() )
268 // we need to copy the file to the debug report directory: give it the
270 name
= fn
.GetFullName();
271 wxCopyFile(fn
.GetFullPath(),
272 wxFileName(GetDirectory(), name
).GetFullPath());
274 else // file relative to the report directory
278 wxASSERT_MSG( wxFileName(GetDirectory(), name
).FileExists(),
279 _T("file should exist in debug report directory") );
283 m_descriptions
.Add(description
);
287 wxDebugReport::AddText(const wxString
& filename
,
288 const wxString
& text
,
289 const wxString
& description
)
291 wxASSERT_MSG( !wxFileName(filename
).IsAbsolute(),
292 _T("filename should be relative to debug report directory") );
294 wxFileName
fn(GetDirectory(), filename
);
295 wxFFile
file(fn
.GetFullPath(), _T("w"));
296 if ( !file
.IsOpened() || !file
.Write(text
) )
299 AddFile(filename
, description
);
304 void wxDebugReport::RemoveFile(const wxString
& name
)
306 const int n
= m_files
.Index(name
);
307 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("No such file in wxDebugReport") );
310 m_descriptions
.RemoveAt(n
);
312 wxRemove(wxFileName(GetDirectory(), name
).GetFullPath());
315 bool wxDebugReport::GetFile(size_t n
, wxString
*name
, wxString
*desc
) const
317 if ( n
>= m_files
.GetCount() )
323 *desc
= m_descriptions
[n
];
328 void wxDebugReport::AddAll(Context context
)
330 #if wxUSE_STACKWALKER
332 #endif // wxUSE_STACKWALKER
334 #if wxUSE_CRASHREPORT
336 #endif // wxUSE_CRASHREPORT
338 #if !wxUSE_STACKWALKER && !wxUSE_CRASHREPORT
339 wxUnusedVar(context
);
343 // ----------------------------------------------------------------------------
344 // adding basic text information about current context
345 // ----------------------------------------------------------------------------
347 #if wxUSE_STACKWALKER
349 bool wxDebugReport::DoAddSystemInfo(wxXmlNode
*nodeSystemInfo
)
351 nodeSystemInfo
->AddProperty(_T("description"), wxGetOsDescription());
356 bool wxDebugReport::DoAddLoadedModules(wxXmlNode
*nodeModules
)
358 wxDynamicLibraryDetailsArray
modules(wxDynamicLibrary::ListLoaded());
359 const size_t count
= modules
.GetCount();
363 for ( size_t n
= 0; n
< count
; n
++ )
365 const wxDynamicLibraryDetails
& info
= modules
[n
];
367 wxXmlNode
*nodeModule
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("module"));
368 nodeModules
->AddChild(nodeModule
);
370 wxString path
= info
.GetPath();
372 path
= info
.GetName();
374 nodeModule
->AddProperty(_T("path"), path
);
378 if ( info
.GetAddress(&addr
, &len
) )
380 HexProperty(nodeModule
, _T("address"), wxPtrToUInt(addr
));
381 HexProperty(nodeModule
, _T("size"), len
);
384 wxString ver
= info
.GetVersion();
387 nodeModule
->AddProperty(_T("version"), ver
);
394 bool wxDebugReport::DoAddExceptionInfo(wxXmlNode
*nodeContext
)
396 #if wxUSE_CRASHREPORT
401 wxXmlNode
*nodeExc
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("exception"));
402 nodeContext
->AddChild(nodeExc
);
404 HexProperty(nodeExc
, _T("code"), c
.code
);
405 nodeExc
->AddProperty(_T("name"), c
.GetExceptionString());
406 HexProperty(nodeExc
, _T("address"), wxPtrToUInt(c
.addr
));
409 wxXmlNode
*nodeRegs
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("registers"));
410 nodeContext
->AddChild(nodeRegs
);
411 HexElement(nodeRegs
, _T("eax"), c
.regs
.eax
);
412 HexElement(nodeRegs
, _T("ebx"), c
.regs
.ebx
);
413 HexElement(nodeRegs
, _T("ecx"), c
.regs
.edx
);
414 HexElement(nodeRegs
, _T("edx"), c
.regs
.edx
);
415 HexElement(nodeRegs
, _T("esi"), c
.regs
.esi
);
416 HexElement(nodeRegs
, _T("edi"), c
.regs
.edi
);
418 HexElement(nodeRegs
, _T("ebp"), c
.regs
.ebp
);
419 HexElement(nodeRegs
, _T("esp"), c
.regs
.esp
);
420 HexElement(nodeRegs
, _T("eip"), c
.regs
.eip
);
422 HexElement(nodeRegs
, _T("cs"), c
.regs
.cs
);
423 HexElement(nodeRegs
, _T("ds"), c
.regs
.ds
);
424 HexElement(nodeRegs
, _T("es"), c
.regs
.es
);
425 HexElement(nodeRegs
, _T("fs"), c
.regs
.fs
);
426 HexElement(nodeRegs
, _T("gs"), c
.regs
.gs
);
427 HexElement(nodeRegs
, _T("ss"), c
.regs
.ss
);
429 HexElement(nodeRegs
, _T("flags"), c
.regs
.flags
);
433 #else // !wxUSE_CRASHREPORT
434 wxUnusedVar(nodeContext
);
437 #endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT
440 bool wxDebugReport::AddContext(wxDebugReport::Context ctx
)
442 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
444 // create XML dump of current context
445 wxXmlDocument xmldoc
;
446 wxXmlNode
*nodeRoot
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("report"));
447 xmldoc
.SetRoot(nodeRoot
);
448 nodeRoot
->AddProperty(_T("version"), _T("1.0"));
449 nodeRoot
->AddProperty(_T("kind"), ctx
== Context_Current
? _T("user")
452 // add system information
453 wxXmlNode
*nodeSystemInfo
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("system"));
454 if ( DoAddSystemInfo(nodeSystemInfo
) )
455 nodeRoot
->AddChild(nodeSystemInfo
);
457 delete nodeSystemInfo
;
459 // add information about the loaded modules
460 wxXmlNode
*nodeModules
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("modules"));
461 if ( DoAddLoadedModules(nodeModules
) )
462 nodeRoot
->AddChild(nodeModules
);
466 // add CPU context information: this only makes sense for exceptions as our
467 // current context is not very interesting otherwise
468 if ( ctx
== Context_Exception
)
470 wxXmlNode
*nodeContext
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("context"));
471 if ( DoAddExceptionInfo(nodeContext
) )
472 nodeRoot
->AddChild(nodeContext
);
477 // add stack traceback
478 #if wxUSE_STACKWALKER
479 wxXmlNode
*nodeStack
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("stack"));
480 XmlStackWalker
sw(nodeStack
);
481 if ( ctx
== Context_Exception
)
483 sw
.WalkFromException();
485 else // Context_Current
491 nodeRoot
->AddChild(nodeStack
);
494 #endif // wxUSE_STACKWALKER
496 // finally let the user add any extra information he needs
497 DoAddCustomContext(nodeRoot
);
500 // save the entire context dump in a file
501 wxFileName
fn(m_dir
, GetReportName(), _T("xml"));
503 if ( !xmldoc
.Save(fn
.GetFullPath()) )
506 AddFile(fn
.GetFullName(), _("process context description"));
511 #endif // wxUSE_STACKWALKER
513 // ----------------------------------------------------------------------------
515 // ----------------------------------------------------------------------------
517 #if wxUSE_CRASHREPORT
519 bool wxDebugReport::AddDump(Context ctx
)
521 wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
523 wxFileName
fn(m_dir
, GetReportName(), _T("dmp"));
524 wxCrashReport::SetFileName(fn
.GetFullPath());
526 if ( !(ctx
== Context_Exception
? wxCrashReport::Generate()
527 : wxCrashReport::GenerateNow()) )
530 AddFile(fn
.GetFullName(), _("dump of the process state (binary)"));
535 #endif // wxUSE_CRASHREPORT
537 // ----------------------------------------------------------------------------
539 // ----------------------------------------------------------------------------
541 bool wxDebugReport::Process()
543 if ( !GetFilesCount() )
545 wxLogError(_("Debug report generation has failed."));
552 wxLogError(_("Processing debug report has failed, leaving the files in \"%s\" directory."),
553 GetDirectory().c_str());
563 bool wxDebugReport::DoProcess()
565 wxString msg
= _("*** A debug report has been generated\n");
566 msg
+= wxString::Format(_("*** It can be found in \"%s\"\n"),
567 GetDirectory().c_str());
568 msg
+= _("*** And includes the following files:\n");
571 const size_t count
= GetFilesCount();
572 for ( size_t n
= 0; n
< count
; n
++ )
574 GetFile(n
, &name
, &desc
);
575 msg
+= wxString::Format(_("\t%s: %s\n"), name
.c_str(), desc
.c_str());
578 msg
+= _("\nPlease send this report to the program maintainer, thank you!\n");
580 wxLogMessage(_T("%s"), msg
.c_str());
582 // we have to do this or the report would be deleted, and we don't even
583 // have any way to ask the user if he wants to keep it from here
589 // ============================================================================
590 // wxDebugReport-derived classes
591 // ============================================================================
595 // ----------------------------------------------------------------------------
596 // wxDebugReportCompress
597 // ----------------------------------------------------------------------------
599 bool wxDebugReportCompress::DoProcess()
601 const size_t count
= GetFilesCount();
605 // create the streams
606 wxFileName
fn(GetDirectory(), GetReportName(), _T("zip"));
607 wxFFileOutputStream
os(fn
.GetFullPath(), _T("wb"));
608 wxZipOutputStream
zos(os
, 9);
610 // add all files to the ZIP one
612 for ( size_t n
= 0; n
< count
; n
++ )
614 GetFile(n
, &name
, &desc
);
616 wxZipEntry
*ze
= new wxZipEntry(name
);
617 ze
->SetComment(desc
);
619 if ( !zos
.PutNextEntry(ze
) )
622 wxFileName
filename(fn
.GetPath(), name
);
623 wxFFileInputStream
is(filename
.GetFullPath());
624 if ( !is
.IsOk() || !zos
.Write(is
).IsOk() )
631 m_zipfile
= fn
.GetFullPath();
636 // ----------------------------------------------------------------------------
637 // wxDebugReportUpload
638 // ----------------------------------------------------------------------------
640 wxDebugReportUpload::wxDebugReportUpload(const wxString
& url
,
641 const wxString
& input
,
642 const wxString
& action
,
643 const wxString
& curl
)
648 if ( m_uploadURL
.Last() != _T('/') )
649 m_uploadURL
+= _T('/');
650 m_uploadURL
+= action
;
653 bool wxDebugReportUpload::DoProcess()
655 if ( !wxDebugReportCompress::DoProcess() )
659 wxArrayString output
, errors
;
660 int rc
= wxExecute(wxString::Format
662 _T("%s -F %s=@\"%s\" %s"),
664 m_inputField
.c_str(),
665 GetCompressedFileName().c_str(),
672 wxLogError(_("Failed to execute curl, please install it in PATH."));
676 const size_t count
= errors
.GetCount();
679 for ( size_t n
= 0; n
< count
; n
++ )
681 wxLogWarning(_T("%s"), errors
[n
].c_str());
685 wxLogError(_("Failed to upload the debug report (error code %d)."), rc
);
689 if ( OnServerReply(output
) )
696 #endif // wxUSE_ZIPSTREAM
698 #endif // wxUSE_DEBUGREPORT