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
dumpsyscalltable.c
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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <argp.h>
25 
26 #include <sys/user.h>
27 #include <sys/ptrace.h>
28 #include <inttypes.h>
29 
30 #include <signal.h>
31 
32 #include <glib.h>
33 #include "glib_wrapper.h"
34 
35 #include "log.h"
36 #include "alist.h"
37 #include "dwdebug.h"
38 #include "target_api.h"
39 #include "target.h"
40 #include "target_os.h"
41 
42 static int cleaning = 0;
43 struct target *t = NULL;
44 
45 void cleanup() {
46  if (cleaning)
47  return;
48 
49  cleaning = 1;
50  if (t) {
51  target_close(t);
52  target_finalize(t);
53  t = NULL;
54  }
55  cleaning = 0;
56 }
57 
58 void sigh(int signo) {
59  if (t) {
60  target_pause(t);
61  cleanup();
62  }
63  target_fini();
64  exit(0);
65 }
66 
67 int main(int argc,char **argv) {
68  struct target_spec *tspec;
69  char *targetstr;
70  int i,j;
71  int maxnum;
72  struct target_os_syscall *sc;
73  struct symbol *argsym;
74  struct dump_info ud = { .stream = stdout,.prefix = "",.detail = 0,.meta = 0 };
75  GSList *gsltmp;
76 
77  tspec = target_argp_driver_parse_one(NULL,NULL,argc,argv,
79 
80  if (!tspec) {
81  verror("could not parse target arguments!\n");
82  exit(-1);
83  }
84 
85  signal(SIGHUP,sigh);
86  signal(SIGINT,sigh);
87  signal(SIGQUIT,sigh);
88  signal(SIGABRT,sigh);
89  signal(SIGKILL,sigh);
90  signal(SIGSEGV,sigh);
91  signal(SIGPIPE,sigh);
92  signal(SIGALRM,sigh);
93  signal(SIGTERM,sigh);
94  signal(SIGUSR1,sigh);
95  signal(SIGUSR2,sigh);
96 
97  target_init();
98  atexit(target_fini);
99 
100  t = target_instantiate(tspec,NULL);
101  if (!t) {
102  verror("could not instantiate target!\n");
103  exit(-1);
104  }
105 
106  if (target_open(t)) {
107  fprintf(stderr,"could not open target!\n");
108  exit(-4);
109  }
110 
111  /*
112  * Make a permanent copy so we can print useful messages after
113  * target_finalize.
114  */
115  targetstr = target_name(t);
116  if (!targetstr)
117  targetstr = strdup("<UNNAMED_TARGET>");
118  else
119  targetstr = strdup(targetstr);
120 
122  goto exit;
123 
124  fflush(stderr);
125  fflush(stdout);
126 
128  if (maxnum < 0) {
129  verror("could not get max number of target syscalls!\n");
130  goto exit;
131  }
132 
133  for (i = 0; i < maxnum; ++i) {
135  if (!sc)
136  continue;
137  if (sc->bsymbol) {
138  printf("%.3d\t%"PRIxADDR"\t%s\t",
139  sc->num,sc->addr,bsymbol_get_name(sc->bsymbol));
140  if (sc->args) {
141  printf("(");
142  j = 0;
143  v_g_slist_foreach(sc->args,gsltmp,argsym) {
144  if (likely(j))
145  printf(", ");
147  printf(" %s",symbol_get_name(argsym));
148  ++j;
149  }
150  printf(")");
151  }
152  printf("\n");
153  if (sc->wrapped_bsymbol) {
154  printf("\t\twrapped syscall:\t\t%s",
156  GSList *wargs =
159  if (wargs) {
160  printf("(");
161  j = 0;
162  v_g_slist_foreach(sc->args,gsltmp,argsym) {
163  if (likely(j))
164  printf(", ");
166  printf(" %s",symbol_get_name(argsym));
167  ++j;
168  }
169  printf(")");
170  }
171  printf("\n");
172  }
173 
174  }
175  else {
176  printf("%.3d\t%"PRIxADDR"\n",sc->num,sc->addr);
177  }
178  }
179 
180  exit:
181  target_resume(t);
182 
183  fflush(stderr);
184  fflush(stdout);
185  cleanup();
186 
187  printf("%s finished.\n",targetstr);
188  exit(0);
189 }
GSList * args
Definition: target_os.h:53
struct target_spec * tspec
Definition: dumptarget.c:113
void cleanup()
static uint64_t unsigned int i
void target_init(void)
Definition: target.c:69
#define v_g_slist_foreach(gslhead, gslcur, elm)
Definition: glib_wrapper.h:60
int target_resume(struct target *target)
Definition: target_api.c:1012
#define likely(expr)
Definition: debugpred.h:102
int target_pause(struct target *target)
Definition: target_api.c:1027
void symbol_type_dump(struct symbol *symbol, struct dump_info *ud)
Definition: debug.c:5328
char * bsymbol_get_name(struct bsymbol *bsymbol)
Definition: symbol.c:62
int target_close(struct target *target)
Definition: target_api.c:1511
#define verror(format,...)
Definition: log.h:30
struct symbol * symbol_get_datatype(struct symbol *symbol)
Definition: debug.c:2989
void target_fini(void)
Definition: target.c:91
int target_os_syscall_table_load(struct target *target)
Definition: target_os.c:415
struct bsymbol * wrapped_bsymbol
Definition: target_os.h:48
struct target_os_syscall * target_os_syscall_lookup_num(struct target *target, int num)
Definition: target_os.c:443
char * target_name(struct target *target)
Definition: target_api.c:505
struct target * t
FILE * stream
Definition: output.h:26
int target_os_syscall_table_get_max_num(struct target *target)
Definition: target_os.c:430
int main(int argc, char **argv)
void sigh(int signo)
int target_finalize(struct target *target)
Definition: target.c:1955
struct symbol * bsymbol_get_symbol(struct bsymbol *bsymbol)
Definition: symbol.c:66
GSList * symbol_get_members(struct symbol *symbol, symbol_type_flag_t flags)
Definition: debug.c:3146
int target_open(struct target *target)
Definition: target_api.c:513
#define PRIxADDR
Definition: common.h:67
struct target_spec * target_argp_driver_parse_one(struct argp *driver_parser, void *driver_state, int argc, char **argv, target_type_t target_types, int filter_quoted)
Definition: target.c:802
char * symbol_get_name(struct symbol *symbol)
Definition: debug.c:2587
struct target * target_instantiate(struct target_spec *spec, struct evloop *evloop)
Definition: target_api.c:55
struct bsymbol * bsymbol
Definition: target_os.h:42