Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/agent/src/os/bsd/libproc_impl.h
38833 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>4950#ifndef register_t51#define register_t uint64_t52#endif5354#if defined(amd64)55/*** registers copied from bsd/amd64 */56typedef struct reg {57register_t r_r15;58register_t r_r14;59register_t r_r13;60register_t r_r12;61register_t r_r11;62register_t r_r10;63register_t r_r9;64register_t r_r8;65register_t r_rdi;66register_t r_rsi;67register_t r_rbp;68register_t r_rbx;69register_t r_rdx;70register_t r_rcx;71register_t r_rax;72uint32_t r_trapno; // not used73uint16_t r_fs;74uint16_t r_gs;75uint32_t r_err; // not used76uint16_t r_es; // not used77uint16_t r_ds; // not used78register_t r_rip;79register_t r_cs;80register_t r_rflags;81register_t r_rsp;82register_t r_ss; // not used83} reg;8485#elif defined(aarch64)86/*** registers copied from bsd/arm64 */87typedef struct reg {88register_t r_r0;89register_t r_r1;90register_t r_r2;91register_t r_r3;92register_t r_r4;93register_t r_r5;94register_t r_r6;95register_t r_r7;96register_t r_r8;97register_t r_r9;98register_t r_r10;99register_t r_r11;100register_t r_r12;101register_t r_r13;102register_t r_r14;103register_t r_r15;104register_t r_r16;105register_t r_r17;106register_t r_r18;107register_t r_r19;108register_t r_r20;109register_t r_r21;110register_t r_r22;111register_t r_r23;112register_t r_r24;113register_t r_r25;114register_t r_r26;115register_t r_r27;116register_t r_r28;117register_t r_fp;118register_t r_lr;119register_t r_sp;120register_t r_pc;121} reg;122123#else124#error UNSUPPORTED_ARCH125#endif126127// convenient defs128typedef struct mach_header_64 mach_header_64;129typedef struct load_command load_command;130typedef struct segment_command_64 segment_command_64;131typedef struct thread_command thread_command;132typedef struct dylib_command dylib_command;133typedef struct symtab_command symtab_command;134typedef struct nlist_64 nlist_64;135#else136#include <thread_db.h>137#include "salibelf.h"138#endif // __APPLE__139140// data structures in this file mimic those of Solaris 8.0 - libproc's Pcontrol.h141142#define BUF_SIZE (PATH_MAX + NAME_MAX + 1)143144// list of shared objects145typedef struct lib_info {146char name[BUF_SIZE];147uintptr_t base;148struct symtab* symtab;149int fd; // file descriptor for lib150struct lib_info* next;151} lib_info;152153// list of threads154typedef struct sa_thread_info {155lwpid_t lwp_id; // same as pthread_t156pthread_t pthread_id; //157struct reg regs; // not for process, core uses for caching regset158struct sa_thread_info* next;159} sa_thread_info;160161// list of virtual memory maps162typedef struct map_info {163int fd; // file descriptor164off_t offset; // file offset of this mapping165uintptr_t vaddr; // starting virtual address166size_t memsz; // size of the mapping167struct map_info* next;168} map_info;169170// vtable for ps_prochandle171typedef struct ps_prochandle_ops {172// "derived class" clean-up173void (*release)(struct ps_prochandle* ph);174// read from debuggee175bool (*p_pread)(struct ps_prochandle *ph,176uintptr_t addr, char *buf, size_t size);177// write into debuggee178bool (*p_pwrite)(struct ps_prochandle *ph,179uintptr_t addr, const char *buf , size_t size);180// get integer regset of a thread181bool (*get_lwp_regs)(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs);182// get info on thread183bool (*get_lwp_info)(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);184} ps_prochandle_ops;185186// the ps_prochandle187188struct core_data {189int core_fd; // file descriptor of core file190int exec_fd; // file descriptor of exec file191int interp_fd; // file descriptor of interpreter (ld-elf.so.1)192// part of the class sharing workaround193int classes_jsa_fd; // file descriptor of class share archive194uintptr_t dynamic_addr; // address of dynamic section of a.out195uintptr_t ld_base_addr; // base address of ld.so196size_t num_maps; // number of maps.197map_info* maps; // maps in a linked list198// part of the class sharing workaround199map_info* class_share_maps;// class share maps in a linked list200map_info** map_array; // sorted (by vaddr) array of map_info pointers201char exec_path[4096]; // file name java202};203204struct ps_prochandle {205ps_prochandle_ops* ops; // vtable ptr206pid_t pid;207int num_libs;208lib_info* libs; // head of lib list209lib_info* lib_tail; // tail of lib list - to append at the end210int num_threads;211sa_thread_info* threads; // head of thread list212struct core_data* core; // data only used for core dumps, NULL for process213};214215int pathmap_open(const char* name);216void print_debug(const char* format,...);217void print_error(const char* format,...);218bool is_debug();219220typedef bool (*thread_info_callback)(struct ps_prochandle* ph, pthread_t pid, lwpid_t lwpid);221222// reads thread info using libthread_db and calls above callback for each thread223bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb);224225// adds a new shared object to lib list, returns NULL on failure226lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base);227228// adds a new shared object to lib list, supply open lib file descriptor as well229lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,230uintptr_t base);231232sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id);233// a test for ELF signature without using libelf234235#ifdef __APPLE__236// a test for Mach-O signature237bool is_macho_file(int fd);238// skip fat head to get image start offset of cpu_type_t239// return false if any error happens, else value in offset.240bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset);241#else242bool is_elf_file(int fd);243#endif // __APPLE__244245lwpid_t get_lwp_id(struct ps_prochandle* ph, int index);246bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid);247bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs);248249// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table250// of the load object object_name in the target process identified by ph.251// It returns the symbol's value as an address in the target process in252// *sym_addr.253254ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,255const char *sym_name, psaddr_t *sym_addr);256257// read "size" bytes info "buf" from address "addr"258ps_err_e ps_pread(struct ps_prochandle *ph, psaddr_t addr,259void *buf, size_t size);260261// write "size" bytes of data to debuggee at address "addr"262ps_err_e ps_pwrite(struct ps_prochandle *ph, psaddr_t addr,263const void *buf, size_t size);264265// fill in ptrace_lwpinfo for lid266ps_err_e ps_linfo(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);267268// needed for when libthread_db is compiled with TD_DEBUG defined269void ps_plog (const char *format, ...);270271// untility, tells the position in file272off_t ltell(int fd);273#endif //_LIBPROC_IMPL_H_274275276