]> git.saurik.com Git - wxWidgets.git/blame - src/unix/stackwalk.cpp
support for getting current event loop on iPhone
[wxWidgets.git] / src / unix / stackwalk.cpp
CommitLineData
eaff0f0d
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: msw/stackwalk.cpp
3// Purpose: wxStackWalker implementation for Unix/glibc
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 2005-01-18
7// RCS-ID: $Id$
8// Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwindows.org>
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#if wxUSE_STACKWALKER
27
28#ifndef WX_PRECOMP
29 #include "wx/string.h"
7a3ba35d
KH
30 #include "wx/app.h"
31 #include "wx/log.h"
32 #include "wx/utils.h"
eaff0f0d
VZ
33#endif
34
35#include "wx/stackwalk.h"
a82c2299 36#include "wx/stdpaths.h"
eaff0f0d
VZ
37
38#include <execinfo.h>
39
40#ifdef HAVE_CXA_DEMANGLE
41 #include <cxxabi.h>
42#endif // HAVE_CXA_DEMANGLE
43
ede3a6d6
VZ
44// ----------------------------------------------------------------------------
45// tiny helper wrapper around popen/pclose()
46// ----------------------------------------------------------------------------
47
48class wxStdioPipe
49{
50public:
51 // ctor parameters are passed to popen()
52 wxStdioPipe(const char *command, const char *type)
53 {
54 m_fp = popen(command, type);
55 }
56
57 // conversion to stdio FILE
58 operator FILE *() const { return m_fp; }
59
60 // dtor closes the pipe
61 ~wxStdioPipe()
62 {
63 if ( m_fp )
64 pclose(m_fp);
65 }
66
67private:
68 FILE *m_fp;
69
c0c133e1 70 wxDECLARE_NO_COPY_CLASS(wxStdioPipe);
ede3a6d6
VZ
71};
72
eaff0f0d
VZ
73// ============================================================================
74// implementation
75// ============================================================================
76
eaff0f0d
VZ
77// ----------------------------------------------------------------------------
78// wxStackFrame
79// ----------------------------------------------------------------------------
80
81void wxStackFrame::OnGetName()
82{
a82c2299 83 if ( !m_name.empty() )
eaff0f0d
VZ
84 return;
85
a82c2299
RR
86 // we already tried addr2line in wxStackWalker::InitFrames: it always
87 // gives us demangled names (even if __cxa_demangle is not available) when
88 // the function is part of the ELF (when it's in a shared object addr2line
89 // will give "??") and because it seems less error-prone.
eaff0f0d 90 // when it works, backtrace_symbols() sometimes returns incorrect results
eaff0f0d
VZ
91
92 // format is: "module(funcname+offset) [address]" but the part in
93 // parentheses can be not present
94 wxString syminfo = wxString::FromAscii(m_syminfo);
9a83f860 95 const size_t posOpen = syminfo.find(wxT('('));
eaff0f0d
VZ
96 if ( posOpen != wxString::npos )
97 {
9a83f860 98 const size_t posPlus = syminfo.find(wxT('+'), posOpen + 1);
eaff0f0d
VZ
99 if ( posPlus != wxString::npos )
100 {
9a83f860 101 const size_t posClose = syminfo.find(wxT(')'), posPlus + 1);
eaff0f0d
VZ
102 if ( posClose != wxString::npos )
103 {
104 if ( m_name.empty() )
105 {
106 m_name.assign(syminfo, posOpen + 1, posPlus - posOpen - 1);
107
108#ifdef HAVE_CXA_DEMANGLE
109 int rc = -1;
110 char *cppfunc = __cxxabiv1::__cxa_demangle
111 (
112 m_name.mb_str(),
113 NULL, // output buffer (none, alloc it)
114 NULL, // [out] len of output buffer
115 &rc
116 );
117 if ( rc == 0 )
118 m_name = wxString::FromAscii(cppfunc);
119
120 free(cppfunc);
121#endif // HAVE_CXA_DEMANGLE
122 }
123
124 unsigned long ofs;
125 if ( wxString(syminfo, posPlus + 1, posClose - posPlus - 1).
126 ToULong(&ofs, 0) )
127 m_offset = ofs;
128 }
129 }
eaff0f0d 130
1872e042
VZ
131 m_module.assign(syminfo, posOpen);
132 }
133 else // not in "module(funcname+offset)" format
134 {
135 m_module = syminfo;
136 }
eaff0f0d
VZ
137}
138
a82c2299
RR
139
140// ----------------------------------------------------------------------------
141// wxStackWalker
142// ----------------------------------------------------------------------------
143
144// that many frames should be enough for everyone
145#define MAX_FRAMES 200
146
147// we need a char buffer big enough to contain a call to addr2line with
148// up to MAX_FRAMES addresses !
149// NB: %p specifier will print the pointer in hexadecimal form
150// and thus will require 2 chars for each byte + 3 for the
151// " 0x" prefix
152#define CHARS_PER_FRAME (sizeof(void*) * 2 + 3)
153
154// BUFSIZE will be 2250 for 32 bit machines
155#define BUFSIZE (50 + MAX_FRAMES*CHARS_PER_FRAME)
156
157// static data
158void *wxStackWalker::ms_addresses[MAX_FRAMES];
159char **wxStackWalker::ms_symbols = NULL;
160int wxStackWalker::m_depth = 0;
161wxString wxStackWalker::ms_exepath;
162static char g_buf[BUFSIZE];
163
164
165void wxStackWalker::SaveStack(size_t maxDepth)
166{
167 // read all frames required
168 maxDepth = wxMin(WXSIZEOF(ms_addresses)/sizeof(void*), maxDepth);
169 m_depth = backtrace(ms_addresses, maxDepth*sizeof(void*));
170 if ( !m_depth )
171 return;
172
173 ms_symbols = backtrace_symbols(ms_addresses, m_depth);
174}
175
176void wxStackWalker::ProcessFrames(size_t skip)
eaff0f0d 177{
a82c2299
RR
178 wxStackFrame frames[MAX_FRAMES];
179
180 if (!ms_symbols || !m_depth)
eaff0f0d
VZ
181 return;
182
a82c2299
RR
183 // we have 3 more "intermediate" frames which the calling code doesn't know
184 // about, account for them
185 skip += 3;
186
187 // call addr2line only once since this call may be very slow
188 // (it has to load in memory the entire EXE of this app which may be quite
189 // big, especially if it contains debug info and is compiled statically!)
190 int towalk = InitFrames(frames, m_depth - skip, &ms_addresses[skip], &ms_symbols[skip]);
191
192 // now do user-defined operations on each frame
193 for ( int n = 0; n < towalk - (int)skip; n++ )
194 OnStackFrame(frames[n]);
195}
196
197void wxStackWalker::FreeStack()
198{
199 // ms_symbols has been allocated by backtrace_symbols() and it's the responsibility
200 // of the caller, i.e. us, to free that pointer
201 if (ms_symbols)
202 free( ms_symbols );
203 ms_symbols = NULL;
204 m_depth = 0;
205}
eaff0f0d 206
a82c2299
RR
207int wxStackWalker::InitFrames(wxStackFrame *arr, size_t n, void **addresses, char **syminfo)
208{
eaff0f0d
VZ
209 // we need to launch addr2line tool to get this information and we need to
210 // have the program name for this
211 wxString exepath = wxStackWalker::GetExePath();
212 if ( exepath.empty() )
213 {
a82c2299
RR
214 exepath = wxStandardPaths::Get().GetExecutablePath();
215 if ( exepath.empty() )
216 {
217 wxLogDebug(wxT("Cannot parse stack frame because the executable ")
218 wxT("path could not be detected"));
219 return 0;
220 }
eaff0f0d
VZ
221 }
222
a82c2299
RR
223 // build the (long) command line for executing addr2line in an optimized way
224 // (e.g. use always chars, even in Unicode build: popen() always takes chars)
8c8b50a9 225 int len = snprintf(g_buf, BUFSIZE, "addr2line -C -f -e \"%s\"", (const char*) exepath.mb_str());
a82c2299
RR
226 len = (len <= 0) ? strlen(g_buf) : len; // in case snprintf() is broken
227 for (size_t i=0; i<n; i++)
eaff0f0d 228 {
a82c2299
RR
229 snprintf(&g_buf[len], BUFSIZE - len, " %p", addresses[i]);
230 len = strlen(g_buf);
ede3a6d6
VZ
231 }
232
a82c2299 233 //wxLogDebug(wxT("piping the command '%s'"), g_buf); // for debug only
ede3a6d6 234
a82c2299
RR
235 wxStdioPipe fp(g_buf, "r");
236 if ( !fp )
237 return 0;
eaff0f0d 238
a82c2299
RR
239 // parse addr2line output (should be exactly 2 lines for each address)
240 // reusing the g_buf used for building the command line above
241 wxString name, filename;
1d2c115e
VZ
242 unsigned long line = 0,
243 curr = 0;
244 for ( size_t i = 0; i < n; i++ )
ede3a6d6 245 {
a82c2299
RR
246 // 1st line has function name
247 if ( fgets(g_buf, WXSIZEOF(g_buf), fp) )
248 {
249 name = wxString::FromAscii(g_buf);
250 name.RemoveLast(); // trailing newline
ede3a6d6 251
9a83f860 252 if ( name == wxT("??") )
a82c2299
RR
253 name.clear();
254 }
255 else
256 {
9a83f860 257 wxLogDebug(wxT("cannot read addr2line output for stack frame #%lu"),
172d83f4 258 (unsigned long)i);
a82c2299
RR
259 return false;
260 }
261
262 // 2nd one -- the file/line info
263 if ( fgets(g_buf, WXSIZEOF(g_buf), fp) )
ede3a6d6 264 {
a82c2299
RR
265 filename = wxString::FromAscii(g_buf);
266 filename.RemoveLast();
267
9a83f860 268 const size_t posColon = filename.find(wxT(':'));
a82c2299 269 if ( posColon != wxString::npos )
eaff0f0d 270 {
1d2c115e
VZ
271 // parse line number (it's ok if it fails, this will just leave
272 // line at its current, invalid, 0 value)
273 wxString(filename, posColon + 1, wxString::npos).ToULong(&line);
a82c2299
RR
274
275 // remove line number from 'filename'
276 filename.erase(posColon);
9a83f860 277 if ( filename == wxT("??") )
a82c2299 278 filename.clear();
eaff0f0d
VZ
279 }
280 else
281 {
9a83f860
VZ
282 wxLogDebug(wxT("Unexpected addr2line format: \"%s\" - ")
283 wxT("the semicolon is missing"),
a82c2299 284 filename.c_str());
eaff0f0d
VZ
285 }
286 }
a82c2299 287
dd9b4376
RR
288 // now we've got enough info to initialize curr-th stack frame
289 // (at worst, only addresses[i] and syminfo[i] have been initialized,
290 // but wxStackFrame::OnGetName may still be able to get function name):
291 arr[curr++].Set(name, filename, syminfo[i], i, line, addresses[i]);
eaff0f0d 292 }
eaff0f0d 293
a82c2299
RR
294 return curr;
295}
eaff0f0d 296
a82c2299 297void wxStackWalker::Walk(size_t skip, size_t maxDepth)
eaff0f0d 298{
a82c2299
RR
299 // read all frames required
300 SaveStack(maxDepth);
eaff0f0d 301
a82c2299
RR
302 // process them
303 ProcessFrames(skip);
eaff0f0d 304
a82c2299
RR
305 // cleanup
306 FreeStack();
eaff0f0d
VZ
307}
308
309#endif // wxUSE_STACKWALKER