| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 1 | #include "config.h" |
| Joe Damato | 47cae1e | 2010-11-08 15:47:39 -0800 | [diff] [blame^] | 2 | #include "common.h" |
| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 3 | |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 4 | #include <sys/types.h> |
| Joe Damato | 47cae1e | 2010-11-08 15:47:39 -0800 | [diff] [blame^] | 5 | #include <link.h> |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | #include <signal.h> |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 9 | #include <unistd.h> |
| 10 | |
| 11 | /* /proc/pid doesn't exist just after the fork, and sometimes `ltrace' |
| 12 | * couldn't open it to find the executable. So it may be necessary to |
| 13 | * have a bit delay |
| 14 | */ |
| 15 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 16 | #define MAX_DELAY 100000 /* 100000 microseconds = 0.1 seconds */ |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 17 | |
| 18 | /* |
| Juan Cespedes | e0660df | 2009-05-21 18:14:39 +0200 | [diff] [blame] | 19 | * Returns a (malloc'd) file name corresponding to a running pid |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 20 | */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 21 | char * |
| 22 | pid2name(pid_t pid) { |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 23 | char proc_exe[1024]; |
| 24 | |
| 25 | if (!kill(pid, 0)) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 26 | int delay = 0; |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 27 | |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 28 | sprintf(proc_exe, "/proc/%d/exe", pid); |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 29 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 30 | while (delay < MAX_DELAY) { |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 31 | if (!access(proc_exe, F_OK)) { |
| 32 | return strdup(proc_exe); |
| 33 | } |
| 34 | delay += 1000; /* 1 milisecond */ |
| 35 | } |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 36 | } |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 37 | return NULL; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 38 | } |
| Joe Damato | 47cae1e | 2010-11-08 15:47:39 -0800 | [diff] [blame^] | 39 | |
| 40 | static int |
| 41 | find_dynamic_entry_addr(Process *proc, void *pvAddr, int d_tag, void **addr) { |
| 42 | int i = 0, done = 0; |
| 43 | ElfW(Dyn) entry; |
| 44 | |
| 45 | debug(DEBUG_FUNCTION, "find_dynamic_entry()"); |
| 46 | |
| 47 | if (addr == NULL || pvAddr == NULL || d_tag < 0 || d_tag > DT_NUM) { |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | while ((!done) && (i < ELF_MAX_SEGMENTS) && |
| 52 | (sizeof(entry) == umovebytes(proc, pvAddr, &entry, sizeof(entry))) && |
| 53 | (entry.d_tag != DT_NULL)) { |
| 54 | if (entry.d_tag == d_tag) { |
| 55 | done = 1; |
| 56 | *addr = (void *)entry.d_un.d_val; |
| 57 | } |
| 58 | pvAddr += sizeof(entry); |
| 59 | i++; |
| 60 | } |
| 61 | |
| 62 | if (done) { |
| 63 | debug(2, "found address: 0x%p in dtag %d\n", *addr, d_tag); |
| 64 | return 0; |
| 65 | } |
| 66 | else { |
| 67 | debug(2, "Couldn't address for dtag!\n"); |
| 68 | return -1; |
| 69 | } |
| 70 | } |