Rizin
unix-like reverse engineering framework and cli tools
libgdbr.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014 defragger <rlaemmert@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "libgdbr.h"
5 #include "arch.h"
6 
7 #include <stdio.h>
8 
9 int gdbr_init(libgdbr_t *g, bool is_server) {
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 }
51 
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 }
75 
76 char *gdbr_get_reg_profile(int arch, int bits) {
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 }
124 
125 int gdbr_set_reg_profile(libgdbr_t *g, const char *str) {
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 }
145 
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 }
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
struct @667 g
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
return memset(p, 0, total)
int gdbr_set_reg_profile(libgdbr_t *g, const char *str)
Function set the gdbr internal registers profile.
Definition: libgdbr.c:125
bool gdbr_set_architecture(libgdbr_t *g, int arch, int bits)
Function initializes the architecture of the gdbsession.
Definition: libgdbr.c:52
char * gdbr_get_reg_profile(int arch, int bits)
Function get gdb registers profile based on arch and bits.
Definition: libgdbr.c:76
int gdbr_cleanup(libgdbr_t *g)
frees all buffers and cleans the libgdbr instance stuff
Definition: libgdbr.c:146
int gdbr_init(libgdbr_t *g, bool is_server)
Function initializes the libgdbr lib.
Definition: libgdbr.c:9
#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
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")
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API RzSocket * rz_socket_new(bool is_ssl)
Definition: socket.c:179
RZ_API int rz_socket_free(RzSocket *s)
Definition: socket.c:453
@ 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
#define RZ_FREE(x)
Definition: rz_types.h:369
gdb_reg_t * arch_parse_reg_profile(const char *reg_profile)
Definition: arch.c:46
Definition: arch.h:13
RZ_API void rz_th_lock_free(RZ_NULLABLE RzThreadLock *thl)
Frees a RzThreadLock structure.
Definition: thread_lock.c:89
RZ_API RZ_OWN RzThreadLock * rz_th_lock_new(bool recursive)
Allocates and initialize a RzThreadLock structure.
Definition: thread_lock.c:14