Rizin
unix-like reverse engineering framework and cli tools
dreg.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2018 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_core.h> // just to get the RzPrint instance
5 #include <rz_debug.h>
6 #include <rz_cons.h>
7 #include <rz_reg.h>
8 
10  int i, n, size;
11  if (!dbg || !dbg->reg || !dbg->cur) {
12  return false;
13  }
14  // There's no point in syncing a dead target
15  if (rz_debug_is_dead(dbg)) {
16  return false;
17  }
18  // Check if the functions needed are available
19  if (write && !dbg->cur->reg_write) {
20  return false;
21  }
22  if (!write && !dbg->cur->reg_read) {
23  return false;
24  }
25  // Sync all the types sequentially if asked
27  // Check to get the correct arena when using @ into reg profile (arena!=type)
28  // if request type is positive and the request regset don't have regs
29  if (i >= RZ_REG_TYPE_GPR && dbg->reg->regset[i].regs && !dbg->reg->regset[i].regs->length) {
30  // seek into the other arena for redirections.
31  for (n = RZ_REG_TYPE_GPR; n < RZ_REG_TYPE_LAST; n++) {
32  // get regset mask
33  int mask = dbg->reg->regset[n].maskregstype;
34  // convert request arena to mask value
35  int v = ((int)1 << i);
36  // skip checks on same request arena and check if this arena have inside the request arena type
37  if (n != i && (mask & v)) {
38  // eprintf(" req = %i arena = %i mask = %x search = %x \n", i, n, mask, v);
39  // eprintf(" request arena %i found at arena %i\n", i, n );
40  // if this arena have the request arena type, force to use this arena.
41  i = n;
42  break;
43  }
44  }
45  }
46  do {
47  if (write) {
49  if (!buf || !dbg->cur->reg_write(dbg, i, buf, size)) {
50  if (i == RZ_REG_TYPE_GPR) {
51  eprintf("rz_debug_reg: error writing "
52  "registers %d to %d\n",
53  i, dbg->tid);
54  }
55  if (type != RZ_REG_TYPE_ANY || i == RZ_REG_TYPE_GPR) {
56  free(buf);
57  return false;
58  }
59  }
60  free(buf);
61  } else {
62  // int bufsize = RZ_MAX (1024, dbg->reg->size*2); // i know. its hacky
63  int bufsize = dbg->reg->size;
64  // int bufsize = dbg->reg->regset[i].arena->size;
65  if (bufsize > 0) {
66  ut8 *buf = calloc(1 + 1, bufsize);
67  if (!buf) {
68  return false;
69  }
70  // we have already checked dbg->h and dbg->h->reg_read above
71  size = dbg->cur->reg_read(dbg, i, buf, bufsize);
72  // we need to check against zero because reg_read can return false
73  if (size > 0) {
74  rz_reg_set_bytes(dbg->reg, i, buf, size); // RZ_MIN (size, bufsize));
75  // free (buf);
76  // return true;
77  }
78  free(buf);
79  }
80  }
81  // DO NOT BREAK RZ_REG_TYPE_ANY PLEASE
82  // break;
83  // Continue the synchronization or just stop if it was asked only for a single type of regs
84  i++;
85  } while ((type == RZ_REG_TYPE_ANY) && (i < RZ_REG_TYPE_LAST));
86  return true;
87 }
88 
89 RZ_API int rz_debug_reg_set(struct rz_debug_t *dbg, const char *name, ut64 num) {
91  if (!ri) {
92  return false;
93  }
94  rz_reg_set_value(dbg->reg, ri, num);
96  return true;
97 }
98 
102 }
103 
104 RZ_API ut64 rz_debug_num_callback(RzNum *userptr, const char *str, int *ok) {
105  RzDebug *dbg = (RzDebug *)userptr;
108  if (!ri) {
109  *ok = 0;
110  return UT64_MAX;
111  }
112  *ok = 1;
113  return rz_reg_get_value(dbg->reg, ri);
114 }
115 
117  if (dbg->cur->reg_profile) {
118  char *p = dbg->cur->reg_profile(dbg);
119  if (p) {
122  free(p);
123  } else {
124  RZ_LOG_WARN("Cannot retrieve reg profile from debug plugin (%s)\n", dbg->cur->name);
125  return false;
126  }
127  }
128  return true;
129 }
#define mask()
lzma_index ** i
Definition: index.h:629
RZ_API bool rz_reg_set_bytes(RzReg *reg, int type, const ut8 *buf, const int len)
Definition: arena.c:92
RZ_API ut8 * rz_reg_get_bytes(RzReg *reg, int type, int *size)
Definition: arena.c:8
#define RZ_API
static static fork write
Definition: sflib.h:33
RzDebug * dbg
Definition: desil.c:30
RZ_API ut64 rz_debug_num_callback(RzNum *userptr, const char *str, int *ok)
Definition: dreg.c:104
RZ_API ut64 rz_debug_reg_get(RzDebug *dbg, const char *name)
Definition: dreg.c:99
RZ_API int rz_debug_reg_sync(RzDebug *dbg, int type, int write)
Definition: dreg.c:9
RZ_API int rz_debug_reg_set(struct rz_debug_t *dbg, const char *name, ut64 num)
Definition: dreg.c:89
RZ_API bool rz_debug_reg_profile_sync(RzDebug *dbg)
Definition: dreg.c:116
const char * v
Definition: dsignal.c:12
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
void * p
Definition: libc.cpp:67
RZ_API bool rz_debug_is_dead(RzDebug *dbg)
Definition: debug.c:1632
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 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
@ ok
Definition: lz4.c:1706
int n
Definition: mipsasm.c:19
int type
Definition: mipsasm.c:17
RZ_API bool rz_reg_set_profile_string(RZ_NONNULL RzReg *reg, RZ_NONNULL const char *profile_str)
Parses a register profile string and sets up all registers accordingly in reg.
Definition: profile.c:431
RZ_API RzRegItem * rz_reg_get_by_role_or_name(RzReg *reg, const char *name)
Definition: reg.c:378
RZ_API ut64 rz_reg_getv_by_role_or_name(RzReg *reg, const char *name)
Definition: reg.c:338
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API bool rz_reg_set_value(RzReg *reg, RzRegItem *item, ut64 value)
Definition: rvalue.c:186
RZ_API ut64 rz_reg_get_value(RzReg *reg, RzRegItem *item)
Definition: rvalue.c:114
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
@ RZ_REG_TYPE_GPR
Definition: rz_reg.h:21
@ RZ_REG_TYPE_LAST
Definition: rz_reg.h:34
@ RZ_REG_TYPE_ANY
Definition: rz_reg.h:35
#define UT64_MAX
Definition: rz_types_base.h:86
static int
Definition: sfsocketcall.h:114
Definition: z80asm.h:102
int(* reg_read)(RzDebug *dbg, int type, ut8 *buf, int size)
Callback to be used for RzBreakpoint. When called, RzBreakpoint.user points to the RzDebug.
Definition: rz_debug.h:392
int(* reg_write)(RzDebug *dbg, int type, const ut8 *buf, int size)
Definition: rz_debug.h:393
const char * name
Definition: rz_debug.h:359
char *(* reg_profile)(RzDebug *dbg)
Definition: rz_debug.h:394
struct rz_debug_plugin_t * cur
Definition: rz_debug.h:295
RzReg * reg
Definition: rz_debug.h:286
ut32 length
Definition: rz_list.h:22
ut32 maskregstype
which type of regs has this register set (logic mask with 1 << RZ_REG_TYPE_XXX)
Definition: rz_reg.h:141
RzList * regs
RzRegItem.
Definition: rz_reg.h:138
int size
Definition: rz_reg.h:156
RzRegSet regset[RZ_REG_TYPE_LAST]
Definition: rz_reg.h:150
ut64(WINAPI *w32_GetEnabledXStateFeatures)()