1 /*
   2  * Copyright (c) 2003, 2015, 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 #include "proc_service.h"
  32 
  33 #ifdef ALT_SASRCDIR
  34 #include "libproc_md.h"
  35 #endif
  36 
  37 #include <sys/ptrace.h>
  38 
  39 /************************************************************************************
  40 
  41 0. This is very minimal subset of Solaris libproc just enough for current application.
  42 Please note that the bulk of the functionality is from proc_service interface. This
  43 adds Pgrab__ and some missing stuff. We hide the difference b/w live process and core
  44 file by this interface.
  45 
  46 1. For historical reasons there are both pthread id (stored in OSThread::_pthread_id)
  47 and kernel thread id (gettid() syscall, stored in OSThread::_thread_id) kept in OSThread.
  48 Both are unique. We use OSThread::_thread_id as unique identifier.
  49 
  50 2. There is a unique LWP id under both thread libraries. libthread_db  maps pthread_id
  51 to its underlying lwp_id under both the thread libraries. thread_info.lwp_id stores
  52 lwp_id of the thread. The lwp id is nothing but the actual pid of clone'd processes. But
  53 unfortunately libthread_db does not work very well for core dumps. So, we get pthread_id
  54 only for processes. For core dumps, we don't use libthread_db at all (like gdb).
  55 
  56 3. ptrace operates on this LWP id under both the thread libraries. When we say 'pid' for
  57 ptrace call, we refer to lwp_id of the thread.
  58 
  59 4. for core file, we parse ELF files and read data from them. For processes we  use
  60 combination of ptrace and /proc calls.
  61 
  62 *************************************************************************************/
  63 
  64 
  65 #if defined(sparc) || defined(sparcv9) || defined(ppc64)
  66 #define user_regs_struct  pt_regs
  67 #endif
  68 #if defined(aarch64)
  69 #define user_regs_struct user_pt_regs
  70 #endif
  71 
  72 // This C bool type must be int for compatibility with Linux calls and
  73 // it would be a mistake to equivalence it to C++ bool on many platforms
  74 
  75 typedef int bool;
  76 #define true  1
  77 #define false 0
  78 
  79 struct ps_prochandle;
  80 
  81 // attach to a process
  82 struct ps_prochandle* Pgrab(pid_t pid);
  83 
  84 // attach to a core dump
  85 struct ps_prochandle* Pgrab_core(const char* execfile, const char* corefile);
  86 
  87 // release a process or core
  88 void Prelease(struct ps_prochandle* ph);
  89 
  90 // functions not directly available in Solaris libproc
  91 
  92 // initialize libproc (call this only once per app)
  93 // pass true to make library verbose
  94 bool init_libproc(bool verbose);
  95 
  96 // get number of threads
  97 int get_num_threads(struct ps_prochandle* ph);
  98 
  99 // get lwp_id of n'th thread
 100 lwpid_t get_lwp_id(struct ps_prochandle* ph, int index);
 101 
 102 // get regs for a given lwp
 103 bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lid, struct user_regs_struct* regs);
 104 
 105 // get number of shared objects
 106 int get_num_libs(struct ps_prochandle* ph);
 107 
 108 // get name of n'th lib
 109 const char* get_lib_name(struct ps_prochandle* ph, int index);
 110 
 111 // get base of lib
 112 uintptr_t get_lib_base(struct ps_prochandle* ph, int index);
 113 
 114 // returns true if given library is found in lib list
 115 bool find_lib(struct ps_prochandle* ph, const char *lib_name);
 116 
 117 // symbol lookup
 118 uintptr_t lookup_symbol(struct ps_prochandle* ph,  const char* object_name,
 119                        const char* sym_name);
 120 
 121 // address->nearest symbol lookup. return NULL for no symbol
 122 const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset);
 123 
 124 struct ps_prochandle* get_proc_handle(JNIEnv* env, jobject this_obj);
 125 
 126 void throw_new_debugger_exception(JNIEnv* env, const char* errMsg);
 127 
 128 #endif //__LIBPROC_H_