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
monitored_target.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <signal.h>
6 #include <unistd.h>
7 
8 #include "common.h"
9 #include "log.h"
10 #include "monitor.h"
11 #include "proxyreq.h"
12 
13 #include "target_rpc.h"
14 #include "debuginfo_rpc.h"
15 
16 static int stdin_callback(int fd,char *buf,int len) {
17  vdebug(0,LA_USER,1,"read '%s' (%d) on fd %d\n",buf,len,fd);
18 
19  return 0;
20 }
21 
22 struct target *target = NULL;
23 struct monitor *monitor = NULL;
24 struct target_spec *tspec = NULL;
25 
26 static void cleanup(void) {
27  static int cleaning = 0;
28 
29  if (cleaning)
30  return;
31  cleaning = 1;
32 
33  if (target) {
34  target_close(target);
35  target_finalize(target);
36  target = NULL;
37  }
38 
39  if (monitor) {
40  monitor_destroy(monitor);
41  monitor = NULL;
42  }
43 
44  target_free_spec(tspec);
45 
47 
48 #ifdef REF_DEBUG
50 #endif
51 }
52 
53 static void sigh(int signo) {
54  if (target)
55  target_pause(target);
56 
57  cleanup();
58 
59  fprintf(stderr,"Shutdown target and monitor connection; exiting (sig %d).\n",
60  signo);
61 
62  exit(1);
63 }
64 
65 int main(int argc,char **argv) {
66  char *svc_name = "target";
67  int rc;
68 
75 
76  tspec = target_argp_driver_parse_one(NULL,NULL,argc,argv,
78  | TARGET_TYPE_GDB,1);
79  if (!tspec) {
80  verror("could not parse a target specification from arguments!\n");
81  monitor_destroy(monitor);
82  exit(-11);
83  }
84 
86  atexit(target_rpc_fini);
87 
88  signal(SIGHUP,sigh);
89  signal(SIGINT,sigh);
90  signal(SIGQUIT,sigh);
91  signal(SIGABRT,sigh);
92  signal(SIGKILL,sigh);
93  signal(SIGSEGV,sigh);
94  signal(SIGPIPE,sigh);
95  signal(SIGALRM,sigh);
96  signal(SIGTERM,sigh);
97  signal(SIGUSR1,sigh);
98  signal(SIGUSR2,sigh);
99 
101  MONITOR_OBJTYPE_TARGET,NULL,NULL,NULL,
102  NULL,NULL); //stdin_callback);
103  if (!monitor) {
104  verror("could not attach to monitor (in pid %d)\n",getpid());
105  exit(-3);
106  }
107 
108  vdebug(1,LA_XML,LF_RPC,"attached to monitor for target %d\n",
109  monitor->objid);
110 
111  target = target_instantiate(tspec,monitor->evloop);
112 
113  if (!target) {
114  verror("could not instantiate target, detaching from monitor!\n");
115  cleanup();
116  exit(-12);
117  }
118 
119  monitor_add_primary_obj(monitor,target->id,MONITOR_OBJTYPE_TARGET,target,NULL);
120 
121  vdebug(1,LA_XML,LF_RPC,"instantiated target %d\n",monitor->objid);
122 
123  if (target->id > -1 && monitor->objid != tspec->target_id) {
124  vwarn("monitored objid %d is not target id %d\n",
125  monitor->objid,tspec->target_id);
126  tspec->target_id = monitor->objid;
127  }
128  else if (tspec->target_id < 0)
129  tspec->target_id = monitor->objid;
130 
131  while (1) {
132  rc = monitor_run(monitor);
133  if (rc < 0) {
134  verror("bad internal error in monitor for %s %d; destroying!\n",
135  svc_name,monitor->objid);
136  monitor_destroy(monitor);
137  return -1;
138  }
139  else {
140  if (monitor_is_done(monitor)) {
141  vdebug(2,LA_XML,LF_RPC,
142  "monitoring on %s %d is done; finalizing!\n",
143  svc_name,monitor->objid);
144  monitor_destroy(monitor);
145  return 0;
146  }
147  else {
148  vwarn("%s %d monitor_run finished unexpectedly; finalizing!\n",
149  svc_name,monitor->objid);
150  monitor_destroy(monitor);
151  return -1;
152  }
153  }
154  }
155 }
void target_rpc_fini(void)
Definition: target_rpc.c:416
void vmi_add_log_area_flags(log_areas_t areas, log_flags_t flags)
Definition: log.c:67
#define LF_ALL
Definition: log.h:232
int monitor_add_primary_obj(struct monitor *monitor, int objid, int objtype, void *obj, void *objstate)
Definition: monitor.c:1576
int monitor_is_done(struct monitor *monitor)
Definition: monitor.c:2224
int target_pause(struct target *target)
Definition: target_api.c:1027
struct evloop * evloop
Definition: monitor.h:256
int target_close(struct target *target)
Definition: target_api.c:1511
#define verror(format,...)
Definition: log.h:30
int objid
Definition: monitor.h:243
void target_rpc_init(void)
Definition: target_rpc.c:393
void vmi_set_log_level(int level)
Definition: log.c:34
#define vwarn(format,...)
Definition: log.h:33
void sigh(int signo)
Definition: debugserver.c:302
struct monitor * monitor_attach(monitor_type_t type, monitor_flags_t flags, int objtype, void *obj, void *objstate, evloop_handler_t custom_child_recv_evh, monitor_stdio_callback_t stdin_callback, void *callback_state)
Definition: monitor.c:1616
Definition: log.h:68
void target_free_spec(struct target_spec *spec)
Definition: target_api.c:453
Definition: log.h:203
int len
Definition: dumptarget.c:52
#define vdebug(devel, areas, flags, format,...)
Definition: log.h:302
Definition: log.h:72
Definition: log.h:70
Definition: log.h:71
#define REF_DEBUG_REPORT_FINISH()
Definition: common.h:653
Definition: log.h:76
int target_finalize(struct target *target)
Definition: target.c:1955
int main(int argc, char **argv)
struct target_spec * tspec
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
struct target * target_instantiate(struct target_spec *spec, struct evloop *evloop)
Definition: target_api.c:55
int id
Definition: target_api.h:2514
int monitor_destroy(struct monitor *monitor)
Definition: monitor.c:1039
int monitor_run(struct monitor *monitor)
Definition: monitor.c:2274
#define MONITOR_OBJTYPE_TARGET
Definition: target_rpc.h:35
void cleanup()
Definition: debugserver.c:295