Path: blob/master/src/jdk.hotspot.agent/macosx/native/libsaproc/libproc.h
41149 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef _LIBPROC_H_25#define _LIBPROC_H_2627#include <unistd.h>28#include <stdint.h>29#include <stdarg.h>30#include <stdio.h>31#include <stdlib.h>32#include <string.h>33#include <fcntl.h>3435#ifdef __APPLE__36typedef enum ps_err_e {37PS_OK, PS_ERR, PS_BADPID, PS_BADLID,38PS_BADADDR, PS_NOSYM, PS_NOFREGS39} ps_err_e;4041#ifndef psaddr_t42#define psaddr_t uintptr_t43#endif4445#ifndef bool46typedef int bool;47#define true 148#define false 049#endif // bool5051#ifndef lwpid_t52#define lwpid_t uintptr_t53#endif5455#include <mach/thread_status.h>56#else // __APPLE__57#include <elf.h>58#include <link.h>59#include <machine/reg.h>60#include <proc_service.h>6162// This C bool type must be int for compatibility with BSD calls and63// it would be a mistake to equivalence it to C++ bool on many platforms64typedef int bool;65#define true 166#define false 06768#endif // __APPLE__6970/************************************************************************************71720. This is very minimal subset of Solaris libproc just enough for current application.73Please note that the bulk of the functionality is from proc_service interface. This74adds Pgrab__ and some missing stuff. We hide the difference b/w live process and core75file by this interface.76771. pthread_id is unique. We store this in OSThread::_pthread_id in JVM code.78792. All threads see the same pid when they call getpid().80We used to save the result of ::getpid() call in OSThread::_thread_id.81Because gettid returns actual pid of thread (lwp id), this is82unique again. We therefore use OSThread::_thread_id as unique identifier.83843. There is a unique LWP id under both thread libraries. libthread_db maps pthread_id85to its underlying lwp_id under both the thread libraries. thread_info.lwp_id stores86lwp_id of the thread. The lwp id is nothing but the actual pid of clone'd processes. But87unfortunately libthread_db does not work very well for core dumps. So, we get pthread_id88only for processes. For core dumps, we don't use libthread_db at all (like gdb).89904. ptrace operates on this LWP id under both the thread libraries. When we say 'pid' for91ptrace call, we refer to lwp_id of the thread.92935. for core file, we parse ELF files and read data from them. For processes we use94combination of ptrace and /proc calls.9596*************************************************************************************/9798struct reg;99struct ps_prochandle;100101// attach to a process102struct ps_prochandle* Pgrab(pid_t pid);103104// attach to a core dump105struct ps_prochandle* Pgrab_core(const char* execfile, const char* corefile);106107// release a process or core108void Prelease(struct ps_prochandle* ph);109110// functions not directly available in Solaris libproc111112// initialize libproc (call this only once per app)113// pass true to make library verbose114bool init_libproc(bool verbose);115116// get number of threads117int get_num_threads(struct ps_prochandle* ph);118119// get lwp_id of n'th thread120lwpid_t get_lwp_id(struct ps_prochandle* ph, int index);121122// get regs for a given lwp123bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lid, struct reg* regs);124125// get number of shared objects126int get_num_libs(struct ps_prochandle* ph);127128// get name of n'th lib129const char* get_lib_name(struct ps_prochandle* ph, int index);130131// get base of lib132uintptr_t get_lib_base(struct ps_prochandle* ph, int index);133134// get address range of lib135void get_lib_addr_range(struct ps_prochandle* ph, int index, uintptr_t* base, uintptr_t* memsz);136137// returns true if given library is found in lib list138bool find_lib(struct ps_prochandle* ph, const char *lib_name);139140// symbol lookup141uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name,142const char* sym_name);143144// address->nearest symbol lookup. return NULL for no symbol145const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset);146147#endif //__LIBPROC_H_148149150