Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
target_os.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, 2014 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #ifndef __TARGET_OS_H__
20 #define __TARGET_OS_H__
21 
22 #include <glib.h>
23 
24 #include "target_api.h"
25 #include "target_process.h"
26 
27 typedef enum {
31 
32 #define THREAD_CTXT_KERNEL 0
33 #define THREAD_CTXT_USER 1
34 
36  /*
37  * If this is a stub or wrapper, this is set.
38  */
39  uint8_t isstub:1;
40  int num;
42  struct bsymbol *bsymbol;
43  /*
44  * Some OSes have a stub/wrapper that calls the actual syscall
45  * function, and the stub is the thing actually *in* the syscall
46  * table. If we found a wrapped one, this is that symbol.
47  */
48  struct bsymbol *wrapped_bsymbol;
49  /*
50  * If @bsymbol, this is the list of struct symbol *s returned by
51  * symbol_get_members().
52  */
53  GSList *args;
54 };
55 
57  unsigned int returned:1;
59  /* A list of REGVALs. */
61  /* A list of struct value *s loaded from REGVALs according to @syscall. */
64 };
65 
66 #define SAFE_TARGET_OS_OP(target,op,errval,...) \
67  do { \
68  if (target->personality != TARGET_PERSONALITY_OS) { \
69  verror("target %s is not an OS!\n",target->name); \
70  errno = EINVAL; \
71  return (errval); \
72  } \
73  else if (!target->os_ops || !target->os_ops->op) { \
74  verror("target %s does not support OS operation '%s'!\n", \
75  target->name,#op); \
76  errno = ENOSYS; \
77  return (errval); \
78  } \
79  else { \
80  return target->os_ops->op(__VA_ARGS__); \
81  } \
82  } while (0);
83 
84 #define SAFE_TARGET_OS_OP_NORET(target,op,errval,outvar,...) \
85  do { \
86  if (target->personality != TARGET_PERSONALITY_OS) { \
87  verror("target %s is not an OS!\n",target->name); \
88  errno = EINVAL; \
89  outvar = (errval); \
90  } \
91  else if (!target->os_ops || !target->os_ops->op) { \
92  verror("target %s does not support OS operation '%s'!\n", \
93  target->name,#op); \
94  errno = ENOSYS; \
95  outvar = (errval); \
96  } \
97  else { \
98  outvar = target->os_ops->op(__VA_ARGS__); \
99  } \
100  } while (0);
101 
102 
104 
105 uint64_t target_os_version(struct target *target);
106 int target_os_version_cmp(struct target *target,uint64_t vers);
107 
108 int target_os_thread_get_pgd_phys(struct target *target,tid_t tid,ADDR *pgdp);
111 
112 /*
113  * These handle the case where the VM catches debug exceptions *for* the
114  * OS, or where it does not -- i.e., where the OS personality has to
115  * emulate exceptions for the OS Process driver.
116  *
117  * We hacked the Xen hypervisor to catch all debug exceptions, whether
118  * they happen in userspace or kernel space (instead of just the kernel
119  * space ones); but if that hack is not available, or if the user is
120  * using a different hypervisor (i.e., QEMU/KVM); then the OS
121  * personality attached to the hypervisor driver must emulate the
122  * exceptions by snatching them away from the OS. If the personality
123  * can do this,
124  */
125 int target_os_thread_singlestep(struct target *target,tid_t tid,int isbp,
126  struct target *overlay,int force_emulate);
128  struct target *overlay,int force_emulate);
130  thread_ctxt_t tidctxt,
131  struct target_memmod *mmod);
133  thread_ctxt_t tidctxt,
134  struct target_memmod *mmod);
135 
136 GHashTable *target_os_process_table_get(struct target *target);
138 
140  int signo,void *data);
141 const char *target_os_signal_to_name(struct target *target,int signo);
142 int target_os_signal_from_name(struct target *target,const char *name);
143 
146 GHashTable *target_os_syscall_table_get(struct target *target);
149  char *name);
151  int num);
153  ADDR addr);
154 int target_os_syscall_table_reload(struct target *target,int force);
156 
158  struct target_os_syscall *syscall,
161  void *handler_data);
165  void *handler_data);
167  tid_t tid);
168 
169 /* These can be used in os personality implementations. */
172 
173 /*
174  * The intent here is to provide a generic interface to common OS-level
175  * abstractions.
176  */
178  int (*init)(struct target *target);
179  int (*fini)(struct target *target);
180 
181  /*
182  * Version info.
183  */
185  uint64_t (*os_version)(struct target *target);
186  char *(*os_version_string)(struct target *target);
187  int (*os_version_cmp)(struct target *target,uint64_t vers);
188 
189  /*
190  * Threads.
191  */
192  int (*thread_get_pgd_phys)(struct target *target,
193  struct target_thread *tthread,ADDR *pgdp);
194  int (*thread_is_user)(struct target *target,struct target_thread *tthread);
195  struct target_thread *(*thread_get_leader)(struct target *target,
196  struct target_thread *tthread);
197 
198 
199  int (*thread_singlestep)(struct target *target,tid_t tid,int isbp,
200  struct target *overlay,int force_emulate);
201  int (*thread_singlestep_end)(struct target *target,tid_t tid,
202  struct target *overlay,int force_emulate);
203 
204  /*
205  * Processes.
206  */
207  GHashTable *(*processes_get)(struct target *target);
208  struct target_process *(*process_get)(struct target *target,
209  struct target_thread *tthread);
210 
211  /*
212  * Signals.
213  */
214  const char *(*signal_to_name)(struct target *target,int signo);
215  int (*signal_from_name)(struct target *target,const char *name);
216  int (*signal_enqueue)(struct target *target,struct target_thread *tthread,
217  int signo,void *data);
218  int (*signal_dequeue)(struct target *target,struct target_thread *tthread,
219  int signo);
220  int (*signal_get_mask)(struct target *target,struct target_thread *tthread,
221  unsigned char **maskbytes,int *masklen);
222  int (*signal_set_mask)(struct target *target,struct target_thread *tthread,
223  unsigned char *maskbytes,int masklen);
224 
225  /*
226  * Syscalls.
227  */
228  /* Mandatory -- if supply one, supply all. */
229  int (*syscall_table_load)(struct target *target);
230  int (*syscall_table_unload)(struct target *target);
231  GHashTable *(*syscall_table_get)(struct target *target);
232  struct target_os_syscall *(*syscall_lookup_name)(struct target *target,
233  char *name);
234  struct target_os_syscall *(*syscall_lookup_num)(struct target *target,
235  int num);
236  struct target_os_syscall *(*syscall_lookup_addr)(struct target *target,
237  ADDR addr);
238  /* Optional. */
239  int (*syscall_table_reload)(struct target *target,int force);
240  int (*syscall_table_store)(struct target *target);
241 
242  /*
243  * Syscall probing.
244  *
245  * The probes returned here *must* be of type
246  * target_os_syscall_probe; and they are not autofree. This probe
247  * type tracks syscall state, and probe_summarize_tid() will return
248  * the current/last syscall state for that thread. State should be
249  * cleared when a new syscall is hit.
250  *
251  * When a user gets one of these probes, and registers one of their
252  * probes on it, their pre_handler will be called whenever a syscall
253  * is entered (either directly at the function itself; or on the
254  * general syscall entry path); and their post_handler will be
255  * called when the system returns (depending on the backend, this
256  * could be either the syscall function's RET, or on the
257  * SYSRET/SYSEXIT/IRET from kernel space into userland).
258  *
259  * Syscall probes should always fire handlers when the CPU is in the
260  * kernel. This means that if the backend places a probe on a
261  * SYSRET or IRET, only the pre-handler should be used.
262  *
263  * (This is here because there is no other better place for it.
264  * Syscall probing is fundamentally a per-OS thing because of the
265  * complexities of system entry/exit. For instance, some syscall
266  * function bodies in the Linux kernel don't return directly (i.e.,
267  * stub_execve just JMPs to sys_execve after doing some stuff) -- so
268  * we can't depend on being able to just probe the function entry
269  * instruction and any of its RETs. Also, there might be
270  * opportunities to combine _probe and _probe_all (suppose user
271  * requests a probe of mmap, and then requests a probe of all -- for
272  * the kernel, the all probe makes the mmap probe irrelevant).
273  * Anyway, for now, syscall probing is in here.)
274  */
275  struct probe *(*syscall_probe)(struct target *target,tid_t tid,
276  struct target_os_syscall *syscall,
279  void *handler_data);
280  struct probe *(*syscall_probe_all)(struct target *target,tid_t tid,
283  void *handler_data);
284 };
285 
287 
288 /*
289  * Helper functions for backend builders.
290  */
291 
293  int no_event_send);
294 
297  struct target_os_syscall *syscall);
298 /*
299  * If either record_argv or record_return fail, the caller must free the
300  * values; otherwise, it must *not* free the values, nor ever use them
301  * again!
302  */
304  struct array_list *regvals,
305  struct array_list *argvals);
307  REGVAL retval);
309 
310 #endif /* __TARGET_OS_H__ */
struct target_os_syscall_state * target_os_syscall_probe_last(struct target *target, tid_t tid)
Definition: target_os.c:531
int target_os_syscall_table_reload(struct target *target, int force)
Definition: target_os.c:455
int target_os_syscall_table_unload(struct target *target)
Definition: target_os.c:420
GHashTable * target_os_process_table_get(struct target *target)
Definition: target_os.c:388
result_t pre_handler(struct probe *probe, tid_t tid, void *data, struct probe *trigger, struct probe *base)
Definition: spf.c:903
struct probe * target_os_syscall_probe_all(struct target *target, tid_t tid, probe_handler_t pre_handler, probe_handler_t post_handler, void *handler_data)
Definition: target_os.c:474
GSList * args
Definition: target_os.h:53
int(* signal_from_name)(struct target *target, const char *name)
Definition: target_os.h:215
uint8_t isstub
Definition: target_os.h:39
int32_t tid_t
Definition: common.h:36
int target_os_syscall_record_argv(struct target *target, tid_t tid, struct array_list *regvals, struct array_list *argvals)
Definition: target_os.c:562
int(* thread_singlestep)(struct target *target, tid_t tid, int isbp, struct target *overlay, int force_emulate)
Definition: target_os.h:199
struct target_process * target_os_process_get(struct target *target, tid_t tid)
Definition: target_os.c:392
int target_os_syscall_table_get_max_num(struct target *target)
Definition: target_os.c:430
int(* os_version_cmp)(struct target *target, uint64_t vers)
Definition: target_os.h:187
int(* syscall_table_reload)(struct target *target, int force)
Definition: target_os.h:239
struct array_list * argvals
Definition: target_os.h:62
int target_os_syscall_record_return(struct target *target, tid_t tid, REGVAL retval)
Definition: target_os.c:580
int(* syscall_table_unload)(struct target *target)
Definition: target_os.h:230
int(* fini)(struct target *target)
Definition: target_os.h:179
uint64_t(* os_version)(struct target *target)
Definition: target_os.h:185
struct probe_ops target_os_syscall_ret_probe_ops
Definition: target_os.c:499
int(* signal_get_mask)(struct target *target, struct target_thread *tthread, unsigned char **maskbytes, int *masklen)
Definition: target_os.h:220
struct target_os_syscall * target_os_syscall_lookup_name(struct target *target, char *name)
Definition: target_os.c:437
void * target_os_syscall_probe_summarize_tid(struct probe *probe, tid_t tid)
Definition: target_os.c:495
int(* thread_singlestep_end)(struct target *target, tid_t tid, struct target *overlay, int force_emulate)
Definition: target_os.h:201
result_t(* probe_handler_t)(struct probe *probe, tid_t tid, void *handler_data, struct probe *trigger, struct probe *base)
Definition: probe_api.h:70
target_os_type_t
Definition: target_os.h:27
struct target_os_syscall_state * target_os_syscall_record_entry(struct target *target, tid_t tid, struct target_os_syscall *syscall)
Definition: target_os.c:543
tid_t tid
Definition: probe.h:344
int(* syscall_table_load)(struct target *target)
Definition: target_os.h:229
int(* thread_is_user)(struct target *target, struct target_thread *tthread)
Definition: target_os.h:194
struct bsymbol * wrapped_bsymbol
Definition: target_os.h:48
int target_os_thread_singlestep_end(struct target *target, tid_t tid, struct target *overlay, int force_emulate)
Definition: target_os.c:79
struct target_os_syscall * syscall
Definition: target_os.h:58
int target_os_thread_get_pgd_phys(struct target *target, tid_t tid, ADDR *pgdp)
Definition: target_os.c:48
int(* signal_enqueue)(struct target *target, struct target_thread *tthread, int signo, void *data)
Definition: target_os.h:216
Definition: probe.h:308
void * target_os_syscall_probe_summarize(struct probe *probe)
Definition: target_os.c:491
struct target_os_syscall * target_os_syscall_lookup_num(struct target *target, int num)
Definition: target_os.c:443
result_t target_os_emulate_bp_handler(struct target *target, tid_t tid, thread_ctxt_t tidctxt, struct target_memmod *mmod)
Definition: target_os.c:85
const char * target_os_signal_to_name(struct target *target, int signo)
Definition: target_os.c:407
int(* thread_get_pgd_phys)(struct target *target, struct target_thread *tthread, ADDR *pgdp)
Definition: target_os.h:192
int(* signal_set_mask)(struct target *target, struct target_thread *tthread, unsigned char *maskbytes, int masklen)
Definition: target_os.h:222
target_os_type_t target_os_type(struct target *target)
Definition: target_os.c:33
target_os_type_t(* os_type)(struct target *target)
Definition: target_os.h:184
int(* syscall_table_store)(struct target *target)
Definition: target_os.h:240
GHashTable * target_os_syscall_table_get(struct target *target)
Definition: target_os.c:425
result_t post_handler(struct probe *probe, tid_t tid, void *data, struct probe *trigger, struct probe *base)
Definition: spf.c:908
int target_os_update_process_threads_generic(struct target_process *process, int no_event_send)
Definition: target_os.c:211
unsigned int thread_ctxt_t
Definition: target_api.h:300
result_t
Definition: common.h:25
int target_os_thread_is_user(struct target *target, tid_t tid)
Definition: target_os.c:55
uint32_t REGVAL
Definition: common.h:66
result_t target_os_emulate_ss_handler(struct target *target, tid_t tid, thread_ctxt_t tidctxt, struct target_memmod *mmod)
Definition: target_os.c:163
int target_os_signal_from_name(struct target *target, const char *name)
Definition: target_os.c:411
uint32_t ADDR
Definition: common.h:64
char * name
Definition: target_api.h:2521
int(* init)(struct target *target)
Definition: target_os.h:178
int(* signal_dequeue)(struct target *target, struct target_thread *tthread, int signo)
Definition: target_os.h:218
int target_os_thread_singlestep(struct target *target, tid_t tid, int isbp, struct target *overlay, int force_emulate)
Definition: target_os.c:73
struct target_os_syscall * target_os_syscall_lookup_addr(struct target *target, ADDR addr)
Definition: target_os.c:449
tid_t target_os_thread_get_leader(struct target *target, tid_t tid)
Definition: target_os.c:62
uint64_t target_os_version(struct target *target)
Definition: target_os.c:38
int target_os_version_cmp(struct target *target, uint64_t vers)
Definition: target_os.c:43
struct array_list * regvals
Definition: target_os.h:60
struct bsymbol * bsymbol
Definition: target_os.h:42
int target_os_syscall_table_store(struct target *target)
Definition: target_os.c:460
unsigned int returned
Definition: target_os.h:57
int target_os_syscall_record_clear(struct target *target, tid_t tid)
Definition: target_os.c:537
int target_os_signal_enqueue(struct target *target, tid_t tid, int signo, void *data)
Definition: target_os.c:399
int target_os_syscall_table_load(struct target *target)
Definition: target_os.c:415
struct probe * target_os_syscall_probe(struct target *target, tid_t tid, struct target_os_syscall *syscall, probe_handler_t pre_handler, probe_handler_t post_handler, void *handler_data)
Definition: target_os.c:465