@@ -36,40 +36,46 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3636#endif
3737
3838#include < string>
39+ #include < array>
3940#include < exception>
4041
41-
42- std::string get_backtrace () {
42+ std::string get_backtrace ()
43+ {
4344#ifdef __GLIBC__
44- void *trace[16 ];
45- int i = 0 , trace_size = 0 ;
46-
47- trace_size = backtrace (trace, 16 );
48- char ** funcNames = backtrace_symbols (trace, trace_size);
45+ std::array<void *, 16 > trace;
46+ int trace_size = 0 ;
4947
48+ trace_size = backtrace (trace.data (), 16 );
49+ char **funcNames = backtrace_symbols (trace.data (), trace_size);
5050
51- std::string message = " \n *** Execution path***\n " ;
52- for (i = 0 ; i < trace_size; ++i) {
51+ std::string message = " \n *** Execution path***\n " ;
52+ if (funcNames != nullptr )
53+ {
54+ for (int i = 0 ; i < trace_size; ++i)
55+ {
5356 message += " [bt]" + static_cast <std::string>(funcNames[i]) + " \n " ;
5457 }
55-
5658 free (funcNames);
57- return message;
59+ }
60+ else
61+ {
62+ message += " [bt] Failed to get backtrace symbols\n " ;
63+ }
64+
65+ return message;
5866#else
59- return " " ;
67+ return " " ;
6068#endif
6169}
6270
63- std::string get_backtrace (const std::string &msg) {
71+ std::string get_backtrace (const std::string &msg)
72+ {
6473 return std::string (" \n " ) + msg + " \n " + get_backtrace ();
6574}
6675
67-
68-
69- const char * AssertFailedException::what () const throw() {
76+ const char *AssertFailedException::what () const throw()
77+ {
7078 return str.c_str ();
7179}
7280
73- AssertFailedException::AssertFailedException (std::string msg) :
74- str(msg) {}
75-
81+ AssertFailedException::AssertFailedException (std::string msg) : str(msg) {}
0 commit comments