1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/agent/src/os/linux/libproc.h Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,142 @@ 1.4 +/* 1.5 + * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef _LIBPROC_H_ 1.29 +#define _LIBPROC_H_ 1.30 + 1.31 +#include <unistd.h> 1.32 +#include <stdint.h> 1.33 +#include "proc_service.h" 1.34 + 1.35 +#if defined(sparc) || defined(sparcv9) 1.36 +/* 1.37 + If _LP64 is defined ptrace.h should be taken from /usr/include/asm-sparc64 1.38 + otherwise it should be from /usr/include/asm-sparc 1.39 + These two files define pt_regs structure differently 1.40 +*/ 1.41 +#ifdef _LP64 1.42 +#include "asm-sparc64/ptrace.h" 1.43 +#else 1.44 +#include "asm-sparc/ptrace.h" 1.45 +#endif 1.46 + 1.47 +#endif //sparc or sparcv9 1.48 + 1.49 +/************************************************************************************ 1.50 + 1.51 +0. This is very minimal subset of Solaris libproc just enough for current application. 1.52 +Please note that the bulk of the functionality is from proc_service interface. This 1.53 +adds Pgrab__ and some missing stuff. We hide the difference b/w live process and core 1.54 +file by this interface. 1.55 + 1.56 +1. pthread_id unique in both NPTL & LinuxThreads. We store this in 1.57 +OSThread::_pthread_id in JVM code. 1.58 + 1.59 +2. All threads see the same pid when they call getpid() under NPTL. 1.60 +Threads receive different pid under LinuxThreads. We used to save the result of 1.61 +::getpid() call in OSThread::_thread_id. This way uniqueness of OSThread::_thread_id 1.62 +was lost under NPTL. Now, we store the result of ::gettid() call in 1.63 +OSThread::_thread_id. Because gettid returns actual pid of thread (lwp id), this is 1.64 +unique again. We therefore use OSThread::_thread_id as unique identifier. 1.65 + 1.66 +3. There is a unique LWP id under both thread libraries. libthread_db maps pthread_id 1.67 +to its underlying lwp_id under both the thread libraries. thread_info.lwp_id stores 1.68 +lwp_id of the thread. The lwp id is nothing but the actual pid of clone'd processes. But 1.69 +unfortunately libthread_db does not work very well for core dumps. So, we get pthread_id 1.70 +only for processes. For core dumps, we don't use libthread_db at all (like gdb). 1.71 + 1.72 +4. ptrace operates on this LWP id under both the thread libraries. When we say 'pid' for 1.73 +ptrace call, we refer to lwp_id of the thread. 1.74 + 1.75 +5. for core file, we parse ELF files and read data from them. For processes we use 1.76 +combination of ptrace and /proc calls. 1.77 + 1.78 +*************************************************************************************/ 1.79 + 1.80 +#ifdef ia64 1.81 +struct user_regs_struct { 1.82 +/* copied from user.h which doesn't define this in a struct */ 1.83 + 1.84 +#define IA64_REG_COUNT (EF_SIZE/8+32) /* integer and fp regs */ 1.85 +unsigned long regs[IA64_REG_COUNT]; /* integer and fp regs */ 1.86 +}; 1.87 +#endif 1.88 + 1.89 +#if defined(sparc) || defined(sparcv9) 1.90 +#define user_regs_struct pt_regs 1.91 +#endif 1.92 + 1.93 +// This C bool type must be int for compatibility with Linux calls and 1.94 +// it would be a mistake to equivalence it to C++ bool on many platforms 1.95 + 1.96 +typedef int bool; 1.97 +#define true 1 1.98 +#define false 0 1.99 + 1.100 +struct ps_prochandle; 1.101 + 1.102 +// attach to a process 1.103 +struct ps_prochandle* Pgrab(pid_t pid); 1.104 + 1.105 +// attach to a core dump 1.106 +struct ps_prochandle* Pgrab_core(const char* execfile, const char* corefile); 1.107 + 1.108 +// release a process or core 1.109 +void Prelease(struct ps_prochandle* ph); 1.110 + 1.111 +// functions not directly available in Solaris libproc 1.112 + 1.113 +// initialize libproc (call this only once per app) 1.114 +// pass true to make library verbose 1.115 +bool init_libproc(bool verbose); 1.116 + 1.117 +// get number of threads 1.118 +int get_num_threads(struct ps_prochandle* ph); 1.119 + 1.120 +// get lwp_id of n'th thread 1.121 +lwpid_t get_lwp_id(struct ps_prochandle* ph, int index); 1.122 + 1.123 +// get regs for a given lwp 1.124 +bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lid, struct user_regs_struct* regs); 1.125 + 1.126 +// get number of shared objects 1.127 +int get_num_libs(struct ps_prochandle* ph); 1.128 + 1.129 +// get name of n'th lib 1.130 +const char* get_lib_name(struct ps_prochandle* ph, int index); 1.131 + 1.132 +// get base of lib 1.133 +uintptr_t get_lib_base(struct ps_prochandle* ph, int index); 1.134 + 1.135 +// returns true if given library is found in lib list 1.136 +bool find_lib(struct ps_prochandle* ph, const char *lib_name); 1.137 + 1.138 +// symbol lookup 1.139 +uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name, 1.140 + const char* sym_name); 1.141 + 1.142 +// address->nearest symbol lookup. return NULL for no symbol 1.143 +const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset); 1.144 + 1.145 +#endif //__LIBPROC_H_