Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/agent/src/os/bsd/libproc_impl.h
48792 views
/*1* Copyright (c) 2003, 2013, 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_IMPL_H_25#define _LIBPROC_IMPL_H_2627#include <unistd.h>28#include <limits.h>29#include "libproc.h"30#include "symtab.h"3132#ifdef __APPLE__33#include <inttypes.h> // for PRIx64, 32, ...34#include <pthread.h>35#include <mach-o/loader.h>36#include <mach-o/nlist.h>37#include <mach-o/fat.h>3839#ifndef register_t40#define register_t uint64_t41#endif4243/*** registers copied from bsd/amd64 */44typedef struct reg {45register_t r_r15;46register_t r_r14;47register_t r_r13;48register_t r_r12;49register_t r_r11;50register_t r_r10;51register_t r_r9;52register_t r_r8;53register_t r_rdi;54register_t r_rsi;55register_t r_rbp;56register_t r_rbx;57register_t r_rdx;58register_t r_rcx;59register_t r_rax;60uint32_t r_trapno; // not used61uint16_t r_fs;62uint16_t r_gs;63uint32_t r_err; // not used64uint16_t r_es; // not used65uint16_t r_ds; // not used66register_t r_rip;67register_t r_cs;68register_t r_rflags;69register_t r_rsp;70register_t r_ss; // not used71} reg;7273// convenient defs74typedef struct mach_header_64 mach_header_64;75typedef struct load_command load_command;76typedef struct segment_command_64 segment_command_64;77typedef struct thread_command thread_command;78typedef struct dylib_command dylib_command;79typedef struct symtab_command symtab_command;80typedef struct nlist_64 nlist_64;81#else82#include <thread_db.h>83#include "salibelf.h"84#endif // __APPLE__8586// data structures in this file mimic those of Solaris 8.0 - libproc's Pcontrol.h8788#define BUF_SIZE (PATH_MAX + NAME_MAX + 1)8990// list of shared objects91typedef struct lib_info {92char name[BUF_SIZE];93uintptr_t base;94struct symtab* symtab;95int fd; // file descriptor for lib96struct lib_info* next;97} lib_info;9899// list of threads100typedef struct sa_thread_info {101lwpid_t lwp_id; // same as pthread_t102pthread_t pthread_id; //103struct reg regs; // not for process, core uses for caching regset104struct sa_thread_info* next;105} sa_thread_info;106107// list of virtual memory maps108typedef struct map_info {109int fd; // file descriptor110off_t offset; // file offset of this mapping111uintptr_t vaddr; // starting virtual address112size_t memsz; // size of the mapping113struct map_info* next;114} map_info;115116// vtable for ps_prochandle117typedef struct ps_prochandle_ops {118// "derived class" clean-up119void (*release)(struct ps_prochandle* ph);120// read from debuggee121bool (*p_pread)(struct ps_prochandle *ph,122uintptr_t addr, char *buf, size_t size);123// write into debuggee124bool (*p_pwrite)(struct ps_prochandle *ph,125uintptr_t addr, const char *buf , size_t size);126// get integer regset of a thread127bool (*get_lwp_regs)(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs);128// get info on thread129bool (*get_lwp_info)(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);130} ps_prochandle_ops;131132// the ps_prochandle133134struct core_data {135int core_fd; // file descriptor of core file136int exec_fd; // file descriptor of exec file137int interp_fd; // file descriptor of interpreter (ld-elf.so.1)138// part of the class sharing workaround139int classes_jsa_fd; // file descriptor of class share archive140uintptr_t dynamic_addr; // address of dynamic section of a.out141uintptr_t ld_base_addr; // base address of ld.so142size_t num_maps; // number of maps.143map_info* maps; // maps in a linked list144// part of the class sharing workaround145map_info* class_share_maps;// class share maps in a linked list146map_info** map_array; // sorted (by vaddr) array of map_info pointers147char exec_path[4096]; // file name java148};149150struct ps_prochandle {151ps_prochandle_ops* ops; // vtable ptr152pid_t pid;153int num_libs;154lib_info* libs; // head of lib list155lib_info* lib_tail; // tail of lib list - to append at the end156int num_threads;157sa_thread_info* threads; // head of thread list158struct core_data* core; // data only used for core dumps, NULL for process159};160161int pathmap_open(const char* name);162void print_debug(const char* format,...);163void print_error(const char* format,...);164bool is_debug();165166typedef bool (*thread_info_callback)(struct ps_prochandle* ph, pthread_t pid, lwpid_t lwpid);167168// reads thread info using libthread_db and calls above callback for each thread169bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb);170171// adds a new shared object to lib list, returns NULL on failure172lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base);173174// adds a new shared object to lib list, supply open lib file descriptor as well175lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,176uintptr_t base);177178sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id);179// a test for ELF signature without using libelf180181#ifdef __APPLE__182// a test for Mach-O signature183bool is_macho_file(int fd);184// skip fat head to get image start offset of cpu_type_t185// return false if any error happens, else value in offset.186bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset);187#else188bool is_elf_file(int fd);189#endif // __APPLE__190191lwpid_t get_lwp_id(struct ps_prochandle* ph, int index);192bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid);193bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs);194195// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table196// of the load object object_name in the target process identified by ph.197// It returns the symbol's value as an address in the target process in198// *sym_addr.199200ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,201const char *sym_name, psaddr_t *sym_addr);202203// read "size" bytes info "buf" from address "addr"204ps_err_e ps_pread(struct ps_prochandle *ph, psaddr_t addr,205void *buf, size_t size);206207// write "size" bytes of data to debuggee at address "addr"208ps_err_e ps_pwrite(struct ps_prochandle *ph, psaddr_t addr,209const void *buf, size_t size);210211// fill in ptrace_lwpinfo for lid212ps_err_e ps_linfo(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);213214// needed for when libthread_db is compiled with TD_DEBUG defined215void ps_plog (const char *format, ...);216217// untility, tells the position in file218off_t ltell(int fd);219#endif //_LIBPROC_IMPL_H_220221222