annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
8058
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 4919
diff changeset
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1294
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1294
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1294
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24 #include <stdarg.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
25 #include <stdio.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
26 #include <stdlib.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
27 #include <string.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
28 #include <fcntl.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
29 #include <thread_db.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
30 #include "libproc_impl.h"
a61af66fc99e Initial load
duke
parents:
diff changeset
31
a61af66fc99e Initial load
duke
parents:
diff changeset
32 #define SA_ALTROOT "SA_ALTROOT"
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 int pathmap_open(const char* name) {
14373
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
35 static const char *alt_root = NULL;
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
36 static int alt_root_initialized = 0;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
37
14373
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
38 int fd;
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
39 char alt_path[PATH_MAX + 1], *alt_path_end;
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
40 const char *s;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
41
14373
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
42 if (!alt_root_initialized) {
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
43 alt_root_initialized = -1;
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
44 alt_root = getenv(SA_ALTROOT);
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
45 }
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
46
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
47 if (alt_root == NULL) {
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
48 return open(name, O_RDONLY);
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
49 }
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
50
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
51 strcpy(alt_path, alt_root);
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
52 alt_path_end = alt_path + strlen(alt_path);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
53
14373
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
54 // Strip path items one by one and try to open file with alt_root prepended
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
55 s = name;
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
56 while (1) {
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
57 strcat(alt_path, s);
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
58 s += 1;
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
59
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
60 fd = open(alt_path, O_RDONLY);
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
61 if (fd >= 0) {
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
62 print_debug("path %s substituted for %s\n", alt_path, name);
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
63 return fd;
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
64 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
65
14373
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
66 // Linker always put full path to solib to process, so we can rely
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
67 // on presence of /. If slash is not present, it means, that SOlib doesn't
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
68 // physically exist (e.g. linux-gate.so) and we fail opening it anyway
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
69 if ((s = strchr(s, '/')) == NULL) {
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
70 break;
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
71 }
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
72
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
73 *alt_path_end = 0;
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
74 }
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
75
d7cb88bd7046 7127191: SA JSDB does not display native symbols correctly for transported Linux cores
dsamersoff
parents: 8058
diff changeset
76 return -1;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
77 }
a61af66fc99e Initial load
duke
parents:
diff changeset
78
a61af66fc99e Initial load
duke
parents:
diff changeset
79 static bool _libsaproc_debug;
a61af66fc99e Initial load
duke
parents:
diff changeset
80
a61af66fc99e Initial load
duke
parents:
diff changeset
81 void print_debug(const char* format,...) {
a61af66fc99e Initial load
duke
parents:
diff changeset
82 if (_libsaproc_debug) {
a61af66fc99e Initial load
duke
parents:
diff changeset
83 va_list alist;
a61af66fc99e Initial load
duke
parents:
diff changeset
84
a61af66fc99e Initial load
duke
parents:
diff changeset
85 va_start(alist, format);
a61af66fc99e Initial load
duke
parents:
diff changeset
86 fputs("libsaproc DEBUG: ", stderr);
a61af66fc99e Initial load
duke
parents:
diff changeset
87 vfprintf(stderr, format, alist);
a61af66fc99e Initial load
duke
parents:
diff changeset
88 va_end(alist);
a61af66fc99e Initial load
duke
parents:
diff changeset
89 }
a61af66fc99e Initial load
duke
parents:
diff changeset
90 }
a61af66fc99e Initial load
duke
parents:
diff changeset
91
8058
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 4919
diff changeset
92 void print_error(const char* format,...) {
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 4919
diff changeset
93 va_list alist;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 4919
diff changeset
94 va_start(alist, format);
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 4919
diff changeset
95 fputs("ERROR: ", stderr);
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 4919
diff changeset
96 vfprintf(stderr, format, alist);
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 4919
diff changeset
97 va_end(alist);
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 4919
diff changeset
98 }
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 4919
diff changeset
99
0
a61af66fc99e Initial load
duke
parents:
diff changeset
100 bool is_debug() {
a61af66fc99e Initial load
duke
parents:
diff changeset
101 return _libsaproc_debug;
a61af66fc99e Initial load
duke
parents:
diff changeset
102 }
a61af66fc99e Initial load
duke
parents:
diff changeset
103
a61af66fc99e Initial load
duke
parents:
diff changeset
104 // initialize libproc
a61af66fc99e Initial load
duke
parents:
diff changeset
105 bool init_libproc(bool debug) {
a61af66fc99e Initial load
duke
parents:
diff changeset
106 // init debug mode
a61af66fc99e Initial load
duke
parents:
diff changeset
107 _libsaproc_debug = debug;
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 // initialize the thread_db library
a61af66fc99e Initial load
duke
parents:
diff changeset
110 if (td_init() != TD_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
111 print_debug("libthread_db's td_init failed\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
112 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
113 }
a61af66fc99e Initial load
duke
parents:
diff changeset
114
a61af66fc99e Initial load
duke
parents:
diff changeset
115 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
116 }
a61af66fc99e Initial load
duke
parents:
diff changeset
117
a61af66fc99e Initial load
duke
parents:
diff changeset
118 static void destroy_lib_info(struct ps_prochandle* ph) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 lib_info* lib = ph->libs;
a61af66fc99e Initial load
duke
parents:
diff changeset
120 while (lib) {
a61af66fc99e Initial load
duke
parents:
diff changeset
121 lib_info *next = lib->next;
a61af66fc99e Initial load
duke
parents:
diff changeset
122 if (lib->symtab) {
a61af66fc99e Initial load
duke
parents:
diff changeset
123 destroy_symtab(lib->symtab);
a61af66fc99e Initial load
duke
parents:
diff changeset
124 }
a61af66fc99e Initial load
duke
parents:
diff changeset
125 free(lib);
a61af66fc99e Initial load
duke
parents:
diff changeset
126 lib = next;
a61af66fc99e Initial load
duke
parents:
diff changeset
127 }
a61af66fc99e Initial load
duke
parents:
diff changeset
128 }
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 static void destroy_thread_info(struct ps_prochandle* ph) {
a61af66fc99e Initial load
duke
parents:
diff changeset
131 thread_info* thr = ph->threads;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 while (thr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
133 thread_info *next = thr->next;
a61af66fc99e Initial load
duke
parents:
diff changeset
134 free(thr);
a61af66fc99e Initial load
duke
parents:
diff changeset
135 thr = next;
a61af66fc99e Initial load
duke
parents:
diff changeset
136 }
a61af66fc99e Initial load
duke
parents:
diff changeset
137 }
a61af66fc99e Initial load
duke
parents:
diff changeset
138
a61af66fc99e Initial load
duke
parents:
diff changeset
139 // ps_prochandle cleanup
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 // ps_prochandle cleanup
a61af66fc99e Initial load
duke
parents:
diff changeset
142 void Prelease(struct ps_prochandle* ph) {
a61af66fc99e Initial load
duke
parents:
diff changeset
143 // do the "derived class" clean-up first
a61af66fc99e Initial load
duke
parents:
diff changeset
144 ph->ops->release(ph);
a61af66fc99e Initial load
duke
parents:
diff changeset
145 destroy_lib_info(ph);
a61af66fc99e Initial load
duke
parents:
diff changeset
146 destroy_thread_info(ph);
a61af66fc99e Initial load
duke
parents:
diff changeset
147 free(ph);
a61af66fc99e Initial load
duke
parents:
diff changeset
148 }
a61af66fc99e Initial load
duke
parents:
diff changeset
149
a61af66fc99e Initial load
duke
parents:
diff changeset
150 lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base) {
a61af66fc99e Initial load
duke
parents:
diff changeset
151 return add_lib_info_fd(ph, libname, -1, base);
a61af66fc99e Initial load
duke
parents:
diff changeset
152 }
a61af66fc99e Initial load
duke
parents:
diff changeset
153
a61af66fc99e Initial load
duke
parents:
diff changeset
154 lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) {
a61af66fc99e Initial load
duke
parents:
diff changeset
155 lib_info* newlib;
a61af66fc99e Initial load
duke
parents:
diff changeset
156
a61af66fc99e Initial load
duke
parents:
diff changeset
157 if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
158 print_debug("can't allocate memory for lib_info\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
159 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
160 }
a61af66fc99e Initial load
duke
parents:
diff changeset
161
a61af66fc99e Initial load
duke
parents:
diff changeset
162 strncpy(newlib->name, libname, sizeof(newlib->name));
a61af66fc99e Initial load
duke
parents:
diff changeset
163 newlib->base = base;
a61af66fc99e Initial load
duke
parents:
diff changeset
164
a61af66fc99e Initial load
duke
parents:
diff changeset
165 if (fd == -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
166 if ( (newlib->fd = pathmap_open(newlib->name)) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
167 print_debug("can't open shared object %s\n", newlib->name);
a61af66fc99e Initial load
duke
parents:
diff changeset
168 free(newlib);
a61af66fc99e Initial load
duke
parents:
diff changeset
169 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
170 }
a61af66fc99e Initial load
duke
parents:
diff changeset
171 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
172 newlib->fd = fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
173 }
a61af66fc99e Initial load
duke
parents:
diff changeset
174
a61af66fc99e Initial load
duke
parents:
diff changeset
175 // check whether we have got an ELF file. /proc/<pid>/map
a61af66fc99e Initial load
duke
parents:
diff changeset
176 // gives out all file mappings and not just shared objects
a61af66fc99e Initial load
duke
parents:
diff changeset
177 if (is_elf_file(newlib->fd) == false) {
a61af66fc99e Initial load
duke
parents:
diff changeset
178 close(newlib->fd);
a61af66fc99e Initial load
duke
parents:
diff changeset
179 free(newlib);
a61af66fc99e Initial load
duke
parents:
diff changeset
180 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
181 }
a61af66fc99e Initial load
duke
parents:
diff changeset
182
1294
7de45b5044c3 6932270: Allow Java's ELF symtab reader to use separate debuginfo files
never
parents: 0
diff changeset
183 newlib->symtab = build_symtab(newlib->fd, libname);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
184 if (newlib->symtab == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
185 print_debug("symbol table build failed for %s\n", newlib->name);
a61af66fc99e Initial load
duke
parents:
diff changeset
186 }
a61af66fc99e Initial load
duke
parents:
diff changeset
187
a61af66fc99e Initial load
duke
parents:
diff changeset
188 // even if symbol table building fails, we add the lib_info.
a61af66fc99e Initial load
duke
parents:
diff changeset
189 // This is because we may need to read from the ELF file for core file
a61af66fc99e Initial load
duke
parents:
diff changeset
190 // address read functionality. lookup_symbol checks for NULL symtab.
a61af66fc99e Initial load
duke
parents:
diff changeset
191 if (ph->libs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
192 ph->lib_tail->next = newlib;
a61af66fc99e Initial load
duke
parents:
diff changeset
193 ph->lib_tail = newlib;
a61af66fc99e Initial load
duke
parents:
diff changeset
194 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
195 ph->libs = ph->lib_tail = newlib;
a61af66fc99e Initial load
duke
parents:
diff changeset
196 }
a61af66fc99e Initial load
duke
parents:
diff changeset
197 ph->num_libs++;
a61af66fc99e Initial load
duke
parents:
diff changeset
198
a61af66fc99e Initial load
duke
parents:
diff changeset
199 return newlib;
a61af66fc99e Initial load
duke
parents:
diff changeset
200 }
a61af66fc99e Initial load
duke
parents:
diff changeset
201
a61af66fc99e Initial load
duke
parents:
diff changeset
202 // lookup for a specific symbol
a61af66fc99e Initial load
duke
parents:
diff changeset
203 uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name,
a61af66fc99e Initial load
duke
parents:
diff changeset
204 const char* sym_name) {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 // ignore object_name. search in all libraries
a61af66fc99e Initial load
duke
parents:
diff changeset
206 // FIXME: what should we do with object_name?? The library names are obtained
a61af66fc99e Initial load
duke
parents:
diff changeset
207 // by parsing /proc/<pid>/maps, which may not be the same as object_name.
a61af66fc99e Initial load
duke
parents:
diff changeset
208 // What we need is a utility to map object_name to real file name, something
a61af66fc99e Initial load
duke
parents:
diff changeset
209 // dlopen() does by looking at LD_LIBRARY_PATH and /etc/ld.so.cache. For
a61af66fc99e Initial load
duke
parents:
diff changeset
210 // now, we just ignore object_name and do a global search for the symbol.
a61af66fc99e Initial load
duke
parents:
diff changeset
211
a61af66fc99e Initial load
duke
parents:
diff changeset
212 lib_info* lib = ph->libs;
a61af66fc99e Initial load
duke
parents:
diff changeset
213 while (lib) {
a61af66fc99e Initial load
duke
parents:
diff changeset
214 if (lib->symtab) {
a61af66fc99e Initial load
duke
parents:
diff changeset
215 uintptr_t res = search_symbol(lib->symtab, lib->base, sym_name, NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
216 if (res) return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
217 }
a61af66fc99e Initial load
duke
parents:
diff changeset
218 lib = lib->next;
a61af66fc99e Initial load
duke
parents:
diff changeset
219 }
a61af66fc99e Initial load
duke
parents:
diff changeset
220
a61af66fc99e Initial load
duke
parents:
diff changeset
221 print_debug("lookup failed for symbol '%s' in obj '%s'\n",
a61af66fc99e Initial load
duke
parents:
diff changeset
222 sym_name, object_name);
a61af66fc99e Initial load
duke
parents:
diff changeset
223 return (uintptr_t) NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
224 }
a61af66fc99e Initial load
duke
parents:
diff changeset
225
a61af66fc99e Initial load
duke
parents:
diff changeset
226
a61af66fc99e Initial load
duke
parents:
diff changeset
227 const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) {
a61af66fc99e Initial load
duke
parents:
diff changeset
228 const char* res = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
229 lib_info* lib = ph->libs;
a61af66fc99e Initial load
duke
parents:
diff changeset
230 while (lib) {
a61af66fc99e Initial load
duke
parents:
diff changeset
231 if (lib->symtab && addr >= lib->base) {
a61af66fc99e Initial load
duke
parents:
diff changeset
232 res = nearest_symbol(lib->symtab, addr - lib->base, poffset);
a61af66fc99e Initial load
duke
parents:
diff changeset
233 if (res) return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
234 }
a61af66fc99e Initial load
duke
parents:
diff changeset
235 lib = lib->next;
a61af66fc99e Initial load
duke
parents:
diff changeset
236 }
a61af66fc99e Initial load
duke
parents:
diff changeset
237 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
238 }
a61af66fc99e Initial load
duke
parents:
diff changeset
239
a61af66fc99e Initial load
duke
parents:
diff changeset
240 // add a thread to ps_prochandle
a61af66fc99e Initial load
duke
parents:
diff changeset
241 thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
a61af66fc99e Initial load
duke
parents:
diff changeset
242 thread_info* newthr;
a61af66fc99e Initial load
duke
parents:
diff changeset
243 if ( (newthr = (thread_info*) calloc(1, sizeof(thread_info))) == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
244 print_debug("can't allocate memory for thread_info\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
245 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
246 }
a61af66fc99e Initial load
duke
parents:
diff changeset
247
a61af66fc99e Initial load
duke
parents:
diff changeset
248 // initialize thread info
a61af66fc99e Initial load
duke
parents:
diff changeset
249 newthr->pthread_id = pthread_id;
a61af66fc99e Initial load
duke
parents:
diff changeset
250 newthr->lwp_id = lwp_id;
a61af66fc99e Initial load
duke
parents:
diff changeset
251
a61af66fc99e Initial load
duke
parents:
diff changeset
252 // add new thread to the list
a61af66fc99e Initial load
duke
parents:
diff changeset
253 newthr->next = ph->threads;
a61af66fc99e Initial load
duke
parents:
diff changeset
254 ph->threads = newthr;
a61af66fc99e Initial load
duke
parents:
diff changeset
255 ph->num_threads++;
a61af66fc99e Initial load
duke
parents:
diff changeset
256 return newthr;
a61af66fc99e Initial load
duke
parents:
diff changeset
257 }
a61af66fc99e Initial load
duke
parents:
diff changeset
258
a61af66fc99e Initial load
duke
parents:
diff changeset
259
a61af66fc99e Initial load
duke
parents:
diff changeset
260 // struct used for client data from thread_db callback
a61af66fc99e Initial load
duke
parents:
diff changeset
261 struct thread_db_client_data {
a61af66fc99e Initial load
duke
parents:
diff changeset
262 struct ps_prochandle* ph;
a61af66fc99e Initial load
duke
parents:
diff changeset
263 thread_info_callback callback;
a61af66fc99e Initial load
duke
parents:
diff changeset
264 };
a61af66fc99e Initial load
duke
parents:
diff changeset
265
a61af66fc99e Initial load
duke
parents:
diff changeset
266 // callback function for libthread_db
a61af66fc99e Initial load
duke
parents:
diff changeset
267 static int thread_db_callback(const td_thrhandle_t *th_p, void *data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
268 struct thread_db_client_data* ptr = (struct thread_db_client_data*) data;
a61af66fc99e Initial load
duke
parents:
diff changeset
269 td_thrinfo_t ti;
a61af66fc99e Initial load
duke
parents:
diff changeset
270 td_err_e err;
a61af66fc99e Initial load
duke
parents:
diff changeset
271
a61af66fc99e Initial load
duke
parents:
diff changeset
272 memset(&ti, 0, sizeof(ti));
a61af66fc99e Initial load
duke
parents:
diff changeset
273 err = td_thr_get_info(th_p, &ti);
a61af66fc99e Initial load
duke
parents:
diff changeset
274 if (err != TD_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
275 print_debug("libthread_db : td_thr_get_info failed, can't get thread info\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
276 return err;
a61af66fc99e Initial load
duke
parents:
diff changeset
277 }
a61af66fc99e Initial load
duke
parents:
diff changeset
278
a61af66fc99e Initial load
duke
parents:
diff changeset
279 print_debug("thread_db : pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid);
a61af66fc99e Initial load
duke
parents:
diff changeset
280
a61af66fc99e Initial load
duke
parents:
diff changeset
281 if (ptr->callback(ptr->ph, ti.ti_tid, ti.ti_lid) != true)
a61af66fc99e Initial load
duke
parents:
diff changeset
282 return TD_ERR;
a61af66fc99e Initial load
duke
parents:
diff changeset
283
a61af66fc99e Initial load
duke
parents:
diff changeset
284 return TD_OK;
a61af66fc99e Initial load
duke
parents:
diff changeset
285 }
a61af66fc99e Initial load
duke
parents:
diff changeset
286
a61af66fc99e Initial load
duke
parents:
diff changeset
287 // read thread_info using libthread_db
a61af66fc99e Initial load
duke
parents:
diff changeset
288 bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb) {
a61af66fc99e Initial load
duke
parents:
diff changeset
289 struct thread_db_client_data mydata;
a61af66fc99e Initial load
duke
parents:
diff changeset
290 td_thragent_t* thread_agent = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
291 if (td_ta_new(ph, &thread_agent) != TD_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
292 print_debug("can't create libthread_db agent\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
293 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
294 }
a61af66fc99e Initial load
duke
parents:
diff changeset
295
a61af66fc99e Initial load
duke
parents:
diff changeset
296 mydata.ph = ph;
a61af66fc99e Initial load
duke
parents:
diff changeset
297 mydata.callback = cb;
a61af66fc99e Initial load
duke
parents:
diff changeset
298
a61af66fc99e Initial load
duke
parents:
diff changeset
299 // we use libthread_db iterator to iterate thru list of threads.
a61af66fc99e Initial load
duke
parents:
diff changeset
300 if (td_ta_thr_iter(thread_agent, thread_db_callback, &mydata,
a61af66fc99e Initial load
duke
parents:
diff changeset
301 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
a61af66fc99e Initial load
duke
parents:
diff changeset
302 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS) != TD_OK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
303 td_ta_delete(thread_agent);
a61af66fc99e Initial load
duke
parents:
diff changeset
304 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
305 }
a61af66fc99e Initial load
duke
parents:
diff changeset
306
a61af66fc99e Initial load
duke
parents:
diff changeset
307 // delete thread agent
a61af66fc99e Initial load
duke
parents:
diff changeset
308 td_ta_delete(thread_agent);
a61af66fc99e Initial load
duke
parents:
diff changeset
309 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
310 }
a61af66fc99e Initial load
duke
parents:
diff changeset
311
a61af66fc99e Initial load
duke
parents:
diff changeset
312
a61af66fc99e Initial load
duke
parents:
diff changeset
313 // get number of threads
a61af66fc99e Initial load
duke
parents:
diff changeset
314 int get_num_threads(struct ps_prochandle* ph) {
a61af66fc99e Initial load
duke
parents:
diff changeset
315 return ph->num_threads;
a61af66fc99e Initial load
duke
parents:
diff changeset
316 }
a61af66fc99e Initial load
duke
parents:
diff changeset
317
a61af66fc99e Initial load
duke
parents:
diff changeset
318 // get lwp_id of n'th thread
a61af66fc99e Initial load
duke
parents:
diff changeset
319 lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
320 int count = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
321 thread_info* thr = ph->threads;
a61af66fc99e Initial load
duke
parents:
diff changeset
322 while (thr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
323 if (count == index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
324 return thr->lwp_id;
a61af66fc99e Initial load
duke
parents:
diff changeset
325 }
a61af66fc99e Initial load
duke
parents:
diff changeset
326 count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
327 thr = thr->next;
a61af66fc99e Initial load
duke
parents:
diff changeset
328 }
a61af66fc99e Initial load
duke
parents:
diff changeset
329 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
330 }
a61af66fc99e Initial load
duke
parents:
diff changeset
331
a61af66fc99e Initial load
duke
parents:
diff changeset
332 // get regs for a given lwp
a61af66fc99e Initial load
duke
parents:
diff changeset
333 bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct user_regs_struct* regs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
334 return ph->ops->get_lwp_regs(ph, lwp_id, regs);
a61af66fc99e Initial load
duke
parents:
diff changeset
335 }
a61af66fc99e Initial load
duke
parents:
diff changeset
336
a61af66fc99e Initial load
duke
parents:
diff changeset
337 // get number of shared objects
a61af66fc99e Initial load
duke
parents:
diff changeset
338 int get_num_libs(struct ps_prochandle* ph) {
a61af66fc99e Initial load
duke
parents:
diff changeset
339 return ph->num_libs;
a61af66fc99e Initial load
duke
parents:
diff changeset
340 }
a61af66fc99e Initial load
duke
parents:
diff changeset
341
a61af66fc99e Initial load
duke
parents:
diff changeset
342 // get name of n'th solib
a61af66fc99e Initial load
duke
parents:
diff changeset
343 const char* get_lib_name(struct ps_prochandle* ph, int index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
344 int count = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
345 lib_info* lib = ph->libs;
a61af66fc99e Initial load
duke
parents:
diff changeset
346 while (lib) {
a61af66fc99e Initial load
duke
parents:
diff changeset
347 if (count == index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
348 return lib->name;
a61af66fc99e Initial load
duke
parents:
diff changeset
349 }
a61af66fc99e Initial load
duke
parents:
diff changeset
350 count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
351 lib = lib->next;
a61af66fc99e Initial load
duke
parents:
diff changeset
352 }
a61af66fc99e Initial load
duke
parents:
diff changeset
353 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
354 }
a61af66fc99e Initial load
duke
parents:
diff changeset
355
a61af66fc99e Initial load
duke
parents:
diff changeset
356 // get base address of a lib
a61af66fc99e Initial load
duke
parents:
diff changeset
357 uintptr_t get_lib_base(struct ps_prochandle* ph, int index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
358 int count = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
359 lib_info* lib = ph->libs;
a61af66fc99e Initial load
duke
parents:
diff changeset
360 while (lib) {
a61af66fc99e Initial load
duke
parents:
diff changeset
361 if (count == index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
362 return lib->base;
a61af66fc99e Initial load
duke
parents:
diff changeset
363 }
a61af66fc99e Initial load
duke
parents:
diff changeset
364 count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
365 lib = lib->next;
a61af66fc99e Initial load
duke
parents:
diff changeset
366 }
a61af66fc99e Initial load
duke
parents:
diff changeset
367 return (uintptr_t)NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
368 }
a61af66fc99e Initial load
duke
parents:
diff changeset
369
a61af66fc99e Initial load
duke
parents:
diff changeset
370 bool find_lib(struct ps_prochandle* ph, const char *lib_name) {
a61af66fc99e Initial load
duke
parents:
diff changeset
371 lib_info *p = ph->libs;
a61af66fc99e Initial load
duke
parents:
diff changeset
372 while (p) {
a61af66fc99e Initial load
duke
parents:
diff changeset
373 if (strcmp(p->name, lib_name) == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
374 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
375 }
a61af66fc99e Initial load
duke
parents:
diff changeset
376 p = p->next;
a61af66fc99e Initial load
duke
parents:
diff changeset
377 }
a61af66fc99e Initial load
duke
parents:
diff changeset
378 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
379 }
a61af66fc99e Initial load
duke
parents:
diff changeset
380
a61af66fc99e Initial load
duke
parents:
diff changeset
381 //--------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
382 // proc service functions
a61af66fc99e Initial load
duke
parents:
diff changeset
383
a61af66fc99e Initial load
duke
parents:
diff changeset
384 // get process id
a61af66fc99e Initial load
duke
parents:
diff changeset
385 pid_t ps_getpid(struct ps_prochandle *ph) {
a61af66fc99e Initial load
duke
parents:
diff changeset
386 return ph->pid;
a61af66fc99e Initial load
duke
parents:
diff changeset
387 }
a61af66fc99e Initial load
duke
parents:
diff changeset
388
a61af66fc99e Initial load
duke
parents:
diff changeset
389 // ps_pglobal_lookup() looks up the symbol sym_name in the symbol table
a61af66fc99e Initial load
duke
parents:
diff changeset
390 // of the load object object_name in the target process identified by ph.
a61af66fc99e Initial load
duke
parents:
diff changeset
391 // It returns the symbol's value as an address in the target process in
a61af66fc99e Initial load
duke
parents:
diff changeset
392 // *sym_addr.
a61af66fc99e Initial load
duke
parents:
diff changeset
393
a61af66fc99e Initial load
duke
parents:
diff changeset
394 ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,
a61af66fc99e Initial load
duke
parents:
diff changeset
395 const char *sym_name, psaddr_t *sym_addr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
396 *sym_addr = (psaddr_t) lookup_symbol(ph, object_name, sym_name);
a61af66fc99e Initial load
duke
parents:
diff changeset
397 return (*sym_addr ? PS_OK : PS_NOSYM);
a61af66fc99e Initial load
duke
parents:
diff changeset
398 }
a61af66fc99e Initial load
duke
parents:
diff changeset
399
a61af66fc99e Initial load
duke
parents:
diff changeset
400 // read "size" bytes info "buf" from address "addr"
a61af66fc99e Initial load
duke
parents:
diff changeset
401 ps_err_e ps_pdread(struct ps_prochandle *ph, psaddr_t addr,
a61af66fc99e Initial load
duke
parents:
diff changeset
402 void *buf, size_t size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
403 return ph->ops->p_pread(ph, (uintptr_t) addr, buf, size)? PS_OK: PS_ERR;
a61af66fc99e Initial load
duke
parents:
diff changeset
404 }
a61af66fc99e Initial load
duke
parents:
diff changeset
405
a61af66fc99e Initial load
duke
parents:
diff changeset
406 // write "size" bytes of data to debuggee at address "addr"
a61af66fc99e Initial load
duke
parents:
diff changeset
407 ps_err_e ps_pdwrite(struct ps_prochandle *ph, psaddr_t addr,
a61af66fc99e Initial load
duke
parents:
diff changeset
408 const void *buf, size_t size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
409 return ph->ops->p_pwrite(ph, (uintptr_t)addr, buf, size)? PS_OK: PS_ERR;
a61af66fc99e Initial load
duke
parents:
diff changeset
410 }
a61af66fc99e Initial load
duke
parents:
diff changeset
411
a61af66fc99e Initial load
duke
parents:
diff changeset
412 // ------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
413 // Functions below this point are not yet implemented. They are here only
a61af66fc99e Initial load
duke
parents:
diff changeset
414 // to make the linker happy.
a61af66fc99e Initial load
duke
parents:
diff changeset
415
a61af66fc99e Initial load
duke
parents:
diff changeset
416 ps_err_e ps_lsetfpregs(struct ps_prochandle *ph, lwpid_t lid, const prfpregset_t *fpregs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
417 print_debug("ps_lsetfpregs not implemented\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
418 return PS_OK;
a61af66fc99e Initial load
duke
parents:
diff changeset
419 }
a61af66fc99e Initial load
duke
parents:
diff changeset
420
a61af66fc99e Initial load
duke
parents:
diff changeset
421 ps_err_e ps_lsetregs(struct ps_prochandle *ph, lwpid_t lid, const prgregset_t gregset) {
a61af66fc99e Initial load
duke
parents:
diff changeset
422 print_debug("ps_lsetregs not implemented\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
423 return PS_OK;
a61af66fc99e Initial load
duke
parents:
diff changeset
424 }
a61af66fc99e Initial load
duke
parents:
diff changeset
425
a61af66fc99e Initial load
duke
parents:
diff changeset
426 ps_err_e ps_lgetfpregs(struct ps_prochandle *ph, lwpid_t lid, prfpregset_t *fpregs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
427 print_debug("ps_lgetfpregs not implemented\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
428 return PS_OK;
a61af66fc99e Initial load
duke
parents:
diff changeset
429 }
a61af66fc99e Initial load
duke
parents:
diff changeset
430
a61af66fc99e Initial load
duke
parents:
diff changeset
431 ps_err_e ps_lgetregs(struct ps_prochandle *ph, lwpid_t lid, prgregset_t gregset) {
a61af66fc99e Initial load
duke
parents:
diff changeset
432 print_debug("ps_lgetfpregs not implemented\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
433 return PS_OK;
a61af66fc99e Initial load
duke
parents:
diff changeset
434 }
a61af66fc99e Initial load
duke
parents:
diff changeset
435
a61af66fc99e Initial load
duke
parents:
diff changeset
436 // new libthread_db of NPTL seem to require this symbol
a61af66fc99e Initial load
duke
parents:
diff changeset
437 ps_err_e ps_get_thread_area() {
a61af66fc99e Initial load
duke
parents:
diff changeset
438 print_debug("ps_get_thread_area not implemented\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
439 return PS_OK;
a61af66fc99e Initial load
duke
parents:
diff changeset
440 }