Mercurial > hg > truffle
comparison agent/src/os/linux/libproc_impl.c @ 14373:d7cb88bd7046
7127191: SA JSDB does not display native symbols correctly for transported Linux cores
Summary: Better handle SA_ALTROOT
Reviewed-by: sla, sspitsyn
author | dsamersoff |
---|---|
date | Wed, 12 Feb 2014 23:39:53 +0400 |
parents | 2394a89e89f4 |
children | 4ca6dc0799b6 |
comparison
equal
deleted
inserted
replaced
14367:46eeb3056482 | 14373:d7cb88bd7046 |
---|---|
27 #include <string.h> | 27 #include <string.h> |
28 #include <fcntl.h> | 28 #include <fcntl.h> |
29 #include <thread_db.h> | 29 #include <thread_db.h> |
30 #include "libproc_impl.h" | 30 #include "libproc_impl.h" |
31 | 31 |
32 static const char* alt_root = NULL; | |
33 static int alt_root_len = -1; | |
34 | |
35 #define SA_ALTROOT "SA_ALTROOT" | 32 #define SA_ALTROOT "SA_ALTROOT" |
36 | 33 |
37 static void init_alt_root() { | |
38 if (alt_root_len == -1) { | |
39 alt_root = getenv(SA_ALTROOT); | |
40 if (alt_root) { | |
41 alt_root_len = strlen(alt_root); | |
42 } else { | |
43 alt_root_len = 0; | |
44 } | |
45 } | |
46 } | |
47 | |
48 int pathmap_open(const char* name) { | 34 int pathmap_open(const char* name) { |
49 int fd; | 35 static const char *alt_root = NULL; |
50 char alt_path[PATH_MAX + 1]; | 36 static int alt_root_initialized = 0; |
51 | 37 |
52 init_alt_root(); | 38 int fd; |
53 | 39 char alt_path[PATH_MAX + 1], *alt_path_end; |
54 if (alt_root_len > 0) { | 40 const char *s; |
55 strcpy(alt_path, alt_root); | 41 |
56 strcat(alt_path, name); | 42 if (!alt_root_initialized) { |
57 fd = open(alt_path, O_RDONLY); | 43 alt_root_initialized = -1; |
58 if (fd >= 0) { | 44 alt_root = getenv(SA_ALTROOT); |
59 print_debug("path %s substituted for %s\n", alt_path, name); | 45 } |
60 return fd; | 46 |
61 } | 47 if (alt_root == NULL) { |
62 | 48 return open(name, O_RDONLY); |
63 if (strrchr(name, '/')) { | 49 } |
64 strcpy(alt_path, alt_root); | 50 |
65 strcat(alt_path, strrchr(name, '/')); | 51 strcpy(alt_path, alt_root); |
66 fd = open(alt_path, O_RDONLY); | 52 alt_path_end = alt_path + strlen(alt_path); |
67 if (fd >= 0) { | 53 |
68 print_debug("path %s substituted for %s\n", alt_path, name); | 54 // Strip path items one by one and try to open file with alt_root prepended |
69 return fd; | 55 s = name; |
70 } | 56 while (1) { |
71 } | 57 strcat(alt_path, s); |
72 } else { | 58 s += 1; |
73 fd = open(name, O_RDONLY); | 59 |
74 if (fd >= 0) { | 60 fd = open(alt_path, O_RDONLY); |
75 return fd; | 61 if (fd >= 0) { |
76 } | 62 print_debug("path %s substituted for %s\n", alt_path, name); |
77 } | 63 return fd; |
78 | 64 } |
79 return -1; | 65 |
66 // Linker always put full path to solib to process, so we can rely | |
67 // on presence of /. If slash is not present, it means, that SOlib doesn't | |
68 // physically exist (e.g. linux-gate.so) and we fail opening it anyway | |
69 if ((s = strchr(s, '/')) == NULL) { | |
70 break; | |
71 } | |
72 | |
73 *alt_path_end = 0; | |
74 } | |
75 | |
76 return -1; | |
80 } | 77 } |
81 | 78 |
82 static bool _libsaproc_debug; | 79 static bool _libsaproc_debug; |
83 | 80 |
84 void print_debug(const char* format,...) { | 81 void print_debug(const char* format,...) { |