Rizin
unix-like reverse engineering framework and cli tools
debug_io.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2016-2018 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_io.h>
5 #include <rz_debug.h>
6 
7 static int __io_step(RzDebug *dbg) {
8  free(dbg->iob.system(dbg->iob.io, "ds"));
9  return true;
10 }
11 
12 static int __io_step_over(RzDebug *dbg) {
13  free(dbg->iob.system(dbg->iob.io, "dso"));
14  return true;
15 }
16 
18  RzList *list = rz_list_new();
19  char *str = dbg->iob.system(dbg->iob.io, "dm");
20  if (!str) {
22  return NULL;
23  }
24  char *ostr = str;
25  ut64 map_start, map_end;
26  char perm[32];
27  char name[512];
28  for (;;) {
29  char *nl = strchr(str, '\n');
30  if (nl) {
31  *nl = 0;
32  *name = 0;
33  *perm = 0;
34  map_start = map_end = 0LL;
35  if (!strncmp(str, "sys ", 4)) {
36  char *sp = strchr(str + 4, ' ');
37  if (sp) {
38  str = sp + 1;
39  } else {
40  str += 4;
41  }
42  }
43  char *_s_ = strstr(str, " s ");
44  if (_s_) {
45  memmove(_s_, _s_ + 2, strlen(_s_));
46  }
47  _s_ = strstr(str, " ? ");
48  if (_s_) {
49  memmove(_s_, _s_ + 2, strlen(_s_));
50  }
51  sscanf(str, "0x%" PFMT64x " - 0x%" PFMT64x " %s %s",
52  &map_start, &map_end, perm, name);
53  if (map_end != 0LL) {
54  RzDebugMap *map = rz_debug_map_new(name, map_start, map_end, rz_str_rwx(perm), 0);
56  }
57  str = nl + 1;
58  } else {
59  break;
60  }
61  }
62  free(ostr);
63  rz_cons_reset();
64  return list;
65 }
66 
68  /* do nothing */
69  return RZ_DEBUG_REASON_NONE;
70 }
71 
72 static int __io_attach(RzDebug *dbg, int pid) {
73  return true;
74 }
75 
76 // "drp" register profile
77 static char *__io_reg_profile(RzDebug *dbg) {
78  rz_cons_push();
79  char *drp = dbg->iob.system(dbg->iob.io, "drp");
80  if (drp) {
81  return drp;
82  }
83  char *buf = rz_cons_get_buffer_dup();
84  if (RZ_STR_ISNOTEMPTY(buf)) {
85  rz_cons_pop();
86  return buf;
87  }
88  free(buf);
89  rz_cons_pop();
91 }
92 
93 // "dr8" read register state
94 static int __reg_read(RzDebug *dbg, int type, ut8 *buf, int size) {
95  char *dr8 = dbg->iob.system(dbg->iob.io, "dr8");
96  if (!dr8) {
97  char *fb = rz_cons_get_buffer_dup();
98  if (RZ_STR_ISEMPTY(fb)) {
99  free(fb);
100  eprintf("debug.io: Failed to get dr8 from io\n");
101  return -1;
102  }
103  dr8 = fb;
104  rz_cons_reset();
105  }
106  ut8 *bregs = calloc(1, strlen(dr8));
107  if (!bregs) {
108  free(dr8);
109  return -1;
110  }
111  rz_str_trim((char *)bregs);
112  int sz = rz_hex_str2bin(dr8, bregs);
113  if (sz > 0) {
114  memcpy(buf, bregs, RZ_MIN(size, sz));
115  free(bregs);
116  free(dr8);
117  return size;
118  } else {
119  // eprintf ("SIZE %d (%s)\n", sz, regs);
120  }
121  free(bregs);
122  free(dr8);
123  return -1;
124 }
125 
126 // "dc" continue execution
127 static int __io_continue(RzDebug *dbg, int pid, int tid, int sig) {
128  dbg->iob.system(dbg->iob.io, "dc");
129  rz_cons_flush();
130  return true;
131 }
132 
133 // "dk" send kill signal
134 static bool __io_kill(RzDebug *dbg, int pid, int tid, int sig) {
135  const char *cmd = sdb_fmt("dk %d", sig);
136  dbg->iob.system(dbg->iob.io, cmd);
137  rz_cons_flush();
138  return true;
139 }
140 
142  .name = "io",
143  .license = "MIT",
144  .arch = "any", // TODO: exception!
145  .bits = RZ_SYS_BITS_32 | RZ_SYS_BITS_64,
146  .step = __io_step,
147  .map_get = __io_maps,
148  .attach = &__io_attach,
149  .wait = &__io_wait,
150  .reg_read = __reg_read,
151  .cont = __io_continue,
152  .kill = __io_kill,
153  .reg_profile = __io_reg_profile,
154  .step_over = __io_step_over,
155  .canstep = 1,
156 #if 0
157  .init = __esil_init,
158  .contsc = __esil_continue_syscall,
159  .detach = &__esil_detach,
160  .stop = __esil_stop,
161  .breakpoint = &__esil_breakpoint,
162 #endif
163 };
164 
165 #ifndef RZ_PLUGIN_INCORE
168  .data = &rz_debug_plugin_io,
170 };
171 #endif
RZ_API char * rz_analysis_get_reg_profile(RzAnalysis *analysis)
Definition: analysis.c:212
RZ_API RZ_OWN char * rz_cons_get_buffer_dup(void)
Return a newly allocated buffer containing what's currently in RzCons buffer.
Definition: cons.c:827
RZ_API void rz_cons_flush(void)
Definition: cons.c:959
RZ_API void rz_cons_reset(void)
Definition: cons.c:804
RZ_API void rz_cons_pop(void)
Definition: cons.c:876
RZ_API void rz_cons_push(void)
Definition: cons.c:860
#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 cmd
Definition: sflib.h:79
static int __esil_breakpoint(RzBreakpoint *bp, RzBreakpointItem *b, bool set)
Definition: debug_esil.c:113
static int __esil_detach(RzDebug *dbg, int pid)
Definition: debug_esil.c:87
static int __esil_continue_syscall(RzDebug *dbg, int pid, int num)
Definition: debug_esil.c:60
static int __esil_stop(RzDebug *dbg)
Definition: debug_esil.c:123
static char * __io_reg_profile(RzDebug *dbg)
Definition: debug_io.c:77
static bool __io_kill(RzDebug *dbg, int pid, int tid, int sig)
Definition: debug_io.c:134
static RzDebugReasonType __io_wait(RzDebug *dbg, int pid)
Definition: debug_io.c:67
static RzList * __io_maps(RzDebug *dbg)
Definition: debug_io.c:17
static int __reg_read(RzDebug *dbg, int type, ut8 *buf, int size)
Definition: debug_io.c:94
RZ_API RzLibStruct rizin_plugin
Definition: debug_io.c:166
RzDebugPlugin rz_debug_plugin_io
Definition: debug_io.c:141
static int __io_continue(RzDebug *dbg, int pid, int tid, int sig)
Definition: debug_io.c:127
static int __io_attach(RzDebug *dbg, int pid)
Definition: debug_io.c:72
static int __io_step_over(RzDebug *dbg)
Definition: debug_io.c:12
static int __io_step(RzDebug *dbg)
Definition: debug_io.c:7
RzDebug * dbg
Definition: desil.c:30
RZ_API RzDebugMap * rz_debug_map_new(char *name, ut64 addr, ut64 addr_end, int perm, int user)
Definition: dmap.c:7
size_t map(int syms, int left, int len)
Definition: enough.c:237
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
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
int type
Definition: mipsasm.c:17
static void __esil_init(RzCore *core)
Definition: panels.c:4056
#define eprintf(x, y...)
Definition: rlcc.c:7
RzDebugReasonType
Definition: rz_debug.h:89
@ RZ_DEBUG_REASON_NONE
Definition: rz_debug.h:91
RZ_API int rz_hex_str2bin(const char *in, ut8 *out)
Convert an input string in into the binary form in out.
Definition: hex.c:444
@ RZ_LIB_TYPE_DBG
Definition: rz_lib.h:70
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
RZ_API int rz_str_rwx(const char *str)
Definition: str.c:318
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API void rz_str_trim(RZ_NONNULL RZ_INOUT char *str)
Removes whitespace characters (space, tab, newline etc.) from the beginning and end of a string.
Definition: str_trim.c:190
@ RZ_SYS_BITS_32
Definition: rz_sys.h:20
@ RZ_SYS_BITS_64
Definition: rz_sys.h:21
#define PFMT64x
Definition: rz_types.h:393
#define RZ_MIN(x, y)
#define RZ_VERSION
Definition: rz_version.h:8
Definition: z80asm.h:102
const char * version
Definition: rz_debug.h:362
const char * name
Definition: rz_debug.h:359
RzAnalysis * analysis
Definition: rz_debug.h:305
RzIOBind iob
Definition: rz_debug.h:293
RzIOSystem system
Definition: rz_io.h:242
RzIO * io
Definition: rz_io.h:232
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int sp
Definition: z80asm.c:91