1 /* 2 * Copyright (c) 2003, 2022, 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 25 #ifndef _LIBPROC_H_ 26 #define _LIBPROC_H_ 27 28 #include <jni.h> 29 #include <unistd.h> 30 #include <stdint.h> 31 32 #include <sys/procfs.h> 33 #include <sys/ptrace.h> 34 35 36 #if defined(ppc64) || defined(ppc64le) 37 #include <asm/ptrace.h> 38 #define user_regs_struct pt_regs 39 #endif 40 #if defined(aarch64) || defined(arm64) 41 #include <asm/ptrace.h> 42 #define user_regs_struct user_pt_regs 43 #elif defined(arm) 44 #include <asm/ptrace.h> 45 #define user_regs_struct pt_regs 46 #elif defined(riscv64) 47 #include <asm/ptrace.h> 48 #endif 49 50 // This C bool type must be int for compatibility with Linux calls and 51 // it would be a mistake to equivalence it to C++ bool on many platforms 52 #ifndef __cplusplus 53 typedef int bool; 54 #define true 1 55 #define false 0 56 #endif 57 58 struct ps_prochandle; 59 struct lib_info; 60 61 #ifdef __cplusplus 62 extern "C" { 63 #endif 64 65 // attach to a process 66 JNIEXPORT struct ps_prochandle* JNICALL 67 Pgrab(pid_t pid, char* err_buf, size_t err_buf_len); 68 69 // attach to a core dump 70 JNIEXPORT struct ps_prochandle* JNICALL 71 Pgrab_core(const char* execfile, const char* corefile); 72 73 // release a process or core 74 JNIEXPORT void JNICALL 75 Prelease(struct ps_prochandle* ph); 76 77 // functions not directly available in Solaris libproc 78 79 // initialize libproc (call this only once per app) 80 // pass true to make library verbose 81 JNIEXPORT bool JNICALL 82 init_libproc(bool verbose); 83 84 // get number of threads 85 int get_num_threads(struct ps_prochandle* ph); 86 87 // get lwp_id of n'th thread 88 lwpid_t get_lwp_id(struct ps_prochandle* ph, int index); 89 90 // get regs for a given lwp 91 bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lid, struct user_regs_struct* regs); 92 93 // get number of shared objects 94 int get_num_libs(struct ps_prochandle* ph); 95 96 // get name of n'th lib 97 const char* get_lib_name(struct ps_prochandle* ph, int index); 98 99 // get base of lib 100 uintptr_t get_lib_base(struct ps_prochandle* ph, int index); 101 102 // get address range of lib 103 void get_lib_addr_range(struct ps_prochandle* ph, int index, uintptr_t* base, uintptr_t* memsz); 104 105 // returns true if given library is found in lib list 106 bool find_lib(struct ps_prochandle* ph, const char *lib_name); 107 108 // returns lib which contains pc 109 struct lib_info *find_lib_by_address(struct ps_prochandle* ph, uintptr_t pc); 110 111 // symbol lookup 112 uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name, 113 const char* sym_name); 114 115 // address->nearest symbol lookup. return NULL for no symbol 116 const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset); 117 118 struct ps_prochandle* get_proc_handle(JNIEnv* env, jobject this_obj); 119 120 void throw_new_debugger_exception(JNIEnv* env, const char* errMsg); 121 122 #ifdef __cplusplus 123 } 124 #endif 125 126 #endif //__LIBPROC_H_ --- EOF ---