Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/agent/src/os/linux/libproc_impl.c
38833 views
1
/*
2
* Copyright (c) 2003, 2019, 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
#ifndef __ANDROID__
30
#include <thread_db.h>
31
#else
32
#include "thread_db.h"
33
#endif
34
#include "libproc_impl.h"
35
36
#define SA_ALTROOT "SA_ALTROOT"
37
38
int pathmap_open(const char* name) {
39
static const char *alt_root = NULL;
40
static int alt_root_initialized = 0;
41
42
int fd;
43
char alt_path[PATH_MAX + 1], *alt_path_end;
44
const char *s;
45
46
if (!alt_root_initialized) {
47
alt_root_initialized = -1;
48
alt_root = getenv(SA_ALTROOT);
49
}
50
51
if (alt_root == NULL) {
52
return open(name, O_RDONLY);
53
}
54
55
strcpy(alt_path, alt_root);
56
alt_path_end = alt_path + strlen(alt_path);
57
58
// Strip path items one by one and try to open file with alt_root prepended
59
s = name;
60
while (1) {
61
strcat(alt_path, s);
62
s += 1;
63
64
fd = open(alt_path, O_RDONLY);
65
if (fd >= 0) {
66
print_debug("path %s substituted for %s\n", alt_path, name);
67
return fd;
68
}
69
70
// Linker always put full path to solib to process, so we can rely
71
// on presence of /. If slash is not present, it means, that SOlib doesn't
72
// physically exist (e.g. linux-gate.so) and we fail opening it anyway
73
if ((s = strchr(s, '/')) == NULL) {
74
break;
75
}
76
77
*alt_path_end = 0;
78
}
79
80
return -1;
81
}
82
83
static bool _libsaproc_debug;
84
85
void print_debug(const char* format,...) {
86
if (_libsaproc_debug) {
87
va_list alist;
88
89
va_start(alist, format);
90
fputs("libsaproc DEBUG: ", stderr);
91
vfprintf(stderr, format, alist);
92
va_end(alist);
93
}
94
}
95
96
void print_error(const char* format,...) {
97
va_list alist;
98
va_start(alist, format);
99
fputs("ERROR: ", stderr);
100
vfprintf(stderr, format, alist);
101
va_end(alist);
102
}
103
104
bool is_debug() {
105
return _libsaproc_debug;
106
}
107
108
// initialize libproc
109
bool init_libproc(bool debug) {
110
// init debug mode
111
_libsaproc_debug = debug;
112
113
// initialize the thread_db library
114
if (td_init() != TD_OK) {
115
print_debug("libthread_db's td_init failed\n");
116
return false;
117
}
118
119
return true;
120
}
121
122
static void destroy_lib_info(struct ps_prochandle* ph) {
123
lib_info* lib = ph->libs;
124
while (lib) {
125
lib_info *next = lib->next;
126
if (lib->symtab) {
127
destroy_symtab(lib->symtab);
128
}
129
free(lib);
130
lib = next;
131
}
132
}
133
134
static void destroy_thread_info(struct ps_prochandle* ph) {
135
thread_info* thr = ph->threads;
136
while (thr) {
137
thread_info *next = thr->next;
138
free(thr);
139
thr = next;
140
}
141
}
142
143
// ps_prochandle cleanup
144
145
// ps_prochandle cleanup
146
void Prelease(struct ps_prochandle* ph) {
147
// do the "derived class" clean-up first
148
ph->ops->release(ph);
149
destroy_lib_info(ph);
150
destroy_thread_info(ph);
151
free(ph);
152
}
153
154
lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base) {
155
return add_lib_info_fd(ph, libname, -1, base);
156
}
157
158
lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) {
159
lib_info* newlib;
160
161
if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) {
162
print_debug("can't allocate memory for lib_info\n");
163
return NULL;
164
}
165
166
if (strlen(libname) >= sizeof(newlib->name)) {
167
print_debug("libname %s too long\n", libname);
168
return NULL;
169
}
170
strcpy(newlib->name, libname);
171
172
newlib->base = base;
173
174
if (fd == -1) {
175
if ( (newlib->fd = pathmap_open(newlib->name)) < 0) {
176
print_debug("can't open shared object %s\n", newlib->name);
177
free(newlib);
178
return NULL;
179
}
180
} else {
181
newlib->fd = fd;
182
}
183
184
// check whether we have got an ELF file. /proc/<pid>/map
185
// gives out all file mappings and not just shared objects
186
if (is_elf_file(newlib->fd) == false) {
187
close(newlib->fd);
188
free(newlib);
189
return NULL;
190
}
191
192
newlib->symtab = build_symtab(newlib->fd, libname);
193
if (newlib->symtab == NULL) {
194
print_debug("symbol table build failed for %s\n", newlib->name);
195
}
196
197
// even if symbol table building fails, we add the lib_info.
198
// This is because we may need to read from the ELF file for core file
199
// address read functionality. lookup_symbol checks for NULL symtab.
200
if (ph->libs) {
201
ph->lib_tail->next = newlib;
202
ph->lib_tail = newlib;
203
} else {
204
ph->libs = ph->lib_tail = newlib;
205
}
206
ph->num_libs++;
207
208
return newlib;
209
}
210
211
// lookup for a specific symbol
212
uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name,
213
const char* sym_name) {
214
// ignore object_name. search in all libraries
215
// FIXME: what should we do with object_name?? The library names are obtained
216
// by parsing /proc/<pid>/maps, which may not be the same as object_name.
217
// What we need is a utility to map object_name to real file name, something
218
// dlopen() does by looking at LD_LIBRARY_PATH and /etc/ld.so.cache. For
219
// now, we just ignore object_name and do a global search for the symbol.
220
221
lib_info* lib = ph->libs;
222
while (lib) {
223
if (lib->symtab) {
224
uintptr_t res = search_symbol(lib->symtab, lib->base, sym_name, NULL);
225
if (res) return res;
226
}
227
lib = lib->next;
228
}
229
230
print_debug("lookup failed for symbol '%s' in obj '%s'\n",
231
sym_name, object_name);
232
return (uintptr_t) NULL;
233
}
234
235
236
const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) {
237
const char* res = NULL;
238
lib_info* lib = ph->libs;
239
while (lib) {
240
if (lib->symtab && addr >= lib->base) {
241
res = nearest_symbol(lib->symtab, addr - lib->base, poffset);
242
if (res) return res;
243
}
244
lib = lib->next;
245
}
246
return NULL;
247
}
248
249
// add a thread to ps_prochandle
250
thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
251
thread_info* newthr;
252
if ( (newthr = (thread_info*) calloc(1, sizeof(thread_info))) == NULL) {
253
print_debug("can't allocate memory for thread_info\n");
254
return NULL;
255
}
256
257
// initialize thread info
258
newthr->pthread_id = pthread_id;
259
newthr->lwp_id = lwp_id;
260
261
// add new thread to the list
262
newthr->next = ph->threads;
263
ph->threads = newthr;
264
ph->num_threads++;
265
return newthr;
266
}
267
268
void delete_thread_info(struct ps_prochandle* ph, thread_info* thr_to_be_removed) {
269
thread_info* current_thr = ph->threads;
270
271
if (thr_to_be_removed == ph->threads) {
272
ph->threads = ph->threads->next;
273
} else {
274
thread_info* previous_thr;
275
while (current_thr && current_thr != thr_to_be_removed) {
276
previous_thr = current_thr;
277
current_thr = current_thr->next;
278
}
279
if (current_thr == NULL) {
280
print_error("Could not find the thread to be removed");
281
return;
282
}
283
previous_thr->next = current_thr->next;
284
}
285
ph->num_threads--;
286
free(current_thr);
287
}
288
289
// struct used for client data from thread_db callback
290
struct thread_db_client_data {
291
struct ps_prochandle* ph;
292
thread_info_callback callback;
293
};
294
295
// callback function for libthread_db
296
static int thread_db_callback(const td_thrhandle_t *th_p, void *data) {
297
struct thread_db_client_data* ptr = (struct thread_db_client_data*) data;
298
td_thrinfo_t ti;
299
td_err_e err;
300
301
memset(&ti, 0, sizeof(ti));
302
err = td_thr_get_info(th_p, &ti);
303
if (err != TD_OK) {
304
print_debug("libthread_db : td_thr_get_info failed, can't get thread info\n");
305
return err;
306
}
307
308
print_debug("thread_db : pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid);
309
310
if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) {
311
print_debug("Skipping pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid);
312
return TD_OK;
313
}
314
315
if (ptr->callback(ptr->ph, ti.ti_tid, ti.ti_lid) != true)
316
return TD_ERR;
317
318
return TD_OK;
319
}
320
321
// read thread_info using libthread_db
322
bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb) {
323
struct thread_db_client_data mydata;
324
td_thragent_t* thread_agent = NULL;
325
if (td_ta_new(ph, &thread_agent) != TD_OK) {
326
print_debug("can't create libthread_db agent\n");
327
return false;
328
}
329
330
mydata.ph = ph;
331
mydata.callback = cb;
332
333
// we use libthread_db iterator to iterate thru list of threads.
334
if (td_ta_thr_iter(thread_agent, thread_db_callback, &mydata,
335
TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
336
TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS) != TD_OK) {
337
td_ta_delete(thread_agent);
338
return false;
339
}
340
341
// delete thread agent
342
td_ta_delete(thread_agent);
343
return true;
344
}
345
346
347
// get number of threads
348
int get_num_threads(struct ps_prochandle* ph) {
349
return ph->num_threads;
350
}
351
352
// get lwp_id of n'th thread
353
lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {
354
int count = 0;
355
thread_info* thr = ph->threads;
356
while (thr) {
357
if (count == index) {
358
return thr->lwp_id;
359
}
360
count++;
361
thr = thr->next;
362
}
363
return -1;
364
}
365
366
// get regs for a given lwp
367
bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct user_regs_struct* regs) {
368
return ph->ops->get_lwp_regs(ph, lwp_id, regs);
369
}
370
371
// get number of shared objects
372
int get_num_libs(struct ps_prochandle* ph) {
373
return ph->num_libs;
374
}
375
376
// get name of n'th solib
377
const char* get_lib_name(struct ps_prochandle* ph, int index) {
378
int count = 0;
379
lib_info* lib = ph->libs;
380
while (lib) {
381
if (count == index) {
382
return lib->name;
383
}
384
count++;
385
lib = lib->next;
386
}
387
return NULL;
388
}
389
390
// get base address of a lib
391
uintptr_t get_lib_base(struct ps_prochandle* ph, int index) {
392
int count = 0;
393
lib_info* lib = ph->libs;
394
while (lib) {
395
if (count == index) {
396
return lib->base;
397
}
398
count++;
399
lib = lib->next;
400
}
401
return (uintptr_t)NULL;
402
}
403
404
bool find_lib(struct ps_prochandle* ph, const char *lib_name) {
405
lib_info *p = ph->libs;
406
while (p) {
407
if (strcmp(p->name, lib_name) == 0) {
408
return true;
409
}
410
p = p->next;
411
}
412
return false;
413
}
414
415
//--------------------------------------------------------------------------
416
// proc service functions
417
418
// get process id
419
pid_t ps_getpid(struct ps_prochandle *ph) {
420
return ph->pid;
421
}
422
423
// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table
424
// of the load object object_name in the target process identified by ph.
425
// It returns the symbol's value as an address in the target process in
426
// *sym_addr.
427
428
ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,
429
const char *sym_name, psaddr_t *sym_addr) {
430
*sym_addr = (psaddr_t) lookup_symbol(ph, object_name, sym_name);
431
return (*sym_addr ? PS_OK : PS_NOSYM);
432
}
433
434
// read "size" bytes info "buf" from address "addr"
435
ps_err_e ps_pdread(struct ps_prochandle *ph, psaddr_t addr,
436
void *buf, size_t size) {
437
return ph->ops->p_pread(ph, (uintptr_t) addr, buf, size)? PS_OK: PS_ERR;
438
}
439
440
// write "size" bytes of data to debuggee at address "addr"
441
ps_err_e ps_pdwrite(struct ps_prochandle *ph, psaddr_t addr,
442
const void *buf, size_t size) {
443
return ph->ops->p_pwrite(ph, (uintptr_t)addr, buf, size)? PS_OK: PS_ERR;
444
}
445
446
// ------------------------------------------------------------------------
447
// Functions below this point are not yet implemented. They are here only
448
// to make the linker happy.
449
450
ps_err_e ps_lsetfpregs(struct ps_prochandle *ph, lwpid_t lid, const prfpregset_t *fpregs) {
451
print_debug("ps_lsetfpregs not implemented\n");
452
return PS_OK;
453
}
454
455
ps_err_e ps_lsetregs(struct ps_prochandle *ph, lwpid_t lid, const prgregset_t gregset) {
456
print_debug("ps_lsetregs not implemented\n");
457
return PS_OK;
458
}
459
460
ps_err_e ps_lgetfpregs(struct ps_prochandle *ph, lwpid_t lid, prfpregset_t *fpregs) {
461
print_debug("ps_lgetfpregs not implemented\n");
462
return PS_OK;
463
}
464
465
ps_err_e ps_lgetregs(struct ps_prochandle *ph, lwpid_t lid, prgregset_t gregset) {
466
print_debug("ps_lgetfpregs not implemented\n");
467
return PS_OK;
468
}
469
470
// new libthread_db of NPTL seem to require this symbol
471
ps_err_e ps_get_thread_area() {
472
print_debug("ps_get_thread_area not implemented\n");
473
return PS_OK;
474
}
475
476