debugrpt.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 693 行 · 第 1/2 页
CPP
693 行
///////////////////////////////////////////////////////////////////////////////
// Name: src/common/debugrpt.cpp
// Purpose: wxDebugReport and related classes implementation
// Author: Vadim Zeitlin
// Modified by:
// Created: 2005-01-17
// RCS-ID: $Id: debugrpt.cpp,v 1.19 2005/07/21 16:19:39 ABX Exp $
// Copyright: (c) 2005 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
// License: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/log.h"
#include "wx/intl.h"
#include "wx/utils.h"
#endif // WX_PRECOMP
#if wxUSE_DEBUGREPORT && wxUSE_XML
#include "wx/debugrpt.h"
#include "wx/ffile.h"
#include "wx/filename.h"
#include "wx/dir.h"
#include "wx/dynlib.h"
#include "wx/xml/xml.h"
#if wxUSE_STACKWALKER
#include "wx/stackwalk.h"
#endif
#if wxUSE_CRASHREPORT
#include "wx/msw/crashrpt.h"
#endif
#if wxUSE_ZIPSTREAM
#include "wx/wfstream.h"
#include "wx/zipstrm.h"
#endif // wxUSE_ZIPSTREAM
WX_CHECK_BUILD_OPTIONS("wxQA")
// ----------------------------------------------------------------------------
// XmlStackWalker: stack walker specialization which dumps stack in XML
// ----------------------------------------------------------------------------
#if wxUSE_STACKWALKER
class XmlStackWalker : public wxStackWalker
{
public:
XmlStackWalker(wxXmlNode *nodeStack)
{
m_isOk = false;
m_nodeStack = nodeStack;
}
bool IsOk() const { return m_isOk; }
protected:
virtual void OnStackFrame(const wxStackFrame& frame);
wxXmlNode *m_nodeStack;
bool m_isOk;
};
#endif // wxUSE_STACKWALKER
// ----------------------------------------------------------------------------
// local functions
// ----------------------------------------------------------------------------
static inline void
HexProperty(wxXmlNode *node, const wxChar *name, unsigned long value)
{
node->AddProperty(name, wxString::Format(_T("%08lx"), value));
}
static inline void
NumProperty(wxXmlNode *node, const wxChar *name, unsigned long value)
{
node->AddProperty(name, wxString::Format(_T("%lu"), value));
}
static inline void
TextElement(wxXmlNode *node, const wxChar *name, const wxString& value)
{
wxXmlNode *nodeChild = new wxXmlNode(wxXML_ELEMENT_NODE, name);
node->AddChild(nodeChild);
nodeChild->AddChild(new wxXmlNode(wxXML_TEXT_NODE, wxEmptyString, value));
}
static inline void
HexElement(wxXmlNode *node, const wxChar *name, unsigned long value)
{
TextElement(node, name, wxString::Format(_T("%08lx"), value));
}
#if wxUSE_STACKWALKER
// ============================================================================
// XmlStackWalker implementation
// ============================================================================
void XmlStackWalker::OnStackFrame(const wxStackFrame& frame)
{
m_isOk = true;
wxXmlNode *nodeFrame = new wxXmlNode(wxXML_ELEMENT_NODE, _T("frame"));
m_nodeStack->AddChild(nodeFrame);
NumProperty(nodeFrame, _T("level"), frame.GetLevel());
wxString func = frame.GetName();
if ( !func.empty() )
{
nodeFrame->AddProperty(_T("function"), func);
HexProperty(nodeFrame, _T("offset"), frame.GetOffset());
}
if ( frame.HasSourceLocation() )
{
nodeFrame->AddProperty(_T("file"), frame.GetFileName());
NumProperty(nodeFrame, _T("line"), frame.GetLine());
}
const size_t nParams = frame.GetParamCount();
if ( nParams )
{
wxXmlNode *nodeParams = new wxXmlNode(wxXML_ELEMENT_NODE, _T("parameters"));
nodeFrame->AddChild(nodeParams);
for ( size_t n = 0; n < nParams; n++ )
{
wxXmlNode *
nodeParam = new wxXmlNode(wxXML_ELEMENT_NODE, _T("parameter"));
nodeParams->AddChild(nodeParam);
NumProperty(nodeParam, _T("number"), n);
wxString type, name, value;
if ( !frame.GetParam(n, &type, &name, &value) )
continue;
if ( !type.empty() )
TextElement(nodeParam, _T("type"), type);
if ( !name.empty() )
TextElement(nodeParam, _T("name"), name);
if ( !value.empty() )
TextElement(nodeParam, _T("value"), value);
}
}
}
#endif // wxUSE_STACKWALKER
// ============================================================================
// wxDebugReport implementation
// ============================================================================
// ----------------------------------------------------------------------------
// initialization and cleanup
// ----------------------------------------------------------------------------
wxDebugReport::wxDebugReport()
{
// get a temporary directory name
wxString appname = GetReportName();
// we can't use CreateTempFileName() because it creates a file, not a
// directory, so do our best to create a unique name ourselves
//
// of course, this doesn't protect us against malicious users...
wxFileName fn;
fn.AssignTempFileName(appname);
m_dir.Printf(_T("%s%c%s_dbgrpt-%lu-%s"),
fn.GetPath().c_str(), wxFILE_SEP_PATH, appname.c_str(),
wxGetProcessId(),
wxDateTime::Now().Format(_T("%Y%m%dT%H%M%S")).c_str());
// as we are going to save the process state there use restrictive
// permissions
if ( !wxMkdir(m_dir, 0700) )
{
wxLogSysError(_("Failed to create directory \"%s\""), m_dir.c_str());
wxLogError(_("Debug report couldn't be created."));
Reset();
}
}
wxDebugReport::~wxDebugReport()
{
if ( !m_dir.empty() )
{
// remove all files in this directory
wxDir dir(m_dir);
wxString file;
for ( bool cont = dir.GetFirst(&file); cont; cont = dir.GetNext(&file) )
{
if ( wxRemove(wxFileName(m_dir, file).GetFullPath()) != 0 )
{
wxLogSysError(_("Failed to remove debug report file \"%s\""),
file.c_str());
m_dir.clear();
break;
}
}
}
if ( !m_dir.empty() )
{
// Temp fix: what should this be? eVC++ doesn't like wxRmDir
#ifdef __WXWINCE__
if ( wxRmdir(m_dir.fn_str()) != 0 )
#else
if ( wxRmDir(m_dir.fn_str()) != 0 )
#endif
{
wxLogSysError(_("Failed to clean up debug report directory \"%s\""),
m_dir.c_str());
}
}
}
// ----------------------------------------------------------------------------
// various helpers
// ----------------------------------------------------------------------------
wxString wxDebugReport::GetReportName() const
{
if(wxTheApp)
return wxTheApp->GetAppName();
return _T("wx");
}
void
wxDebugReport::AddFile(const wxString& filename, const wxString& description)
{
wxString name;
wxFileName fn(filename);
if ( fn.IsAbsolute() )
{
// we need to copy the file to the debug report directory: give it the
// same name there
name = fn.GetFullName();
wxCopyFile(fn.GetFullPath(),
wxFileName(GetDirectory(), name).GetFullPath());
}
else // file relative to the report directory
{
name = filename;
wxASSERT_MSG( wxFileName(GetDirectory(), name).FileExists(),
_T("file should exist in debug report directory") );
}
m_files.Add(name);
m_descriptions.Add(description);
}
bool
wxDebugReport::AddText(const wxString& filename,
const wxString& text,
const wxString& description)
{
wxASSERT_MSG( !wxFileName(filename).IsAbsolute(),
_T("filename should be relative to debug report directory") );
wxFileName fn(GetDirectory(), filename);
wxFFile file(fn.GetFullPath(), _T("w"));
if ( !file.IsOpened() || !file.Write(text) )
return false;
AddFile(filename, description);
return true;
}
void wxDebugReport::RemoveFile(const wxString& name)
{
const int n = m_files.Index(name);
wxCHECK_RET( n != wxNOT_FOUND, _T("No such file in wxDebugReport") );
m_files.RemoveAt(n);
m_descriptions.RemoveAt(n);
wxRemove(wxFileName(GetDirectory(), name).GetFullPath());
}
bool wxDebugReport::GetFile(size_t n, wxString *name, wxString *desc) const
{
if ( n >= m_files.GetCount() )
return false;
if ( name )
*name = m_files[n];
if ( desc )
*desc = m_descriptions[n];
return true;
}
void wxDebugReport::AddAll(Context context)
{
#if wxUSE_STACKWALKER
AddContext(context);
#endif // wxUSE_STACKWALKER
#if wxUSE_CRASHREPORT
AddDump(context);
#endif // wxUSE_CRASHREPORT
#if !wxUSE_STACKWALKER && !wxUSE_CRASHREPORT
wxUnusedVar(context);
#endif
}
// ----------------------------------------------------------------------------
// adding basic text information about current context
// ----------------------------------------------------------------------------
#if wxUSE_STACKWALKER
bool wxDebugReport::DoAddSystemInfo(wxXmlNode *nodeSystemInfo)
{
nodeSystemInfo->AddProperty(_T("description"), wxGetOsDescription());
return true;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?