From 2ec7c9e7eb48225e62cd15cf7897e77fcedb0c6d Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Wed, 7 May 2025 11:52:53 +0200 Subject: [PATCH] proj/intercept.c: Parse readelf output correctly --- proj/intercept/src/intercept.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/proj/intercept/src/intercept.c b/proj/intercept/src/intercept.c index d223807..d250777 100644 --- a/proj/intercept/src/intercept.c +++ b/proj/intercept/src/intercept.c @@ -890,6 +890,9 @@ static int iter_lib_debug(struct dl_phdr_info *info, size_t size, void *data) { name = name_buf; } + char cmd_buf[512]; + snprintf(cmd_buf, sizeof(cmd_buf), "readelf --debug-dump=decodedline '%s' | grep 0x | sort -k3", name_buf); + int fds[2]; __real_pipe(fds); pid_t pid = __real_fork(); @@ -902,13 +905,13 @@ static int iter_lib_debug(struct dl_phdr_info *info, size_t size, void *data) { __real_close(STDERR_FILENO); __real_dup2(fds[1], STDOUT_FILENO); __real_close(fds[1]); - __real_execle("/bin/readelf", "readelf", "--debug-dump=decodedline", name, NULL, NULL); + __real_execle("/bin/sh", "sh", "-c", cmd_buf, NULL, NULL); default: // parent __real_close(fds[1]); // close write end } - char state = 0; + char state = 'A'; char buf[1024]; char line[3][256] = {0}; int n = 0, n_entries = 0; @@ -916,17 +919,7 @@ static int iter_lib_debug(struct dl_phdr_info *info, size_t size, void *data) { for (ssize_t num = -2; (num != -1 && num != 0) || (num == -1 && errno == EINTR); num = __real_read(fds[0], buf, sizeof(buf))) { for (int i = 0; i < num; i++) { char ch = buf[i]; - if (state == 0 && ch == 'S') { - state = 'S'; - } else if (state == 'S' && ch == 't') { - state = 't'; - } else if (state == 't' && ch == 'm') { - state = 'm'; - } else if (state == 'm' && ch == 't') { - state = 'T'; - } else if (state == 'T' && ch == '\n') { - state = 'A'; - } else if (state == 'A' || state == 'B' || state == 'C') { + if (state == 'A' || state == 'B' || state == 'C') { // file name | line number | starting address if (ch == ' ') { if (n == 0)