Rizin
unix-like reverse engineering framework and cli tools
libgdbr.c File Reference
#include "libgdbr.h"
#include "arch.h"
#include <stdio.h>
#include "reg/x86_32.h"
#include "reg/x86_64.h"
#include "reg/arm32.h"
#include "reg/arm64.h"
#include "reg/sh.h"
#include "reg/lm32.h"
#include "reg/riscv64.h"
#include "reg/mips.h"
#include "reg/avr.h"
#include "reg/v850.h"

Go to the source code of this file.

Functions

int gdbr_init (libgdbr_t *g, bool is_server)
 Function initializes the libgdbr lib. More...
 
bool gdbr_set_architecture (libgdbr_t *g, int arch, int bits)
 Function initializes the architecture of the gdbsession. More...
 
char * gdbr_get_reg_profile (int arch, int bits)
 Function get gdb registers profile based on arch and bits. More...
 
int gdbr_set_reg_profile (libgdbr_t *g, const char *str)
 Function set the gdbr internal registers profile. More...
 
int gdbr_cleanup (libgdbr_t *g)
 frees all buffers and cleans the libgdbr instance stuff More...
 

Function Documentation

◆ gdbr_cleanup()

int gdbr_cleanup ( libgdbr_t g)

frees all buffers and cleans the libgdbr instance stuff

Returns
a failure code (currently -1) or 0 if call successfully

Definition at line 146 of file libgdbr.c.

146  {
147  if (!g) {
148  return -1;
149  }
150  RZ_FREE(g->data);
151  g->send_len = 0;
152  RZ_FREE(g->send_buff);
153  RZ_FREE(g->read_buff);
154  rz_socket_free(g->sock);
155  rz_th_lock_free(g->gdbr_lock);
156  return 0;
157 }
struct @667 g
RZ_API int rz_socket_free(RzSocket *s)
Definition: socket.c:453
#define RZ_FREE(x)
Definition: rz_types.h:369
RZ_API void rz_th_lock_free(RZ_NULLABLE RzThreadLock *thl)
Frees a RzThreadLock structure.
Definition: thread_lock.c:89

References g, RZ_FREE, rz_socket_free(), and rz_th_lock_free().

Referenced by __close(), and rz_core_rtr_gdb_run().

◆ gdbr_get_reg_profile()

char* gdbr_get_reg_profile ( int  arch,
int  bits 
)

Function get gdb registers profile based on arch and bits.

Parameters
architectureand bit size.
Returns
a failure code

Definition at line 76 of file libgdbr.c.

76  {
77  switch (arch) {
78  case RZ_SYS_ARCH_X86:
79  if (bits == 32) {
80 #include "reg/x86_32.h"
81  } else if (bits == 64) {
82 #include "reg/x86_64.h"
83  } else {
84  eprintf("%s: unsupported x86 bits: %d\n", __func__, bits);
85  return NULL;
86  }
87  break;
88  case RZ_SYS_ARCH_ARM:
89  if (bits == 32) {
90 #include "reg/arm32.h"
91  } else if (bits == 64) {
92 #include "reg/arm64.h"
93  } else {
94  eprintf("%s: unsupported arm bits: %d\n", __func__, bits);
95  return NULL;
96  }
97  break;
98  case RZ_SYS_ARCH_SH:
99 #include "reg/sh.h"
100  break;
101  case RZ_SYS_ARCH_LM32:
102 #include "reg/lm32.h"
103  break;
104  case RZ_SYS_ARCH_RISCV:
105  if (bits == 64) {
106 #include "reg/riscv64.h"
107  } else {
108  eprintf("%s: unsupported riscv bits: %d\n", __func__, bits);
109  return NULL;
110  }
111  break;
112  case RZ_SYS_ARCH_MIPS:
113 #include "reg/mips.h"
114  break;
115  case RZ_SYS_ARCH_AVR:
116 #include "reg/avr.h"
117  break;
118  case RZ_SYS_ARCH_V850:
119 #include "reg/v850.h"
120  break;
121  }
122  return NULL;
123 }
int bits(struct state *s, int need)
Definition: blast.c:72
#define NULL
Definition: cris-opc.c:27
cs_arch arch
Definition: cstool.c:13
#define eprintf(x, y...)
Definition: rlcc.c:7
@ RZ_SYS_ARCH_MIPS
Definition: rz_types.h:537
@ RZ_SYS_ARCH_RISCV
Definition: rz_types.h:564
@ RZ_SYS_ARCH_SH
Definition: rz_types.h:543
@ RZ_SYS_ARCH_AVR
Definition: rz_types.h:544
@ RZ_SYS_ARCH_V850
Definition: rz_types.h:555
@ RZ_SYS_ARCH_LM32
Definition: rz_types.h:563
@ RZ_SYS_ARCH_X86
Definition: rz_types.h:532
@ RZ_SYS_ARCH_ARM
Definition: rz_types.h:533

References arch, bits(), eprintf, NULL, RZ_SYS_ARCH_ARM, RZ_SYS_ARCH_AVR, RZ_SYS_ARCH_LM32, RZ_SYS_ARCH_MIPS, RZ_SYS_ARCH_RISCV, RZ_SYS_ARCH_SH, RZ_SYS_ARCH_V850, and RZ_SYS_ARCH_X86.

Referenced by gdbr_set_architecture(), and rz_debug_gdb_reg_profile().

◆ gdbr_init()

int gdbr_init ( libgdbr_t g,
bool  is_server 
)

Function initializes the libgdbr lib.

Returns
a failure code (currently -1) or 0 if call successfully

Definition at line 9 of file libgdbr.c.

9  {
10  if (!g) {
11  return -1;
12  }
13  memset(g, 0, sizeof(libgdbr_t));
14  g->no_ack = false;
15  g->stub_features.extended_mode = -1;
16  g->stub_features.pkt_sz = 64;
17  g->stub_features.P = true;
18  g->remote_file_fd = -1;
19  g->is_server = is_server;
20  g->send_max = 2500;
21  g->send_buff = (char *)calloc(g->send_max, 1);
22  g->page_size = 4096;
23  g->num_retries = 40; // safe number, should be ~10 seconds
24  if (!g->send_buff) {
25  return -1;
26  }
27  g->send_len = 0;
28  g->read_max = 4096;
29  g->read_buff = (char *)calloc(g->read_max, 1);
30  if (!g->read_buff) {
31  RZ_FREE(g->send_buff);
32  return -1;
33  }
34  g->sock = rz_socket_new(0);
35  g->gdbr_lock = rz_th_lock_new(true);
36  g->gdbr_lock_depth = 0;
37  g->last_code = MSG_OK;
38  g->connected = 0;
39  g->data_len = 0;
40  g->data_max = 4096;
41  g->data = calloc(g->data_max, 1);
42  if (!g->data) {
43  RZ_FREE(g->send_buff);
44  RZ_FREE(g->read_buff);
45  return -1;
46  }
47  g->remote_type = GDB_REMOTE_TYPE_GDB;
48  g->isbreaked = false;
49  return 0;
50 }
return memset(p, 0, total)
#define GDB_REMOTE_TYPE_GDB
Definition: libgdbr.h:19
#define MSG_OK
Definition: libgdbr.h:15
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
RZ_API RzSocket * rz_socket_new(bool is_ssl)
Definition: socket.c:179
RZ_API RZ_OWN RzThreadLock * rz_th_lock_new(bool recursive)
Allocates and initialize a RzThreadLock structure.
Definition: thread_lock.c:14

References calloc(), g, GDB_REMOTE_TYPE_GDB, memset(), MSG_OK, RZ_FREE, rz_socket_new(), and rz_th_lock_new().

Referenced by __open(), and rz_core_rtr_gdb_run().

◆ gdbr_set_architecture()

bool gdbr_set_architecture ( libgdbr_t g,
int  arch,
int  bits 
)

Function initializes the architecture of the gdbsession.

Parameters
architecturedefines the architecure used (registersize, and such)
Returns
false on failure

Definition at line 52 of file libgdbr.c.

52  {
53  if (!g) {
54  return false;
55  }
56  if (g->target.valid && g->registers) {
57  return true;
58  }
59 
60  char *regprofile = gdbr_get_reg_profile(arch, bits);
61  if (!regprofile) {
62  eprintf("cannot find gdb reg_profile\n");
63  return false;
64  }
65  if (!gdbr_set_reg_profile(g, regprofile)) {
66  free(regprofile);
67  return false;
68  }
69  g->target.arch = arch;
70  g->target.bits = bits;
71  g->target.valid = true;
72 
73  return true;
74 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
int gdbr_set_reg_profile(libgdbr_t *g, const char *str)
Function set the gdbr internal registers profile.
Definition: libgdbr.c:125
char * gdbr_get_reg_profile(int arch, int bits)
Function get gdb registers profile based on arch and bits.
Definition: libgdbr.c:76

References arch, bits(), eprintf, free(), g, gdbr_get_reg_profile(), and gdbr_set_reg_profile().

Referenced by rz_core_rtr_gdb_run(), rz_debug_gdb_attach(), and rz_debug_gdb_reg_profile().

◆ gdbr_set_reg_profile()

int gdbr_set_reg_profile ( libgdbr_t g,
const char *  str 
)

Function set the gdbr internal registers profile.

Parameters
registersprofile string which shares the same format as RzReg API
Returns
a failure code

Definition at line 125 of file libgdbr.c.

125  {
126  if (!g || !str) {
127  return -1;
128  }
129  gdb_reg_t *registers = arch_parse_reg_profile(str);
130  if (!registers) {
131  eprintf("cannot parse reg profile\n");
132  return -1;
133  }
134  if (g->target.regprofile) {
135  free(g->target.regprofile);
136  }
137  g->target.regprofile = strdup(str);
138  if (g->registers) {
139  free(g->registers);
140  }
141  g->registers = arch_parse_reg_profile(str);
142 
143  return 0;
144 }
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")
gdb_reg_t * arch_parse_reg_profile(const char *reg_profile)
Definition: arch.c:46
Definition: arch.h:13

References arch_parse_reg_profile(), eprintf, free(), g, cmd_descs_generate::str, and strdup().

Referenced by gdbr_set_architecture(), and rz_debug_gdb_set_reg_profile().