Rizin
unix-like reverse engineering framework and cli tools
generic-x86.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 /* definition */
5 
6 // static RzList *backtrace_frames_x86_32(RzDebug *dbg, ut64 at);
7 // static RzList *backtrace_frames_x86_32_analysis(RzDebug *dbg, ut64 at);
8 
9 /* implementation */
10 #include <rz_debug.h>
11 
13  RzRegItem *ri;
14  RzReg *reg = dbg->reg;
15  ut32 i, _esp, esp, ebp2;
16  RzList *list = rz_list_new();
17  RzIOBind *bio = &dbg->iob;
18  ut8 buf[4];
19 
20  list->free = free;
21  ri = (at == UT64_MAX) ? rz_reg_get(reg, "ebp", RZ_REG_TYPE_GPR) : NULL;
22  _esp = (ut32)((ri) ? rz_reg_get_value(reg, ri) : at);
23  // TODO: implement [stack] map uptrace method too
24  esp = _esp;
25  for (i = 0; i < dbg->btdepth; i++) {
26  bio->read_at(bio->io, esp, (void *)&ebp2, 4);
27  if (ebp2 == UT32_MAX)
28  break;
29  *buf = '\0';
30  bio->read_at(bio->io, (ebp2 - 5) - (ebp2 - 5) % 4, (void *)&buf, 4);
31 
32  // TODO: arch_is_call() here and this fun will be portable
33  if (buf[(ebp2 - 5) % 4] == 0xe8) {
35  frame->addr = ebp2;
36  frame->size = esp - _esp;
37  rz_list_append(list, frame);
38  }
39  esp += 4;
40  }
41  return list;
42 }
43 
44 /* TODO: Can I use this as in a coroutine? */
46  RzRegItem *ri;
47  RzReg *reg = dbg->reg;
48  ut32 i, _esp, esp, eip, ebp2;
49  RzList *list;
50  RzIOBind *bio = &dbg->iob;
51  RzAnalysisFunction *fcn;
52  RzDebugFrame *frame;
53  ut8 buf[4];
54 
55  // TODO : frame->size by using esil to emulate first instructions
56  list = rz_list_new();
57  list->free = free;
58 
59  ri = (at == UT64_MAX) ? rz_reg_get(reg, "ebp", RZ_REG_TYPE_GPR) : NULL;
60  _esp = (ut32)((ri) ? rz_reg_get_value(reg, ri) : at);
61  // TODO: implement [stack] map uptrace method too
62  esp = _esp;
63 
66  if (fcn != NULL) {
67  frame = RZ_NEW0(RzDebugFrame);
68  frame->addr = eip;
69  frame->size = 0;
70  rz_list_append(list, frame);
71  }
72 
73  for (i = 1; i < dbg->btdepth; i++) {
74  bio->read_at(bio->io, esp, (void *)&ebp2, 4);
75  if (ebp2 == UT32_MAX)
76  break;
77  *buf = '\0';
78  bio->read_at(bio->io, (ebp2 - 5) - (ebp2 - 5) % 4, (void *)&buf, 4);
79 
80  // TODO: arch_is_call() here and this fun will be portable
81  if (buf[(ebp2 - 5) % 4] == 0xe8) {
82  frame = RZ_NEW0(RzDebugFrame);
83  frame->addr = ebp2;
84  frame->size = esp - _esp;
85  frame->sp = _esp;
86  frame->bp = _esp + frame->size;
87  rz_list_append(list, frame);
88  }
89  esp += 4;
90  }
91  return list;
92 }
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
uint32_t ut32
RzDebug * dbg
Definition: desil.c:30
RZ_DEPRECATE RZ_API RzAnalysisFunction * rz_analysis_get_fcn_in(RzAnalysis *analysis, ut64 addr, int type)
Definition: fcn.c:1687
static RzList * backtrace_x86_32(RzDebug *dbg, ut64 at)
Definition: generic-x86.c:12
static RzList * backtrace_x86_32_analysis(RzDebug *dbg, ut64 at)
Definition: generic-x86.c:45
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
#define reg(n)
uint8_t ut8
Definition: lh5801.h:11
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 RzRegItem * rz_reg_get(RzReg *reg, const char *name, int type)
Definition: reg.c:344
RZ_API ut64 rz_reg_get_value(RzReg *reg, RzRegItem *item)
Definition: rvalue.c:114
@ RZ_ANALYSIS_FCN_TYPE_NULL
Definition: rz_analysis.h:192
@ RZ_REG_TYPE_GPR
Definition: rz_reg.h:21
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define UT32_MAX
Definition: rz_types_base.h:99
#define UT64_MAX
Definition: rz_types_base.h:86
int btdepth
Definition: rz_debug.h:259
RzAnalysis * analysis
Definition: rz_debug.h:305
RzReg * reg
Definition: rz_debug.h:286
RzIOBind iob
Definition: rz_debug.h:293
RzIOReadAt read_at
Definition: rz_io.h:240
RzIO * io
Definition: rz_io.h:232
ut64(WINAPI *w32_GetEnabledXStateFeatures)()