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