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 25 #ifndef _LIBPROC_H_ 26 #define _LIBPROC_H_ 27 28 #include <unistd.h> 29 #include <stdint.h> 30 #include <stdarg.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <fcntl.h> 35 36 #ifdef __APPLE__ 37 typedef enum ps_err_e { 38 PS_OK, PS_ERR, PS_BADPID, PS_BADLID, 39 PS_BADADDR, PS_NOSYM, PS_NOFREGS 40 } ps_err_e; 41 42 #ifndef psaddr_t 43 #define psaddr_t uintptr_t 44 #endif 45 46 #ifndef bool 47 typedef int bool; 48 #define true 1 49 #define false 0 50 #endif // bool 51 52 #ifndef lwpid_t 53 #define lwpid_t uintptr_t 54 #endif 55 56 #include <mach/thread_status.h> 57 #else // __APPLE__ 58 #include <elf.h> 59 #include <link.h> 60 #include <machine/reg.h> 61 #include <proc_service.h> 62 #if defined(sparc) || defined(sparcv9) 63 /* 64 If _LP64 is defined ptrace.h should be taken from /usr/include/asm-sparc64 65 otherwise it should be from /usr/include/asm-sparc 66 These two files define pt_regs structure differently 67 */ 68 #ifdef _LP64 69 #include "asm-sparc64/ptrace.h" 70 #else 71 #include "asm-sparc/ptrace.h" 72 #endif 73 74 #endif //sparc or sparcv9 75 76 // This C bool type must be int for compatibility with BSD calls and 77 // it would be a mistake to equivalence it to C++ bool on many platforms 78 typedef int bool; 79 #define true 1 80 #define false 0 81 82 #endif // __APPLE__ 83 84 /************************************************************************************ 85 86 0. This is very minimal subset of Solaris libproc just enough for current application. 87 Please note that the bulk of the functionality is from proc_service interface. This 88 adds Pgrab__ and some missing stuff. We hide the difference b/w live process and core 89 file by this interface. 90 91 1. pthread_id is unique. We store this in OSThread::_pthread_id in JVM code. 92 93 2. All threads see the same pid when they call getpid(). 94 We used to save the result of ::getpid() call in OSThread::_thread_id. 95 Because gettid returns actual pid of thread (lwp id), this is 96 unique again. We therefore use OSThread::_thread_id as unique identifier. 97 98 3. There is a unique LWP id under both thread libraries. libthread_db maps pthread_id 99 to its underlying lwp_id under both the thread libraries. thread_info.lwp_id stores 100 lwp_id of the thread. The lwp id is nothing but the actual pid of clone'd processes. But 101 unfortunately libthread_db does not work very well for core dumps. So, we get pthread_id 102 only for processes. For core dumps, we don't use libthread_db at all (like gdb). 103 104 4. ptrace operates on this LWP id under both the thread libraries. When we say 'pid' for 105 ptrace call, we refer to lwp_id of the thread. 106 107 5. for core file, we parse ELF files and read data from them. For processes we use 108 combination of ptrace and /proc calls. 109 110 *************************************************************************************/ 111 112 struct reg; 113 struct ps_prochandle; 114 115 // attach to a process 116 struct ps_prochandle* Pgrab(pid_t pid); 117 118 // attach to a core dump 119 struct ps_prochandle* Pgrab_core(const char* execfile, const char* corefile); 120 121 // release a process or core 122 void Prelease(struct ps_prochandle* ph); 123 124 // functions not directly available in Solaris libproc 125 126 // initialize libproc (call this only once per app) 127 // pass true to make library verbose 128 bool init_libproc(bool verbose); 129 130 // get number of threads 131 int get_num_threads(struct ps_prochandle* ph); 132 133 // get lwp_id of n'th thread 134 lwpid_t get_lwp_id(struct ps_prochandle* ph, int index); 135 136 // get regs for a given lwp 137 bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lid, struct reg* regs); 138 139 // get number of shared objects 140 int get_num_libs(struct ps_prochandle* ph); 141 142 // get name of n'th lib 143 const char* get_lib_name(struct ps_prochandle* ph, int index); 144 145 // get base of lib 146 uintptr_t get_lib_base(struct ps_prochandle* ph, int index); 147 148 // returns true if given library is found in lib list 149 bool find_lib(struct ps_prochandle* ph, const char *lib_name); 150 151 // symbol lookup 152 uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name, 153 const char* sym_name); 154 155 // address->nearest symbol lookup. return NULL for no symbol 156 const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset); 157 158 #endif //__LIBPROC_H_