Rizin
unix-like reverse engineering framework and cli tools
csyscall.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2021 pancake <pancake@nopcode.org>
2 // SPDX-FileCopyrightText: 2009-2021 maijin <maijin21@gmail.com>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_core.h>
6 
7 static const char *syscallNumber(int n) {
8  return sdb_fmt(n > 1000 ? "0x%x" : "%d", n);
9 }
10 
27  int i;
28  char str[64];
29  st64 N = n;
30  int defVector = rz_syscall_get_swi(core->analysis->syscall);
31  if (defVector > 0) {
32  n = -1;
33  }
34  if (n == -1 || defVector > 0) {
35  n = (int)rz_core_reg_getv_by_role_or_name(core, "oeax");
36  if (!n || n == -1) {
37  const char *a0 = rz_reg_get_name(core->analysis->reg, RZ_REG_NAME_SN);
38  n = (a0 == NULL) ? -1 : (int)rz_core_reg_getv_by_role_or_name(core, a0);
39  }
40  }
41  RzSyscallItem *item = rz_syscall_get(core->analysis->syscall, n, defVector);
42  if (!item) {
43  item = rz_syscall_get(core->analysis->syscall, N, -1);
44  }
45  if (!item) {
46  return rz_str_newf("%s = unknown ()", syscallNumber(n));
47  }
48  char *res = rz_str_newf("%s = %s (", syscallNumber(item->num), item->name);
49  // TODO: move this to rz_syscall
50  const char *cc = rz_analysis_syscc_default(core->analysis);
51  // TODO replace the hardcoded CC with the sdb ones
52  for (i = 0; i < item->args; i++) {
53  // XXX this is a hack to make syscall args work on x86-32 and x86-64
54  // we need to shift sn first.. which is bad, but needs to be redesigned
55  int regidx = i;
56  if (core->rasm->bits == 32 && core->rasm->cur && !strcmp(core->rasm->cur->arch, "x86")) {
57  regidx++;
58  }
59  ut64 arg = rz_core_arg_get(core, cc, regidx); // TODO here
60  // rz_cons_printf ("(%d:0x%"PFMT64x")\n", i, arg);
61  if (item->sargs) {
62  switch (item->sargs[i]) {
63  case 'p': // pointer
64  res = rz_str_appendf(res, "0x%08" PFMT64x "", arg);
65  break;
66  case 'i':
67  res = rz_str_appendf(res, "%" PFMT64u "", arg);
68  break;
69  case 'z':
70  memset(str, 0, sizeof(str));
71  rz_io_read_at(core->io, arg, (ut8 *)str, sizeof(str) - 1);
73  res = rz_str_appendf(res, "\"%s\"", str);
74  break;
75  case 'Z': {
76  // TODO replace the hardcoded CC with the sdb ones
77  ut64 len = rz_core_arg_get(core, cc, i + 2);
78  len = RZ_MIN(len + 1, sizeof(str) - 1);
79  if (len == 0) {
80  len = 16; // override default
81  }
82  (void)rz_io_read_at(core->io, arg, (ut8 *)str, len);
83  str[len] = 0;
85  res = rz_str_appendf(res, "\"%s\"", str);
86  } break;
87  default:
88  res = rz_str_appendf(res, "0x%08" PFMT64x "", arg);
89  break;
90  }
91  } else {
92  res = rz_str_appendf(res, "0x%08" PFMT64x "", arg);
93  }
94  if (i + 1 < item->args) {
95  res = rz_str_appendf(res, ", ");
96  }
97  }
99  return rz_str_appendf(res, ")");
100 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
RZ_DEPRECATE RZ_API ut64 rz_core_arg_get(RzCore *core, const char *cc, int num)
Get the value of the num-th argument from the current debug or emulation state.
Definition: carg.c:13
RZ_API const char * rz_analysis_syscc_default(RzAnalysis *analysis)
Definition: cc.c:210
#define RZ_API
RZ_API ut64 rz_core_reg_getv_by_role_or_name(RzCore *core, const char *name)
rz_reg_getv_by_role_or_name() on rz_core_reg_default()
Definition: creg.c:24
#define NULL
Definition: cris-opc.c:27
RZ_API RZ_OWN char * rz_core_syscall_as_string(RzCore *core, st64 n, ut64 addr)
Returns the syscall representation as a string.
Definition: csyscall.c:26
static const char * syscallNumber(int n)
Definition: csyscall.c:7
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
a0
Definition: insn-good.s.cs:704
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
int n
Definition: mipsasm.c:19
RZ_API const char * rz_reg_get_name(RzReg *reg, int role)
Definition: reg.c:147
RZ_API bool rz_io_read_at(RzIO *io, ut64 addr, ut8 *buf, int len)
Definition: io.c:300
@ RZ_REG_NAME_SN
Definition: rz_reg.h:70
RZ_API char * rz_str_appendf(char *ptr, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API void rz_str_filter(char *str)
Convert all non-printable characters in str with '.'.
Definition: str.c:2359
#define RZ_OWN
Definition: rz_types.h:62
#define PFMT64u
Definition: rz_types.h:395
#define PFMT64x
Definition: rz_types.h:393
#define RZ_MIN(x, y)
#define st64
Definition: rz_types_base.h:10
static int
Definition: sfsocketcall.h:114
RzSyscall * syscall
Definition: rz_analysis.h:570
int bits
Definition: rz_asm.h:100
_RzAsmPlugin * cur
Definition: rz_asm.h:106
RzAsm * rasm
Definition: rz_core.h:323
RzAnalysis * analysis
Definition: rz_core.h:322
RzIO * io
Definition: rz_core.h:313
RZ_API void rz_syscall_item_free(RzSyscallItem *si)
Definition: syscall.c:325
RZ_API int rz_syscall_get_swi(RzSyscall *s)
Definition: syscall.c:341
RZ_API RzSyscallItem * rz_syscall_get(RzSyscall *s, int num, int swi)
Definition: syscall.c:345
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58
#define N
Definition: zip_err_str.c:8