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/bsd/libproc_impl.h
38833 views
1
/*
2
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*
24
*/
25
26
#ifndef _LIBPROC_IMPL_H_
27
#define _LIBPROC_IMPL_H_
28
29
#include <unistd.h>
30
#include <limits.h>
31
#include "libproc.h"
32
#include "symtab.h"
33
34
#define UNSUPPORTED_ARCH "Unsupported architecture!"
35
36
#if defined(__x86_64__) && !defined(amd64)
37
#define amd64 1
38
#endif
39
40
#if defined(__arm64__) && !defined(aarch64)
41
#define aarch64 1
42
#endif
43
44
#ifdef __APPLE__
45
#include <inttypes.h> // for PRIx64, 32, ...
46
#include <pthread.h>
47
#include <mach-o/loader.h>
48
#include <mach-o/nlist.h>
49
#include <mach-o/fat.h>
50
51
#ifndef register_t
52
#define register_t uint64_t
53
#endif
54
55
#if defined(amd64)
56
/*** registers copied from bsd/amd64 */
57
typedef struct reg {
58
register_t r_r15;
59
register_t r_r14;
60
register_t r_r13;
61
register_t r_r12;
62
register_t r_r11;
63
register_t r_r10;
64
register_t r_r9;
65
register_t r_r8;
66
register_t r_rdi;
67
register_t r_rsi;
68
register_t r_rbp;
69
register_t r_rbx;
70
register_t r_rdx;
71
register_t r_rcx;
72
register_t r_rax;
73
uint32_t r_trapno; // not used
74
uint16_t r_fs;
75
uint16_t r_gs;
76
uint32_t r_err; // not used
77
uint16_t r_es; // not used
78
uint16_t r_ds; // not used
79
register_t r_rip;
80
register_t r_cs;
81
register_t r_rflags;
82
register_t r_rsp;
83
register_t r_ss; // not used
84
} reg;
85
86
#elif defined(aarch64)
87
/*** registers copied from bsd/arm64 */
88
typedef struct reg {
89
register_t r_r0;
90
register_t r_r1;
91
register_t r_r2;
92
register_t r_r3;
93
register_t r_r4;
94
register_t r_r5;
95
register_t r_r6;
96
register_t r_r7;
97
register_t r_r8;
98
register_t r_r9;
99
register_t r_r10;
100
register_t r_r11;
101
register_t r_r12;
102
register_t r_r13;
103
register_t r_r14;
104
register_t r_r15;
105
register_t r_r16;
106
register_t r_r17;
107
register_t r_r18;
108
register_t r_r19;
109
register_t r_r20;
110
register_t r_r21;
111
register_t r_r22;
112
register_t r_r23;
113
register_t r_r24;
114
register_t r_r25;
115
register_t r_r26;
116
register_t r_r27;
117
register_t r_r28;
118
register_t r_fp;
119
register_t r_lr;
120
register_t r_sp;
121
register_t r_pc;
122
} reg;
123
124
#else
125
#error UNSUPPORTED_ARCH
126
#endif
127
128
// convenient defs
129
typedef struct mach_header_64 mach_header_64;
130
typedef struct load_command load_command;
131
typedef struct segment_command_64 segment_command_64;
132
typedef struct thread_command thread_command;
133
typedef struct dylib_command dylib_command;
134
typedef struct symtab_command symtab_command;
135
typedef struct nlist_64 nlist_64;
136
#else
137
#include <thread_db.h>
138
#include "salibelf.h"
139
#endif // __APPLE__
140
141
// data structures in this file mimic those of Solaris 8.0 - libproc's Pcontrol.h
142
143
#define BUF_SIZE (PATH_MAX + NAME_MAX + 1)
144
145
// list of shared objects
146
typedef struct lib_info {
147
char name[BUF_SIZE];
148
uintptr_t base;
149
struct symtab* symtab;
150
int fd; // file descriptor for lib
151
struct lib_info* next;
152
} lib_info;
153
154
// list of threads
155
typedef struct sa_thread_info {
156
lwpid_t lwp_id; // same as pthread_t
157
pthread_t pthread_id; //
158
struct reg regs; // not for process, core uses for caching regset
159
struct sa_thread_info* next;
160
} sa_thread_info;
161
162
// list of virtual memory maps
163
typedef struct map_info {
164
int fd; // file descriptor
165
off_t offset; // file offset of this mapping
166
uintptr_t vaddr; // starting virtual address
167
size_t memsz; // size of the mapping
168
struct map_info* next;
169
} map_info;
170
171
// vtable for ps_prochandle
172
typedef struct ps_prochandle_ops {
173
// "derived class" clean-up
174
void (*release)(struct ps_prochandle* ph);
175
// read from debuggee
176
bool (*p_pread)(struct ps_prochandle *ph,
177
uintptr_t addr, char *buf, size_t size);
178
// write into debuggee
179
bool (*p_pwrite)(struct ps_prochandle *ph,
180
uintptr_t addr, const char *buf , size_t size);
181
// get integer regset of a thread
182
bool (*get_lwp_regs)(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs);
183
// get info on thread
184
bool (*get_lwp_info)(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);
185
} ps_prochandle_ops;
186
187
// the ps_prochandle
188
189
struct core_data {
190
int core_fd; // file descriptor of core file
191
int exec_fd; // file descriptor of exec file
192
int interp_fd; // file descriptor of interpreter (ld-elf.so.1)
193
// part of the class sharing workaround
194
int classes_jsa_fd; // file descriptor of class share archive
195
uintptr_t dynamic_addr; // address of dynamic section of a.out
196
uintptr_t ld_base_addr; // base address of ld.so
197
size_t num_maps; // number of maps.
198
map_info* maps; // maps in a linked list
199
// part of the class sharing workaround
200
map_info* class_share_maps;// class share maps in a linked list
201
map_info** map_array; // sorted (by vaddr) array of map_info pointers
202
char exec_path[4096]; // file name java
203
};
204
205
struct ps_prochandle {
206
ps_prochandle_ops* ops; // vtable ptr
207
pid_t pid;
208
int num_libs;
209
lib_info* libs; // head of lib list
210
lib_info* lib_tail; // tail of lib list - to append at the end
211
int num_threads;
212
sa_thread_info* threads; // head of thread list
213
struct core_data* core; // data only used for core dumps, NULL for process
214
};
215
216
int pathmap_open(const char* name);
217
void print_debug(const char* format,...);
218
void print_error(const char* format,...);
219
bool is_debug();
220
221
typedef bool (*thread_info_callback)(struct ps_prochandle* ph, pthread_t pid, lwpid_t lwpid);
222
223
// reads thread info using libthread_db and calls above callback for each thread
224
bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb);
225
226
// adds a new shared object to lib list, returns NULL on failure
227
lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base);
228
229
// adds a new shared object to lib list, supply open lib file descriptor as well
230
lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,
231
uintptr_t base);
232
233
sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id);
234
// a test for ELF signature without using libelf
235
236
#ifdef __APPLE__
237
// a test for Mach-O signature
238
bool is_macho_file(int fd);
239
// skip fat head to get image start offset of cpu_type_t
240
// return false if any error happens, else value in offset.
241
bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset);
242
#else
243
bool is_elf_file(int fd);
244
#endif // __APPLE__
245
246
lwpid_t get_lwp_id(struct ps_prochandle* ph, int index);
247
bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid);
248
bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs);
249
250
// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table
251
// of the load object object_name in the target process identified by ph.
252
// It returns the symbol's value as an address in the target process in
253
// *sym_addr.
254
255
ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,
256
const char *sym_name, psaddr_t *sym_addr);
257
258
// read "size" bytes info "buf" from address "addr"
259
ps_err_e ps_pread(struct ps_prochandle *ph, psaddr_t addr,
260
void *buf, size_t size);
261
262
// write "size" bytes of data to debuggee at address "addr"
263
ps_err_e ps_pwrite(struct ps_prochandle *ph, psaddr_t addr,
264
const void *buf, size_t size);
265
266
// fill in ptrace_lwpinfo for lid
267
ps_err_e ps_linfo(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);
268
269
// needed for when libthread_db is compiled with TD_DEBUG defined
270
void ps_plog (const char *format, ...);
271
272
// untility, tells the position in file
273
off_t ltell(int fd);
274
#endif //_LIBPROC_IMPL_H_
275
276