Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.hotspot.agent/macosx/native/libsaproc/libproc_impl.h
41149 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
#include <mach-o/stab.h>
51
52
#ifndef register_t
53
#define register_t uint64_t
54
#endif
55
56
#if defined(amd64)
57
/*** registers copied from bsd/amd64 */
58
typedef struct reg {
59
register_t r_r15;
60
register_t r_r14;
61
register_t r_r13;
62
register_t r_r12;
63
register_t r_r11;
64
register_t r_r10;
65
register_t r_r9;
66
register_t r_r8;
67
register_t r_rdi;
68
register_t r_rsi;
69
register_t r_rbp;
70
register_t r_rbx;
71
register_t r_rdx;
72
register_t r_rcx;
73
register_t r_rax;
74
uint32_t r_trapno; // not used
75
uint16_t r_fs;
76
uint16_t r_gs;
77
uint32_t r_err; // not used
78
uint16_t r_es; // not used
79
uint16_t r_ds; // not used
80
register_t r_rip;
81
register_t r_cs;
82
register_t r_rflags;
83
register_t r_rsp;
84
register_t r_ss; // not used
85
} reg;
86
87
#elif defined(aarch64)
88
/*** registers copied from bsd/arm64 */
89
typedef struct reg {
90
register_t r_r0;
91
register_t r_r1;
92
register_t r_r2;
93
register_t r_r3;
94
register_t r_r4;
95
register_t r_r5;
96
register_t r_r6;
97
register_t r_r7;
98
register_t r_r8;
99
register_t r_r9;
100
register_t r_r10;
101
register_t r_r11;
102
register_t r_r12;
103
register_t r_r13;
104
register_t r_r14;
105
register_t r_r15;
106
register_t r_r16;
107
register_t r_r17;
108
register_t r_r18;
109
register_t r_r19;
110
register_t r_r20;
111
register_t r_r21;
112
register_t r_r22;
113
register_t r_r23;
114
register_t r_r24;
115
register_t r_r25;
116
register_t r_r26;
117
register_t r_r27;
118
register_t r_r28;
119
register_t r_fp;
120
register_t r_lr;
121
register_t r_sp;
122
register_t r_pc;
123
} reg;
124
125
#else
126
#error UNSUPPORTED_ARCH
127
#endif
128
129
// convenient defs
130
typedef struct mach_header_64 mach_header_64;
131
typedef struct load_command load_command;
132
typedef struct segment_command_64 segment_command_64;
133
typedef struct thread_command thread_command;
134
typedef struct dylib_command dylib_command;
135
typedef struct symtab_command symtab_command;
136
typedef struct nlist_64 nlist_64;
137
#else
138
#include <thread_db.h>
139
#include "salibelf.h"
140
#endif // __APPLE__
141
142
// data structures in this file mimic those of Solaris 8.0 - libproc's Pcontrol.h
143
144
#define BUF_SIZE (PATH_MAX + NAME_MAX + 1)
145
146
// list of shared objects
147
typedef struct lib_info {
148
char name[BUF_SIZE];
149
uintptr_t base;
150
struct symtab* symtab;
151
int fd; // file descriptor for lib
152
struct lib_info* next;
153
size_t memsz;
154
} lib_info;
155
156
// list of threads
157
typedef struct sa_thread_info {
158
lwpid_t lwp_id; // same as pthread_t
159
pthread_t pthread_id; //
160
struct reg regs; // not for process, core uses for caching regset
161
struct sa_thread_info* next;
162
} sa_thread_info;
163
164
// list of virtual memory maps
165
typedef struct map_info {
166
int fd; // file descriptor
167
uint64_t offset; // file offset of this mapping
168
uint64_t vaddr; // starting virtual address
169
size_t memsz; // size of the mapping
170
uint32_t flags; // access flags
171
struct map_info* next;
172
} map_info;
173
174
// vtable for ps_prochandle
175
typedef struct ps_prochandle_ops {
176
// "derived class" clean-up
177
void (*release)(struct ps_prochandle* ph);
178
// read from debuggee
179
bool (*p_pread)(struct ps_prochandle *ph,
180
uintptr_t addr, char *buf, size_t size);
181
// write into debuggee
182
bool (*p_pwrite)(struct ps_prochandle *ph,
183
uintptr_t addr, const char *buf , size_t size);
184
// get integer regset of a thread
185
bool (*get_lwp_regs)(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs);
186
// get info on thread
187
bool (*get_lwp_info)(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);
188
} ps_prochandle_ops;
189
190
// the ps_prochandle
191
192
struct core_data {
193
int core_fd; // file descriptor of core file
194
int exec_fd; // file descriptor of exec file
195
int interp_fd; // file descriptor of interpreter (ld-elf.so.1)
196
// part of the class sharing workaround
197
int classes_jsa_fd; // file descriptor of class share archive
198
uintptr_t dynamic_addr; // address of dynamic section of a.out
199
uintptr_t ld_base_addr; // base address of ld.so
200
size_t num_maps; // number of maps.
201
map_info* maps; // maps in a linked list
202
// part of the class sharing workaround
203
map_info* class_share_maps;// class share maps in a linked list
204
map_info** map_array; // sorted (by vaddr) array of map_info pointers
205
char exec_path[4096]; // file name java
206
};
207
208
struct ps_prochandle {
209
ps_prochandle_ops* ops; // vtable ptr
210
pid_t pid;
211
int num_libs;
212
lib_info* libs; // head of lib list
213
lib_info* lib_tail; // tail of lib list - to append at the end
214
int num_threads;
215
sa_thread_info* threads; // head of thread list
216
struct core_data* core; // data only used for core dumps, NULL for process
217
};
218
219
int pathmap_open(const char* name);
220
void print_debug(const char* format,...);
221
void print_error(const char* format,...);
222
bool is_debug();
223
224
typedef bool (*thread_info_callback)(struct ps_prochandle* ph, pthread_t pid, lwpid_t lwpid);
225
226
// reads thread info using libthread_db and calls above callback for each thread
227
bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb);
228
229
// adds a new shared object to lib list, returns NULL on failure
230
lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base);
231
232
// adds a new shared object to lib list, supply open lib file descriptor as well
233
lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base);
234
235
sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id);
236
// a test for ELF signature without using libelf
237
238
#ifdef __APPLE__
239
// a test for Mach-O signature
240
bool is_macho_file(int fd);
241
// skip fat head to get image start offset of cpu_type_t
242
// return false if any error happens, else value in offset.
243
bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset);
244
#else
245
bool is_elf_file(int fd);
246
#endif // __APPLE__
247
248
lwpid_t get_lwp_id(struct ps_prochandle* ph, int index);
249
bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid);
250
bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs);
251
252
// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table
253
// of the load object object_name in the target process identified by ph.
254
// It returns the symbol's value as an address in the target process in
255
// *sym_addr.
256
257
ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,
258
const char *sym_name, psaddr_t *sym_addr);
259
260
// read "size" bytes info "buf" from address "addr"
261
ps_err_e ps_pread(struct ps_prochandle *ph, psaddr_t addr,
262
void *buf, size_t size);
263
264
// write "size" bytes of data to debuggee at address "addr"
265
ps_err_e ps_pwrite(struct ps_prochandle *ph, psaddr_t addr,
266
const void *buf, size_t size);
267
268
// fill in ptrace_lwpinfo for lid
269
ps_err_e ps_linfo(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);
270
271
// needed for when libthread_db is compiled with TD_DEBUG defined
272
void ps_plog (const char *format, ...);
273
274
// untility, tells the position in file
275
off_t ltell(int fd);
276
#endif //_LIBPROC_IMPL_H_
277
278