This commit is contained in:
parent
e2c7e0f632
commit
e0bfa8bb19
@ -42,51 +42,61 @@ struct SourceInfo {
|
|||||||
int line = 0;
|
int line = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
SourceInfo get_source_info(const char* executable, void* addr) {
|
std::vector<SourceInfo> get_source_info_batch(const char* executable, const std::vector<void*>& addresses) {
|
||||||
char cmd[512];
|
if (addresses.empty()) return {};
|
||||||
snprintf(cmd, sizeof(cmd), "addr2line -f -e %s %p 2>/dev/null", executable, addr);
|
|
||||||
|
|
||||||
FILE* pipe = popen(cmd, "r");
|
// Build command with all addresses
|
||||||
if (!pipe) return {};
|
std::string cmd = std::string("addr2line -f -e ") + executable;
|
||||||
|
for (void* addr : addresses) {
|
||||||
|
char addr_buf[32];
|
||||||
|
snprintf(addr_buf, sizeof(addr_buf), " %p", addr);
|
||||||
|
cmd += addr_buf;
|
||||||
|
}
|
||||||
|
cmd += " 2>/dev/null";
|
||||||
|
|
||||||
SourceInfo info;
|
FILE* pipe = popen(cmd.c_str(), "r");
|
||||||
char func_buf[1024] = {0};
|
if (!pipe) return std::vector<SourceInfo>(addresses.size());
|
||||||
char file_buf[1024] = {0};
|
|
||||||
int line = 0;
|
|
||||||
|
|
||||||
// Read function name
|
std::vector<SourceInfo> results(addresses.size());
|
||||||
if (fgets(func_buf, sizeof(func_buf), pipe)) {
|
char buffer[1024] = {0};
|
||||||
|
size_t current_frame = 0;
|
||||||
|
|
||||||
|
// Read function names and file/line info for each address
|
||||||
|
while (current_frame < addresses.size() && fgets(buffer, sizeof(buffer), pipe)) {
|
||||||
// Remove trailing newline
|
// Remove trailing newline
|
||||||
size_t len = strlen(func_buf);
|
size_t len = strlen(buffer);
|
||||||
if (len > 0 && func_buf[len-1] == '\n') {
|
if (len > 0 && buffer[len-1] == '\n') {
|
||||||
func_buf[len-1] = '\0';
|
buffer[len-1] = '\0';
|
||||||
}
|
}
|
||||||
info.function = func_buf;
|
|
||||||
|
|
||||||
// Demangle the function name
|
// Demangle function name
|
||||||
int status = 0;
|
int status = 0;
|
||||||
char* demangled = abi::__cxa_demangle(func_buf, nullptr, nullptr, &status);
|
char* demangled = abi::__cxa_demangle(buffer, nullptr, nullptr, &status);
|
||||||
if (demangled) {
|
if (demangled) {
|
||||||
info.function = demangled;
|
results[current_frame].function = demangled;
|
||||||
free(demangled);
|
free(demangled);
|
||||||
}
|
} else {
|
||||||
|
results[current_frame].function = buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read file and line number
|
// Read file and line number
|
||||||
if (fgets(file_buf, sizeof(file_buf), pipe)) {
|
if (fgets(buffer, sizeof(buffer), pipe)) {
|
||||||
char* colon = strchr(file_buf, ':');
|
char* colon = strchr(buffer, ':');
|
||||||
if (colon) {
|
if (colon) {
|
||||||
*colon = '\0';
|
*colon = '\0';
|
||||||
info.line = atoi(colon + 1);
|
results[current_frame].line = atoi(colon + 1);
|
||||||
|
|
||||||
// Extract just the filename
|
// Extract just the filename
|
||||||
const char* slash = strrchr(file_buf, '/');
|
const char* slash = strrchr(buffer, '/');
|
||||||
info.file = (slash ? slash + 1 : file_buf);
|
results[current_frame].file = (slash ? slash + 1 : buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
current_frame++;
|
||||||
|
}
|
||||||
|
|
||||||
pclose(pipe);
|
pclose(pipe);
|
||||||
return info;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_stacktrace() {
|
void print_stacktrace() {
|
||||||
@ -110,13 +120,19 @@ void print_stacktrace() {
|
|||||||
unw_init_local(&cursor, &context);
|
unw_init_local(&cursor, &context);
|
||||||
|
|
||||||
// First pass: collect frames until we find main
|
// First pass: collect frames until we find main
|
||||||
|
std::vector<void*> addresses;
|
||||||
while (unw_step(&cursor) > 0) {
|
while (unw_step(&cursor) > 0) {
|
||||||
if (unw_get_reg(&cursor, UNW_REG_IP, &pc) != 0 || pc == 0) {
|
if (unw_get_reg(&cursor, UNW_REG_IP, &pc) != 0 || pc == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
addresses.push_back(reinterpret_cast<void*>(pc));
|
||||||
|
}
|
||||||
|
|
||||||
SourceInfo info = get_source_info(exe_path, reinterpret_cast<void*>(pc));
|
// Get source info for all addresses in batch
|
||||||
|
std::vector<SourceInfo> results = get_source_info_batch(exe_path, addresses);
|
||||||
|
|
||||||
|
// Process results and check for main
|
||||||
|
for (auto& info : results) {
|
||||||
// Stop collecting after main()
|
// Stop collecting after main()
|
||||||
if (!info.function.empty() &&
|
if (!info.function.empty() &&
|
||||||
(info.function.find("main") != std::string::npos ||
|
(info.function.find("main") != std::string::npos ||
|
||||||
@ -125,7 +141,6 @@ void print_stacktrace() {
|
|||||||
frames.push_back(info);
|
frames.push_back(info);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
frames.push_back(info);
|
frames.push_back(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user