Rizin
unix-like reverse engineering framework and cli tools
dsignal.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-2020 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_debug.h>
5 
6 #define DB dbg->sgnls
7 
8 // TODO: this must be done by the debugger plugin
9 // which is stored already in SDB.. but this is faster :P
10 static struct {
11  const char *k;
12  const char *v;
13 } signals[] = {
14  // hardcoded from linux
15  { "SIGHUP", "1" },
16  { "SIGINT", "2" },
17  { "SIGQUIT", "3" },
18  { "SIGILL", "4" },
19  { "SIGTRAP", "5" },
20  { "SIGABRT", "6" },
21  // { "SIGIOT", "6" },
22  { "SIGBUS", "7" },
23  { "SIGFPE", "8" },
24  { "SIGKILL", "9" },
25  { "SIGUSR1", "10" },
26  { "SIGSEGV", "11" },
27  { "SIGUSR2", "12" },
28  { "SIGPIPE", "13" },
29  { "SIGALRM", "14" },
30  { "SIGTERM", "15" },
31  { "SIGSTKFLT", "16" },
32  { "SIGCHLD", "17" },
33  { "SIGCONT", "18" },
34  { "SIGSTOP", "19" },
35  { "SIGTSTP", "20" },
36  { "SIGTTIN", "21" },
37  { "SIGTTOU", "22" },
38  { "SIGURG", "23" },
39  { "SIGXCPU", "24" },
40  { "SIGXFSZ", "25" },
41  { "SIGVTALRM", "26" },
42  { "SIGPROF", "27" },
43  { "SIGWINCH", "28" },
44  { "SIGIO", "29" },
45  { "SIGPOLL", "SIGIO" },
46  { "SIGLOST", "29" },
47  { "SIGPWR", "30" },
48  { "SIGSYS", "31" },
49  { "SIGRTMIN", "32" },
50  { "SIGRTMAX", "NSIG" },
51  { NULL }
52 };
53 
55  int i;
56  // XXX
57  DB = sdb_new(NULL, "signals", 0);
58  for (i = 0; signals[i].k; i++) {
59  sdb_set(DB, signals[i].k, signals[i].v, 0);
60  sdb_set(DB, signals[i].v, signals[i].k, 0);
61  }
62 }
63 
64 static bool siglistcb(void *p, const char *k, const char *v) {
65  static char key[32] = "cfg.";
66  RzDebug *dbg = (RzDebug *)p;
67  int opt, mode = dbg->_mode;
68  if (atoi(k) > 0) {
69  strncpy(key + 4, k, 20);
70  opt = sdb_num_get(DB, key, 0);
71  if (opt) {
72  rz_cons_printf("%s %s", k, v);
73  if (opt & RZ_DBG_SIGNAL_CONT) {
74  rz_cons_strcat(" cont");
75  }
76  if (opt & RZ_DBG_SIGNAL_SKIP) {
77  rz_cons_strcat(" skip");
78  }
80  } else {
81  if (mode == 0) {
82  rz_cons_printf("%s %s\n", k, v);
83  }
84  }
85  }
86  return true;
87 }
88 
89 struct debug_pj {
91  PJ *pj;
92 };
93 
94 static bool siglistjsoncb(void *p, const char *k, const char *v) {
95  static char key[32] = "cfg.";
96  struct debug_pj *dpj = (struct debug_pj *)p;
97  int opt;
98  if (atoi(k) > 0) {
99  strncpy(key + 4, k, 20);
100  opt = (int)sdb_num_get(dpj->dbg->sgnls, key, 0);
101  if (dpj->dbg->_mode == 2) {
102  dpj->dbg->_mode = 0;
103  }
104  pj_o(dpj->pj);
105  pj_ks(dpj->pj, "signum", k);
106  pj_ks(dpj->pj, "name", v);
107  if (opt & RZ_DBG_SIGNAL_CONT) {
108  pj_ks(dpj->pj, "option", "cont");
109  } else if (opt & RZ_DBG_SIGNAL_SKIP) {
110  pj_ks(dpj->pj, "option", "skip");
111  } else {
112  pj_knull(dpj->pj, "option");
113  }
114  pj_end(dpj->pj);
115  }
116  return true;
117 }
118 
120  dbg->_mode = mode;
121  switch (mode) {
122  case RZ_OUTPUT_MODE_JSON: {
123  PJ *pj = pj_new();
124  if (!pj) {
125  break;
126  }
127  struct debug_pj dpj = { dbg, pj };
128  pj_a(pj);
129  sdb_foreach(DB, siglistjsoncb, &dpj);
130  pj_end(pj);
132  pj_free(pj);
133  break;
134  }
136  default:
138  break;
139  }
140  dbg->_mode = 0;
141 }
142 
144  return rz_sys_kill(dbg->pid, num);
145 }
146 
148  sdb_queryf(DB, "cfg.%d=%d", num, opt);
149 }
150 
152  char k[32];
153  snprintf(k, sizeof(k), "cfg.%d", num);
154  return sdb_num_get(DB, k, 0);
155 }
156 
158  // TODO
159  // rz_debug_syscall (dbg, "signal", "addr");
160  return 0;
161 }
162 
163 /* TODO rename to _kill_ -> _signal_ */
165  if (dbg->cur->kill_list) {
166  return dbg->cur->kill_list(dbg);
167  }
168  return NULL;
169 }
170 
172  eprintf("TODO: set signal handlers of child\n");
173  // TODO: must inject code to call signal()
174 #if 0
175  if (dbg->cur->kill_setup)
176  return dbg->cur->kill_setup (dbg, sig, action);
177 #endif
178  // TODO: implement rz_debug_kill_setup
179  return false;
180 }
lzma_index ** i
Definition: index.h:629
RZ_API void rz_cons_strcat(const char *str)
Definition: cons.c:1263
RZ_API void rz_cons_newline(void)
Definition: cons.c:1274
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
RZ_API void rz_cons_println(const char *str)
Definition: cons.c:233
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
RzDebug * dbg
Definition: desil.c:30
RZ_API void rz_debug_signal_setup(RzDebug *dbg, int num, int opt)
Definition: dsignal.c:147
static struct @231 signals[]
RZ_API int rz_debug_signal_set(RzDebug *dbg, int num, ut64 addr)
Definition: dsignal.c:157
const char * k
Definition: dsignal.c:11
RZ_API RzList * rz_debug_kill_list(RzDebug *dbg)
Definition: dsignal.c:164
RZ_API void rz_debug_signal_init(RzDebug *dbg)
Definition: dsignal.c:54
RZ_API void rz_debug_signal_list(RzDebug *dbg, RzOutputMode mode)
Definition: dsignal.c:119
const char * v
Definition: dsignal.c:12
RZ_API int rz_debug_signal_send(RzDebug *dbg, int num)
Definition: dsignal.c:143
RZ_API int rz_debug_signal_what(RzDebug *dbg, int num)
Definition: dsignal.c:151
#define DB
Definition: dsignal.c:6
static bool siglistjsoncb(void *p, const char *k, const char *v)
Definition: dsignal.c:94
RZ_API int rz_debug_kill_setup(RzDebug *dbg, int sig, int action)
Definition: dsignal.c:171
static bool siglistcb(void *p, const char *k, const char *v)
Definition: dsignal.c:64
const char int mode
Definition: ioapi.h:137
snprintf
Definition: kernel.h:364
void * p
Definition: libc.cpp:67
RZ_API int sdb_queryf(Sdb *s, const char *fmt,...)
Definition: query.c:57
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126
RZ_API ut64 sdb_num_get(Sdb *s, const char *key, ut32 *cas)
Definition: num.c:13
#define eprintf(x, y...)
Definition: rlcc.c:7
@ RZ_DBG_SIGNAL_CONT
Definition: rz_debug.h:71
@ RZ_DBG_SIGNAL_SKIP
Definition: rz_debug.h:72
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87
RZ_API const char * pj_string(PJ *pj)
Definition: pj.c:57
RZ_API void pj_free(PJ *j)
Definition: pj.c:34
RZ_API PJ * pj_knull(PJ *j, const char *k)
Definition: pj.c:114
RZ_API PJ * pj_o(PJ *j)
Definition: pj.c:75
RZ_API PJ * pj_ks(PJ *j, const char *k, const char *v)
Definition: pj.c:170
RZ_API PJ * pj_a(PJ *j)
Definition: pj.c:81
RZ_API int rz_sys_kill(int pid, int sig)
Send signal sig to process with pid pid.
Definition: sys.c:1850
RzOutputMode
Enum to describe the way data are printed.
Definition: rz_types.h:38
@ RZ_OUTPUT_MODE_JSON
Definition: rz_types.h:40
@ RZ_OUTPUT_MODE_STANDARD
Definition: rz_types.h:39
RZ_API Sdb * sdb_new(const char *path, const char *name, int lock)
Definition: sdb.c:47
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
RZ_API bool sdb_foreach(Sdb *s, SdbForeachCallback cb, void *user)
Definition: sdb.c:758
static int
Definition: sfsocketcall.h:114
RzDebug * dbg
Definition: dsignal.c:90
PJ * pj
Definition: dsignal.c:91
Definition: rz_pj.h:12
RzList *(* kill_list)(RzDebug *dbg)
Definition: rz_debug.h:387
int _mode
Definition: rz_debug.h:316
struct rz_debug_plugin_t * cur
Definition: rz_debug.h:295
Sdb * sgnls
Definition: rz_debug.h:313
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58