1 /*
   2  * Copyright (c) 2003, 2020, 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_IMPL_H_
  26 #define _LIBPROC_IMPL_H_
  27 
  28 #include <unistd.h>
  29 #include <limits.h>
  30 #include "libproc.h"
  31 #include "symtab.h"
  32 
  33 // data structures in this file mimic those of Solaris 8.0 - libproc's Pcontrol.h
  34 
  35 #define BUF_SIZE     (PATH_MAX + NAME_MAX + 1)
  36 
  37 // .eh_frame data
  38 typedef struct eh_frame_info {
  39   uintptr_t library_base_addr;
  40   uintptr_t v_addr;
  41   unsigned char* data;
  42   int size;
  43 } eh_frame_info;
  44 
  45 // list of shared objects
  46 typedef struct lib_info {
  47   char             name[BUF_SIZE];
  48   uintptr_t        base;
  49   uintptr_t        exec_start;
  50   uintptr_t        exec_end;
  51   eh_frame_info    eh_frame;
  52   struct symtab*   symtab;
  53   int              fd;        // file descriptor for lib
  54   struct lib_info* next;
  55 } lib_info;
  56 
  57 // list of threads
  58 typedef struct thread_info {
  59    lwpid_t                  lwp_id;
  60    struct user_regs_struct  regs;       // not for process, core uses for caching regset
  61    struct thread_info*      next;
  62 } thread_info;
  63 
  64 // list of virtual memory maps
  65 typedef struct map_info {
  66    int              fd;       // file descriptor
  67    off_t            offset;   // file offset of this mapping
  68    uintptr_t        vaddr;    // starting virtual address
  69    size_t           memsz;    // size of the mapping
  70    uint32_t         flags;    // acces flags
  71    struct map_info* next;
  72 } map_info;
  73 
  74 // vtable for ps_prochandle
  75 typedef struct ps_prochandle_ops {
  76    // "derived class" clean-up
  77    void (*release)(struct ps_prochandle* ph);
  78    // read from debuggee
  79    bool (*p_pread)(struct ps_prochandle *ph,
  80             uintptr_t addr, char *buf, size_t size);
  81    // write into debuggee
  82    bool (*p_pwrite)(struct ps_prochandle *ph,
  83             uintptr_t addr, const char *buf , size_t size);
  84    // get integer regset of a thread
  85    bool (*get_lwp_regs)(struct ps_prochandle* ph, lwpid_t lwp_id, struct user_regs_struct* regs);
  86 } ps_prochandle_ops;
  87 
  88 // the ps_prochandle
  89 
  90 struct core_data {
  91    int                core_fd;   // file descriptor of core file
  92    int                exec_fd;   // file descriptor of exec file
  93    int                interp_fd; // file descriptor of interpreter (ld-linux.so.2)
  94    // part of the class sharing workaround
  95    int                classes_jsa_fd; // file descriptor of class share archive
  96    uintptr_t          dynamic_addr;  // address of dynamic section of a.out
  97    uintptr_t          ld_base_addr;  // base address of ld.so
  98    size_t             num_maps;  // number of maps.
  99    map_info*          maps;      // maps in a linked list
 100    // part of the class sharing workaround
 101    map_info*          class_share_maps;// class share maps in a linked list
 102    map_info**         map_array; // sorted (by vaddr) array of map_info pointers
 103 };
 104 
 105 struct ps_prochandle {
 106    ps_prochandle_ops* ops;       // vtable ptr
 107    pid_t              pid;
 108    int                num_libs;
 109    lib_info*          libs;      // head of lib list
 110    lib_info*          lib_tail;  // tail of lib list - to append at the end
 111    int                num_threads;
 112    thread_info*       threads;   // head of thread list
 113    struct core_data*  core;      // data only used for core dumps, NULL for process
 114 };
 115 
 116 #ifdef __cplusplus
 117 extern "C" {
 118 #endif
 119 
 120 int pathmap_open(const char* name);
 121 
 122 void print_debug(const char* format,...);
 123 void print_error(const char* format,...);
 124 bool is_debug();
 125 
 126 // deletes a thread from the thread list
 127 void delete_thread_info(struct ps_prochandle* ph, thread_info* thr);
 128 
 129 // adds a new shared object to lib list, returns NULL on failure
 130 lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base);
 131 
 132 // adds a new shared object to lib list, supply open lib file descriptor as well
 133 lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,
 134                           uintptr_t base);
 135 
 136 // adds a new thread to threads list, returns NULL on failure
 137 thread_info* add_thread_info(struct ps_prochandle* ph, lwpid_t lwp_id);
 138 
 139 // a test for ELF signature without using libelf
 140 bool is_elf_file(int fd);
 141 
 142 #ifdef __cplusplus
 143 }
 144 #endif
 145 
 146 #endif //_LIBPROC_IMPL_H_