1 /*
   2  * Copyright (c) 2003, 2005, 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 // list of shared objects
  38 typedef struct lib_info {
  39   char             name[BUF_SIZE];
  40   uintptr_t        base;
  41   struct symtab*   symtab;
  42   int              fd;        // file descriptor for lib
  43   struct lib_info* next;
  44 } lib_info;
  45 
  46 // list of threads
  47 typedef struct thread_info {
  48    lwpid_t                  lwp_id;
  49    pthread_t                pthread_id; // not used cores, always -1
  50    struct reg               regs;       // not for process, core uses for caching regset
  51    struct thread_info*      next;
  52 } thread_info;
  53 
  54 // list of virtual memory maps
  55 typedef struct map_info {
  56    int              fd;       // file descriptor
  57    off_t            offset;   // file offset of this mapping
  58    uintptr_t        vaddr;    // starting virtual address
  59    size_t           memsz;    // size of the mapping
  60    struct map_info* next;
  61 } map_info;
  62 
  63 // vtable for ps_prochandle
  64 typedef struct ps_prochandle_ops {
  65    // "derived class" clean-up
  66    void (*release)(struct ps_prochandle* ph);
  67    // read from debuggee
  68    bool (*p_pread)(struct ps_prochandle *ph,
  69             uintptr_t addr, char *buf, size_t size);
  70    // write into debuggee
  71    bool (*p_pwrite)(struct ps_prochandle *ph,
  72             uintptr_t addr, const char *buf , size_t size);
  73    // get integer regset of a thread
  74    bool (*get_lwp_regs)(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs);
  75    // get info on thread
  76    bool (*get_lwp_info)(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);
  77 } ps_prochandle_ops;
  78 
  79 // the ps_prochandle
  80 
  81 struct core_data {
  82    int                core_fd;   // file descriptor of core file
  83    int                exec_fd;   // file descriptor of exec file
  84    int                interp_fd; // file descriptor of interpreter (ld-elf.so.1)
  85    // part of the class sharing workaround
  86    int                classes_jsa_fd; // file descriptor of class share archive
  87    uintptr_t          dynamic_addr;  // address of dynamic section of a.out
  88    uintptr_t          ld_base_addr;  // base address of ld.so
  89    size_t             num_maps;  // number of maps.
  90    map_info*          maps;      // maps in a linked list
  91    // part of the class sharing workaround
  92    map_info*          class_share_maps;// class share maps in a linked list
  93    map_info**         map_array; // sorted (by vaddr) array of map_info pointers
  94 };
  95 
  96 struct ps_prochandle {
  97    ps_prochandle_ops* ops;       // vtable ptr
  98    pid_t              pid;
  99    int                num_libs;
 100    lib_info*          libs;      // head of lib list
 101    lib_info*          lib_tail;  // tail of lib list - to append at the end
 102    int                num_threads;
 103    thread_info*       threads;   // head of thread list
 104    struct core_data*  core;      // data only used for core dumps, NULL for process
 105 };
 106 
 107 int pathmap_open(const char* name);
 108 
 109 void print_debug(const char* format,...);
 110 bool is_debug();
 111 
 112 typedef bool (*thread_info_callback)(struct ps_prochandle* ph, pthread_t pid, lwpid_t lwpid);
 113 
 114 // reads thread info using libthread_db and calls above callback for each thread
 115 bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb);
 116 
 117 // adds a new shared object to lib list, returns NULL on failure
 118 lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base);
 119 
 120 // adds a new shared object to lib list, supply open lib file descriptor as well
 121 lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,
 122                           uintptr_t base);
 123 
 124 // adds a new thread to threads list, returns NULL on failure
 125 thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id);
 126 
 127 // a test for ELF signature without using libelf
 128 bool is_elf_file(int fd);
 129 
 130 #endif //_LIBPROC_IMPL_H_