1/*
2 * Copyright (c) 2003, 2013, 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#ifdef __APPLE__
34#include <inttypes.h>     // for PRIx64, 32, ...
35#include <pthread.h>
36#include <mach-o/loader.h>
37#include <mach-o/nlist.h>
38#include <mach-o/fat.h>
39
40#ifndef register_t
41#define register_t uint64_t
42#endif
43
44/*** registers copied from bsd/amd64 */
45typedef struct reg {
46  register_t      r_r15;
47  register_t      r_r14;
48  register_t      r_r13;
49  register_t      r_r12;
50  register_t      r_r11;
51  register_t      r_r10;
52  register_t      r_r9;
53  register_t      r_r8;
54  register_t      r_rdi;
55  register_t      r_rsi;
56  register_t      r_rbp;
57  register_t      r_rbx;
58  register_t      r_rdx;
59  register_t      r_rcx;
60  register_t      r_rax;
61  uint32_t        r_trapno;      // not used
62  uint16_t        r_fs;
63  uint16_t        r_gs;
64  uint32_t        r_err;         // not used
65  uint16_t        r_es;          // not used
66  uint16_t        r_ds;          // not used
67  register_t      r_rip;
68  register_t      r_cs;
69  register_t      r_rflags;
70  register_t      r_rsp;
71  register_t      r_ss;          // not used
72} reg;
73
74// convenient defs
75typedef struct mach_header_64 mach_header_64;
76typedef struct load_command load_command;
77typedef struct segment_command_64 segment_command_64;
78typedef struct thread_command thread_command;
79typedef struct dylib_command dylib_command;
80typedef struct symtab_command symtab_command;
81typedef struct nlist_64 nlist_64;
82#else
83#include <thread_db.h>
84#include "salibelf.h"
85#endif //  __APPLE__
86
87// data structures in this file mimic those of Solaris 8.0 - libproc's Pcontrol.h
88
89#define BUF_SIZE     (PATH_MAX + NAME_MAX + 1)
90
91// list of shared objects
92typedef struct lib_info {
93  char             name[BUF_SIZE];
94  uintptr_t        base;
95  struct symtab*   symtab;
96  int              fd;        // file descriptor for lib
97  struct lib_info* next;
98} lib_info;
99
100// list of threads
101typedef struct sa_thread_info {
102   lwpid_t                  lwp_id;     // same as pthread_t
103   pthread_t                pthread_id; //
104   struct reg               regs;       // not for process, core uses for caching regset
105   struct sa_thread_info*   next;
106} sa_thread_info;
107
108// list of virtual memory maps
109typedef struct map_info {
110   int              fd;       // file descriptor
111   off_t            offset;   // file offset of this mapping
112   uintptr_t        vaddr;    // starting virtual address
113   size_t           memsz;    // size of the mapping
114   struct map_info* next;
115} map_info;
116
117// vtable for ps_prochandle
118typedef struct ps_prochandle_ops {
119   // "derived class" clean-up
120   void (*release)(struct ps_prochandle* ph);
121   // read from debuggee
122   bool (*p_pread)(struct ps_prochandle *ph,
123            uintptr_t addr, char *buf, size_t size);
124   // write into debuggee
125   bool (*p_pwrite)(struct ps_prochandle *ph,
126            uintptr_t addr, const char *buf , size_t size);
127   // get integer regset of a thread
128   bool (*get_lwp_regs)(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs);
129   // get info on thread
130   bool (*get_lwp_info)(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);
131} ps_prochandle_ops;
132
133// the ps_prochandle
134
135struct core_data {
136   int                core_fd;   // file descriptor of core file
137   int                exec_fd;   // file descriptor of exec file
138   int                interp_fd; // file descriptor of interpreter (ld-elf.so.1)
139   // part of the class sharing workaround
140   int                classes_jsa_fd; // file descriptor of class share archive
141   uintptr_t          dynamic_addr;  // address of dynamic section of a.out
142   uintptr_t          ld_base_addr;  // base address of ld.so
143   size_t             num_maps;  // number of maps.
144   map_info*          maps;      // maps in a linked list
145   // part of the class sharing workaround
146   map_info*          class_share_maps;// class share maps in a linked list
147   map_info**         map_array; // sorted (by vaddr) array of map_info pointers
148   char               exec_path[4096];  // file name java
149};
150
151struct ps_prochandle {
152   ps_prochandle_ops* ops;       // vtable ptr
153   pid_t              pid;
154   int                num_libs;
155   lib_info*          libs;      // head of lib list
156   lib_info*          lib_tail;  // tail of lib list - to append at the end
157   int                num_threads;
158   sa_thread_info*    threads;   // head of thread list
159   struct core_data*  core;      // data only used for core dumps, NULL for process
160};
161
162int pathmap_open(const char* name);
163void print_debug(const char* format,...);
164void print_error(const char* format,...);
165bool is_debug();
166
167typedef bool (*thread_info_callback)(struct ps_prochandle* ph, pthread_t pid, lwpid_t lwpid);
168
169// reads thread info using libthread_db and calls above callback for each thread
170bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb);
171
172// adds a new shared object to lib list, returns NULL on failure
173lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base);
174
175// adds a new shared object to lib list, supply open lib file descriptor as well
176lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,
177                          uintptr_t base);
178
179sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id);
180// a test for ELF signature without using libelf
181
182#ifdef __APPLE__
183// a test for Mach-O signature
184bool is_macho_file(int fd);
185// skip fat head to get image start offset of cpu_type_t
186// return false if any error happens, else value in offset.
187bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset);
188#else
189bool is_elf_file(int fd);
190#endif // __APPLE__
191
192lwpid_t get_lwp_id(struct ps_prochandle* ph, int index);
193bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid);
194bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs);
195
196// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table
197// of the load object object_name in the target process identified by ph.
198// It returns the symbol's value as an address in the target process in
199// *sym_addr.
200
201ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,
202                    const char *sym_name, psaddr_t *sym_addr);
203
204// read "size" bytes info "buf" from address "addr"
205ps_err_e ps_pread(struct ps_prochandle *ph, psaddr_t  addr,
206                  void *buf, size_t size);
207
208// write "size" bytes of data to debuggee at address "addr"
209ps_err_e ps_pwrite(struct ps_prochandle *ph, psaddr_t addr,
210                   const void *buf, size_t size);
211
212// fill in ptrace_lwpinfo for lid
213ps_err_e ps_linfo(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);
214
215// needed for when libthread_db is compiled with TD_DEBUG defined
216void ps_plog (const char *format, ...);
217
218// untility, tells the position in file
219off_t ltell(int fd);
220#endif //_LIBPROC_IMPL_H_
221