OpenCPN Partial API docs
Loading...
Searching...
No Matches
crashprint.cpp
Go to the documentation of this file.
1
2// File: crashprint.cpp
3// Purpose: wxCrashPrint
4// Maintainer: Wyo
5// Created: 2004-09-28
6// Copyright: (c) 2004 wxCode
7// Licence: wxWindows
9
16#include <wx/wxprec.h>
17
18#ifndef WX_PRECOMP
19#include <wx/wx.h>
20#endif
21
22#if defined(__linux__)
23#include <execinfo.h> // Needed for backtrace
24#include <cxxabi.h> // Needed for __cxa_demangle
25#include <unistd.h>
26#endif
27
28#include <wx/string.h>
29
30#include "crashprint.h" // crash print support
31
32wxCrashPrint::wxCrashPrint(int flags, const wxString &fname) {
33 m_flags = flags;
34 m_fname = fname;
35};
36
37void wxCrashPrint::Report() {
38#if defined(__linux__)
39 wxString appname = wxTheApp->GetAppName();
40
41 // get the backtrace with symbols
42 int btCount;
43 btCount = backtrace(m_btBuffer, maxBtCount);
44 if (btCount < 0) {
45 wxPrintf("\n%s: Backtrace could not be created\n", appname.c_str());
46 }
47 m_btStrings = backtrace_symbols(m_btBuffer, btCount);
48 if (!m_btStrings) {
49 wxPrintf("\n%s: Backtrace could not get symbols\n", appname.c_str());
50 }
51
52 // print backtrace announcement
53 wxPrintf("\n*** %s (%s) crashed ***, see backtrace!\n", appname.c_str(),
54 wxVERSION_STRING);
55
56 // format backtrace lines
57 int status;
58 wxString cur, addr, func, addrs;
59 wxArrayString lines;
60 size_t pos1, pos2;
61 if (m_btStrings)
62 for (int i = 0; i < btCount; ++i) {
63 cur = wxString::FromAscii(m_btStrings[i]);
64 pos1 = cur.rfind('[');
65 pos2 = cur.rfind(']');
66 if ((pos1 != wxString::npos) && (pos2 != wxString::npos)) {
67 addr = cur.substr(pos1 + 1, pos2 - pos1 - 1);
68 addrs.Append(addr + " ");
69 }
70 pos1 = cur.rfind("_Z");
71 pos2 = cur.rfind('+');
72 if (pos2 == wxString::npos) pos2 = cur.rfind(')');
73 if (pos1 != wxString::npos) {
74 func = cur.substr(pos1, pos2 - pos1);
75 func = wxString::FromAscii(
76 abi::__cxa_demangle(func.mb_str(), 0, 0, &status));
77 } else {
78 pos1 = cur.rfind('(');
79 if (pos1 != wxString::npos) {
80 func = cur.substr(pos1 + 1, pos2 - pos1 - 1);
81 } else {
82 pos2 = cur.rfind('[');
83 func = cur.substr(0, pos2 - 1);
84 }
85 }
86 lines.Add(addr + " in " + func);
87 if (func == "main") break;
88 }
89
90 // determine line from address
91 wxString cmd = wxString::Format("addr2line -e /proc/%d/exe -s ", getpid());
92 wxArrayString fnames;
93 if (wxExecute(cmd + addrs, fnames) != -1) {
94 for (size_t i = 0; i < fnames.GetCount(); ++i) {
95 wxPrintf("%s at %s\n", lines[i].c_str(), fnames[i].c_str());
96 }
97 } else {
98 for (size_t i = 0; i < lines.GetCount(); ++i) {
99 wxPrintf("%s\n", lines[i].c_str());
100 }
101 }
102#endif
103}
wxCrashPrint(int flags=0, const wxString &fname=wxEmptyString)
constructor
Dump debug info on crash.