45 static std::string getLine(
const std::string&,
const std::string&);
46 static std::string
pOpen(
const std::string&,
int line=0);
51 int main(
int argc,
char *argv[])
53 int optHelp = 0, optFunctions = 0, optVersion = 0;
55 std::string filename =
"a.out";
56 std::vector<std::string> addresses;
58 static struct option opts[] =
60 {
"target", required_argument,
nullptr,
'b' },
61 {
"demangle", required_argument,
nullptr,
'C' },
62 {
"exe", required_argument,
nullptr,
'e' },
63 {
"functions", no_argument, &optFunctions, 1 },
64 {
"version", no_argument, &optVersion, 1 },
65 {
"basename", no_argument,
nullptr,
's' },
66 {
"inlines", no_argument,
nullptr,
'i' },
67 {
"section", required_argument,
nullptr,
'j' },
68 {
"help", no_argument, &optHelp, 1 },
69 {
nullptr, 0,
nullptr, 0 }
72 while ((ch = getopt_long(argc, argv,
"b:C:e:fVsij:H", opts,
nullptr)) != -1)
77 filename = std::string(optarg);
105 addresses.push_back(std::string(*argv));
110 for (
const auto& addr : addresses)
112 std::cout<<
'\n' << getLine(filename, addr).c_str() <<
'\n';
122 <<
"usage: addr2line [-e filename|--exe=filename]"
130 std::cout<<
"OpenFOAM addr2line emulator\n" <<
std::endl;
135 std::string
pOpen(
const std::string& cmd,
int line)
139 FILE* cmdPipe = popen(cmd.c_str(),
"r");
140 if (!cmdPipe)
return res;
145 for (
int cnt = 0; cnt <= line; ++cnt)
148 ssize_t linelen = ::getline(&buf, &linecap, cmdPipe);
157 res = std::string(buf);
162 res.resize(res.size()-1);
176 std::string getLine(
const std::string& filename,
const std::string& addr)
181 "echo 'image lookup -va " + addr
184 +
"-O 'target create --no-dependents -a x86_64 "
187 +
"target module load -f "
189 +
" __TEXT 0x0' 2>/dev/null"
190 +
" | grep LineEntry"
197 if (!
re.match(line, groups))
203 line = groups[1].
str() +
":" + groups[2].
str();