]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/stackwalk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/stackwalk.cpp
3 // Purpose: wxStackWalker implementation for Unix/glibc
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
29 #include "wx/string.h"
35 #include "wx/stackwalk.h"
39 #ifdef HAVE_CXA_DEMANGLE
41 #endif // HAVE_CXA_DEMANGLE
43 // ============================================================================
45 // ============================================================================
47 wxString
wxStackWalker::ms_exepath
;
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 void wxStackFrame::OnGetName()
60 // try addr2line first because it always gives us demangled names (even if
61 // __cxa_demangle is not available) and because it seems less error-prone
62 // when it works, backtrace_symbols() sometimes returns incorrect results
65 // format is: "module(funcname+offset) [address]" but the part in
66 // parentheses can be not present
67 wxString syminfo
= wxString::FromAscii(m_syminfo
);
68 const size_t posOpen
= syminfo
.find(_T('('));
69 if ( posOpen
!= wxString::npos
)
71 const size_t posPlus
= syminfo
.find(_T('+'), posOpen
+ 1);
72 if ( posPlus
!= wxString::npos
)
74 const size_t posClose
= syminfo
.find(_T(')'), posPlus
+ 1);
75 if ( posClose
!= wxString::npos
)
79 m_name
.assign(syminfo
, posOpen
+ 1, posPlus
- posOpen
- 1);
81 #ifdef HAVE_CXA_DEMANGLE
83 char *cppfunc
= __cxxabiv1::__cxa_demangle
86 NULL
, // output buffer (none, alloc it)
87 NULL
, // [out] len of output buffer
91 m_name
= wxString::FromAscii(cppfunc
);
94 #endif // HAVE_CXA_DEMANGLE
98 if ( wxString(syminfo
, posPlus
+ 1, posClose
- posPlus
- 1).
105 m_module
.assign(syminfo
, posOpen
);
108 void wxStackFrame::OnGetLocation()
113 m_hasLocation
= true;
115 // we need to launch addr2line tool to get this information and we need to
116 // have the program name for this
117 wxString exepath
= wxStackWalker::GetExePath();
118 if ( exepath
.empty() )
120 if ( !wxTheApp
|| !wxTheApp
->argv
)
122 exepath
= wxTheApp
->argv
[0];
125 wxArrayString output
;
127 if ( wxExecute(wxString::Format(_T("addr2line -C -f -e \"%s\" %p"),
129 m_address
), output
) == 0 )
131 if ( output
.GetCount() != 2 )
133 wxLogDebug(_T("Unexpected addr2line output."));
135 else // 1st line has function name, 2nd one -- the file/line info
137 if ( GetName().empty() )
140 if ( m_name
== _T("??") )
144 const size_t posColon
= output
[1].find(_T(':'));
145 if ( posColon
!= wxString::npos
)
147 m_filename
.assign(output
[1], 0, posColon
);
148 if ( m_filename
== _T("??") )
155 if ( wxString(output
[1], posColon
+ 1, wxString::npos
).
162 wxLogDebug(_T("Unexpected addr2line format: \"%s\""),
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 void wxStackWalker::Walk(size_t skip
)
175 // that many frames should be enough for everyone
176 void *addresses
[200];
178 int depth
= backtrace(addresses
, WXSIZEOF(addresses
));
182 char **symbols
= backtrace_symbols(addresses
, depth
);
184 for ( int n
= 0; n
< depth
; n
++ )
186 wxStackFrame
frame(n
, addresses
[n
], symbols
[n
]);
191 #endif // wxUSE_STACKWALKER