Repository navigation
Expand file tree
/
Copy pathget_caller.cpp
More file actions
97 lines (80 loc) · 2.11 KB
/
Copy pathget_caller.cpp
File metadata and controls
97 lines (80 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <cxxabi.h>
#include <string.h>
#include <algorithm>
#include <string>
int BackTraceCaller(const size_t level,std::string& caller)
{
size_t frames = level + 2; // frames need to trace
void *preArray[32];
void **array = preArray;
if ( frames > sizeof(preArray)/sizeof(preArray[0]) ){
array = (void **)malloc(sizeof(void*)*frames);
}
if ( array == NULL ) {
return __LINE__;
}
struct ArrayCleanup{
void ** &m_array;
void ** m_compare;
ArrayCleanup(void **& array, void** compare):m_array(array),m_compare(compare){}
~ArrayCleanup(){
if(m_array != m_compare){
free(m_array);
m_array = NULL;
}
}
}arrayCleanup(array,preArray);
size_t tracedFrames = backtrace (array, frames);
if(tracedFrames != frames){
return __LINE__;
}
// Get the symbol from the desired stack frame
char * symbol;
char **strings = backtrace_symbols (&(array[tracedFrames-1]), 1);
if(strings == NULL){
return __LINE__;
}
struct AutoFree{
void * m_ptr;
AutoFree(void * ptr):m_ptr(ptr){}
~AutoFree() { free(m_ptr); }
}stringsAutoFree(strings);
symbol = strings[0];
const char* const left = "(";
const char* const right = "+";
char* begin = std::find_first_of(symbol,symbol+strlen(symbol),left,left+1) + 1;
char* end = std::find_first_of(symbol,symbol+strlen(symbol),right,right+1);
if( (begin>=symbol+strlen(symbol)) || (end>=symbol+strlen(symbol)) || (begin>=end) ) {
return __LINE__;
}
*end = '\0';
char * mangled = begin;
int status;
char* demangled = abi::__cxa_demangle(mangled,NULL,NULL,&status);
AutoFree freeDemangled(demangled);
if( (status!=0) && (status!=-2) ){
return __LINE__;
}
if(status==-2){
caller = mangled;
}else{
caller = demangled;
}
return 0;
}
#include <iostream>
/* A dummy function to make the backtrace more interesting. */
void dummy_function (void)
{
std::string name;
BackTraceCaller(0,name);
std::cout<<name<<std::endl;
}
int main (int argc, char** argv)
{
dummy_function ();
return 0;
}