Path: blob/master/src/jdk.hotspot.agent/macosx/native/libsaproc/libproc_impl.h
41149 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2021, Azul Systems, Inc. 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#ifndef _LIBPROC_IMPL_H_26#define _LIBPROC_IMPL_H_2728#include <unistd.h>29#include <limits.h>30#include "libproc.h"31#include "symtab.h"3233#define UNSUPPORTED_ARCH "Unsupported architecture!"3435#if defined(__x86_64__) && !defined(amd64)36#define amd64 137#endif3839#if defined(__arm64__) && !defined(aarch64)40#define aarch64 141#endif4243#ifdef __APPLE__44#include <inttypes.h> // for PRIx64, 32, ...45#include <pthread.h>46#include <mach-o/loader.h>47#include <mach-o/nlist.h>48#include <mach-o/fat.h>49#include <mach-o/stab.h>5051#ifndef register_t52#define register_t uint64_t53#endif5455#if defined(amd64)56/*** registers copied from bsd/amd64 */57typedef struct reg {58register_t r_r15;59register_t r_r14;60register_t r_r13;61register_t r_r12;62register_t r_r11;63register_t r_r10;64register_t r_r9;65register_t r_r8;66register_t r_rdi;67register_t r_rsi;68register_t r_rbp;69register_t r_rbx;70register_t r_rdx;71register_t r_rcx;72register_t r_rax;73uint32_t r_trapno; // not used74uint16_t r_fs;75uint16_t r_gs;76uint32_t r_err; // not used77uint16_t r_es; // not used78uint16_t r_ds; // not used79register_t r_rip;80register_t r_cs;81register_t r_rflags;82register_t r_rsp;83register_t r_ss; // not used84} reg;8586#elif defined(aarch64)87/*** registers copied from bsd/arm64 */88typedef struct reg {89register_t r_r0;90register_t r_r1;91register_t r_r2;92register_t r_r3;93register_t r_r4;94register_t r_r5;95register_t r_r6;96register_t r_r7;97register_t r_r8;98register_t r_r9;99register_t r_r10;100register_t r_r11;101register_t r_r12;102register_t r_r13;103register_t r_r14;104register_t r_r15;105register_t r_r16;106register_t r_r17;107register_t r_r18;108register_t r_r19;109register_t r_r20;110register_t r_r21;111register_t r_r22;112register_t r_r23;113register_t r_r24;114register_t r_r25;115register_t r_r26;116register_t r_r27;117register_t r_r28;118register_t r_fp;119register_t r_lr;120register_t r_sp;121register_t r_pc;122} reg;123124#else125#error UNSUPPORTED_ARCH126#endif127128// convenient defs129typedef struct mach_header_64 mach_header_64;130typedef struct load_command load_command;131typedef struct segment_command_64 segment_command_64;132typedef struct thread_command thread_command;133typedef struct dylib_command dylib_command;134typedef struct symtab_command symtab_command;135typedef struct nlist_64 nlist_64;136#else137#include <thread_db.h>138#include "salibelf.h"139#endif // __APPLE__140141// data structures in this file mimic those of Solaris 8.0 - libproc's Pcontrol.h142143#define BUF_SIZE (PATH_MAX + NAME_MAX + 1)144145// list of shared objects146typedef struct lib_info {147char name[BUF_SIZE];148uintptr_t base;149struct symtab* symtab;150int fd; // file descriptor for lib151struct lib_info* next;152size_t memsz;153} lib_info;154155// list of threads156typedef struct sa_thread_info {157lwpid_t lwp_id; // same as pthread_t158pthread_t pthread_id; //159struct reg regs; // not for process, core uses for caching regset160struct sa_thread_info* next;161} sa_thread_info;162163// list of virtual memory maps164typedef struct map_info {165int fd; // file descriptor166uint64_t offset; // file offset of this mapping167uint64_t vaddr; // starting virtual address168size_t memsz; // size of the mapping169uint32_t flags; // access flags170struct map_info* next;171} map_info;172173// vtable for ps_prochandle174typedef struct ps_prochandle_ops {175// "derived class" clean-up176void (*release)(struct ps_prochandle* ph);177// read from debuggee178bool (*p_pread)(struct ps_prochandle *ph,179uintptr_t addr, char *buf, size_t size);180// write into debuggee181bool (*p_pwrite)(struct ps_prochandle *ph,182uintptr_t addr, const char *buf , size_t size);183// get integer regset of a thread184bool (*get_lwp_regs)(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs);185// get info on thread186bool (*get_lwp_info)(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);187} ps_prochandle_ops;188189// the ps_prochandle190191struct core_data {192int core_fd; // file descriptor of core file193int exec_fd; // file descriptor of exec file194int interp_fd; // file descriptor of interpreter (ld-elf.so.1)195// part of the class sharing workaround196int classes_jsa_fd; // file descriptor of class share archive197uintptr_t dynamic_addr; // address of dynamic section of a.out198uintptr_t ld_base_addr; // base address of ld.so199size_t num_maps; // number of maps.200map_info* maps; // maps in a linked list201// part of the class sharing workaround202map_info* class_share_maps;// class share maps in a linked list203map_info** map_array; // sorted (by vaddr) array of map_info pointers204char exec_path[4096]; // file name java205};206207struct ps_prochandle {208ps_prochandle_ops* ops; // vtable ptr209pid_t pid;210int num_libs;211lib_info* libs; // head of lib list212lib_info* lib_tail; // tail of lib list - to append at the end213int num_threads;214sa_thread_info* threads; // head of thread list215struct core_data* core; // data only used for core dumps, NULL for process216};217218int pathmap_open(const char* name);219void print_debug(const char* format,...);220void print_error(const char* format,...);221bool is_debug();222223typedef bool (*thread_info_callback)(struct ps_prochandle* ph, pthread_t pid, lwpid_t lwpid);224225// reads thread info using libthread_db and calls above callback for each thread226bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb);227228// adds a new shared object to lib list, returns NULL on failure229lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base);230231// adds a new shared object to lib list, supply open lib file descriptor as well232lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base);233234sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id);235// a test for ELF signature without using libelf236237#ifdef __APPLE__238// a test for Mach-O signature239bool is_macho_file(int fd);240// skip fat head to get image start offset of cpu_type_t241// return false if any error happens, else value in offset.242bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset);243#else244bool is_elf_file(int fd);245#endif // __APPLE__246247lwpid_t get_lwp_id(struct ps_prochandle* ph, int index);248bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid);249bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs);250251// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table252// of the load object object_name in the target process identified by ph.253// It returns the symbol's value as an address in the target process in254// *sym_addr.255256ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,257const char *sym_name, psaddr_t *sym_addr);258259// read "size" bytes info "buf" from address "addr"260ps_err_e ps_pread(struct ps_prochandle *ph, psaddr_t addr,261void *buf, size_t size);262263// write "size" bytes of data to debuggee at address "addr"264ps_err_e ps_pwrite(struct ps_prochandle *ph, psaddr_t addr,265const void *buf, size_t size);266267// fill in ptrace_lwpinfo for lid268ps_err_e ps_linfo(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);269270// needed for when libthread_db is compiled with TD_DEBUG defined271void ps_plog (const char *format, ...);272273// untility, tells the position in file274off_t ltell(int fd);275#endif //_LIBPROC_IMPL_H_276277278