blob: 86ad301b4107e04d6a011088cdd373abd491154e [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#include "config.h"
Joe Damato47cae1e2010-11-08 15:47:39 -08002#include "common.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +02003
Juan Cespedes1fe93d51998-03-13 00:29:21 +01004#include <sys/types.h>
Joe Damato47cae1e2010-11-08 15:47:39 -08005#include <link.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +01006#include <stdio.h>
7#include <string.h>
8#include <signal.h>
Juan Cespedes273ea6d1998-03-14 23:02:40 +01009#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 Wienand2d45b1a2006-02-20 22:48:07 +010016#define MAX_DELAY 100000 /* 100000 microseconds = 0.1 seconds */
Juan Cespedes1fe93d51998-03-13 00:29:21 +010017
18/*
Juan Cespedese0660df2009-05-21 18:14:39 +020019 * Returns a (malloc'd) file name corresponding to a running pid
Juan Cespedes1fe93d51998-03-13 00:29:21 +010020 */
Juan Cespedesf1350522008-12-16 18:19:58 +010021char *
22pid2name(pid_t pid) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +010023 char proc_exe[1024];
24
25 if (!kill(pid, 0)) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010026 int delay = 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +010027
Juan Cespedes1fe93d51998-03-13 00:29:21 +010028 sprintf(proc_exe, "/proc/%d/exe", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +010029
Ian Wienand2d45b1a2006-02-20 22:48:07 +010030 while (delay < MAX_DELAY) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010031 if (!access(proc_exe, F_OK)) {
32 return strdup(proc_exe);
33 }
34 delay += 1000; /* 1 milisecond */
35 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +010036 }
Juan Cespedes273ea6d1998-03-14 23:02:40 +010037 return NULL;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010038}
Joe Damato47cae1e2010-11-08 15:47:39 -080039
40static int
41find_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}