Rizin
unix-like reverse engineering framework and cli tools
pid.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2017 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_debug.h>
5 
6 RZ_API RzDebugPid *rz_debug_pid_new(const char *path, int pid, int uid, char status, ut64 pc) {
8  if (!p) {
9  return NULL;
10  }
11  p->path = strdup(path);
12  p->pid = pid;
13  p->uid = uid;
14  p->status = status;
15  p->runnable = true;
16  p->pc = pc;
17  return p;
18 }
19 
21  free(pid->path);
22  free(pid);
23  return NULL;
24 }
25 
27  if (dbg && dbg->cur && dbg->cur->pids) {
28  return dbg->cur->pids(dbg, pid);
29  }
30  return NULL;
31 }
32 
33 // TODO: deprecate list/iterate functions from core apis? keep them for easiness?
34 RZ_API int rz_debug_pid_list(RzDebug *dbg, int pid, char fmt) {
35  RzList *list;
37  RzDebugPid *p;
38  if (dbg && dbg->cur && dbg->cur->pids) {
39  list = dbg->cur->pids(dbg, RZ_MAX(0, pid));
40  if (!list) {
41  return false;
42  }
43  PJ *j = pj_new();
44  pj_a(j);
45  rz_list_foreach (list, iter, p) {
46  switch (fmt) {
47  case 'j':
48  pj_o(j);
49  pj_kb(j, "current", dbg->pid == p->pid);
50  pj_ki(j, "ppid", p->ppid);
51  pj_ki(j, "pid", p->pid);
52  pj_ki(j, "uid", p->uid);
53  pj_ks(j, "status", &p->status);
54  pj_ks(j, "path", p->path);
55  pj_end(j);
56  break;
57  default:
58  dbg->cb_printf(" %c %d ppid:%d uid:%d %c %s\n",
59  dbg->pid == p->pid ? '*' : '-',
60  p->pid, p->ppid, p->uid, p->status, p->path);
61  break;
62  }
63  }
64  pj_end(j);
65  if (fmt == 'j') {
66  dbg->cb_printf("%s", pj_string(j));
67  }
68  pj_free(j);
70  }
71  return false;
72 }
73 
74 RZ_API int rz_debug_thread_list(RzDebug *dbg, int pid, char fmt) {
75  RzList *list;
77  RzDebugPid *p;
78  RzAnalysisFunction *fcn = NULL;
79  RzDebugMap *map = NULL;
80  RzStrBuf *path = NULL;
81  if (pid == -1) {
82  return false;
83  }
84  if (dbg && dbg->cur && dbg->cur->threads) {
85  list = dbg->cur->threads(dbg, pid);
86  if (!list) {
87  return false;
88  }
89  PJ *j = pj_new();
90  pj_a(j);
91  rz_list_foreach (list, iter, p) {
92  path = rz_strbuf_new("");
93  if (p->pc != 0) {
94  map = rz_debug_map_get(dbg, p->pc);
95  if (map && map->name && map->name[0]) {
96  rz_strbuf_appendf(path, "%s ", map->name);
97  }
98 
99  rz_strbuf_appendf(path, "(0x%" PFMT64x ")", p->pc);
100 
101  fcn = rz_analysis_get_fcn_in(dbg->analysis, p->pc, 0);
102  if (fcn) {
103  if (p->pc == fcn->addr) {
104  rz_strbuf_appendf(path, " at %s", fcn->name);
105  } else {
106  st64 delta = p->pc - fcn->addr;
107  char sign = delta >= 0 ? '+' : '-';
108  rz_strbuf_appendf(path, " in %s%c%" PFMT64u, fcn->name, sign, RZ_ABS(delta));
109  }
110  } else {
111  const char *flag_name = dbg->corebind.getName(dbg->corebind.core, p->pc);
112  if (flag_name) {
113  rz_strbuf_appendf(path, " at %s", flag_name);
114  } else {
115  char *name_delta = dbg->corebind.getNameDelta(dbg->corebind.core, p->pc);
116  if (name_delta) {
117  rz_strbuf_appendf(path, " in %s", name_delta);
118  free(name_delta);
119  }
120  }
121  }
122  }
123  switch (fmt) {
124  case 'j':
125  pj_o(j);
126  pj_kb(j, "current", dbg->tid == p->pid);
127  pj_ki(j, "pid", p->pid);
128  pj_ks(j, "status", &p->status);
129  pj_ks(j, "path", rz_strbuf_get(path));
130  pj_end(j);
131  break;
132  default:
133  dbg->cb_printf(" %c %d %c %s\n",
134  dbg->tid == p->pid ? '*' : '-',
135  p->pid, p->status, rz_strbuf_get(path));
136  break;
137  }
139  }
140  pj_end(j);
141  if (fmt == 'j') {
142  dbg->cb_printf("%s", pj_string(j));
143  }
144  pj_free(j);
146  }
147  return false;
148 }
149 
150 /* processes */
152  // fork in child
153  return 0;
154 }
155 
156 #if 0
157 RZ_API int rz_debug_pid_del(struct rz_debug_t *dbg) {
158  // kill da child
159  return true;
160 }
161 
162 /* threads */
163 RZ_API int rz_debug_pid_add_thread(struct rz_debug_t *dbg) {
164  // create a thread in process
165  return true;
166 }
167 
168 RZ_API int rz_debug_pid_del_thread(struct rz_debug_t *dbg) {
169  // kill a thread in process
170  return true;
171 }
172 #endif
173 
174 /* status */
176  return true;
177 }
178 
179 /* status */
181  return NULL;
182 }
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
RzDebug * dbg
Definition: desil.c:30
RZ_API RzDebugMap * rz_debug_map_get(RzDebug *dbg, ut64 addr)
Definition: dmap.c:65
size_t map(int syms, int left, int len)
Definition: enough.c:237
RZ_DEPRECATE RZ_API RzAnalysisFunction * rz_analysis_get_fcn_in(RzAnalysis *analysis, ut64 addr, int type)
Definition: fcn.c:1687
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * p
Definition: libc.cpp:67
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
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 pid
Definition: sflib.h:64
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
@ RZ_ABS
RZ_API int rz_debug_pid_set_state(struct rz_debug_t *dbg, int status)
Definition: pid.c:175
RZ_API RzList * rz_debug_pids(RzDebug *dbg, int pid)
Definition: pid.c:26
RZ_API RzDebugPid * rz_debug_pid_free(RzDebugPid *pid)
Definition: pid.c:20
RZ_API int rz_debug_pid_parent(RzDebugPid *pid)
Definition: pid.c:151
RZ_API struct rz_debug_pid_t * rz_debug_pid_get_status(struct rz_debug_t *dbg, int pid)
Definition: pid.c:180
RZ_API RzDebugPid * rz_debug_pid_new(const char *path, int pid, int uid, char status, ut64 pc)
Definition: pid.c:6
RZ_API int rz_debug_pid_list(RzDebug *dbg, int pid, char fmt)
Definition: pid.c:34
RZ_API int rz_debug_thread_list(RzDebug *dbg, int pid, char fmt)
Definition: pid.c:74
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API PJ * pj_kb(PJ *j, const char *k, bool v)
Definition: pj.c:177
RZ_API PJ * pj_ki(PJ *j, const char *k, int d)
Definition: pj.c:149
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_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 char * rz_strbuf_get(RzStrBuf *sb)
Definition: strbuf.c:321
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API void rz_strbuf_free(RzStrBuf *sb)
Definition: strbuf.c:358
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define PFMT64u
Definition: rz_types.h:395
#define PFMT64x
Definition: rz_types.h:393
#define st64
Definition: rz_types_base.h:10
#define RZ_MAX(x, y)
Definition: rz_pj.h:12
void * core
Definition: rz_bind.h:31
RzCoreGetNameDelta getNameDelta
Definition: rz_bind.h:41
RzCoreGetName getName
Definition: rz_bind.h:40
RzList *(* threads)(RzDebug *dbg, int pid)
Definition: rz_debug.h:375
RzList *(* pids)(RzDebug *dbg, int pid)
Definition: rz_debug.h:376
RzCoreBind corebind
Definition: rz_debug.h:314
PrintfCallback cb_printf
Definition: rz_debug.h:292
struct rz_debug_plugin_t * cur
Definition: rz_debug.h:295
RzAnalysis * analysis
Definition: rz_debug.h:305
static st64 delta
Definition: vmenus.c:2425
ut64(WINAPI *w32_GetEnabledXStateFeatures)()