Rizin
unix-like reverse engineering framework and cli tools
core.h File Reference
#include "rz_types.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "../libgdbr.h"
#include "../utils.h"
#include "../arch.h"

Go to the source code of this file.

Macros

#define CMD_ATTACH   "vAttach;"
 
#define CMD_DETACH_MP   "D;"
 
#define CMD_KILL_MP   "vKill;"
 
#define CMD_READREGS   "g"
 
#define CMD_WRITEREGS   "G"
 
#define CMD_READREG   "p"
 
#define CMD_WRITEREG   "P"
 
#define CMD_WRITEMEM   "M"
 
#define CMD_READMEM   "m"
 
#define CMD_BP   "Z0"
 
#define CMD_RBP   "z0"
 
#define CMD_HBP   "Z1"
 
#define CMD_RHBP   "z1"
 
#define CMD_HWW   "Z2"
 
#define CMD_RHWW   "z2"
 
#define CMD_HWR   "Z3"
 
#define CMD_RHWR   "z3"
 
#define CMD_HWA   "Z4"
 
#define CMD_RHWA   "z4"
 
#define CMD_QRCMD   "qRcmd,"
 
#define CMD_C   "vCont"
 
#define CMD_C_CONT   "c"
 
#define CMD_C_CONT_SIG   "C"
 
#define CMD_C_STEP   "s"
 

Enumerations

enum  Breakpoint {
  BREAKPOINT , HARDWARE_BREAKPOINT , WRITE_WATCHPOINT , READ_WATCHPOINT ,
  ACCESS_WATCHPOINT , BREAKPOINT , HARDWARE_BREAKPOINT , WRITE_WATCHPOINT ,
  READ_WATCHPOINT , ACCESS_WATCHPOINT
}
 

Functions

int send_vcont (libgdbr_t *g, const char *command, const char *thread_id)
 Function sends a vCont command to the gdbserver. More...
 
int set_bp (libgdbr_t *g, ut64 address, const char *conditions, enum Breakpoint type, int sizebp)
 
int remove_bp (libgdbr_t *g, ut64 address, enum Breakpoint type, int sizebp)
 

Macro Definition Documentation

◆ CMD_ATTACH

#define CMD_ATTACH   "vAttach;"

Definition at line 19 of file core.h.

◆ CMD_BP

#define CMD_BP   "Z0"

Definition at line 30 of file core.h.

◆ CMD_C

#define CMD_C   "vCont"

Definition at line 41 of file core.h.

◆ CMD_C_CONT

#define CMD_C_CONT   "c"

Definition at line 42 of file core.h.

◆ CMD_C_CONT_SIG

#define CMD_C_CONT_SIG   "C"

Definition at line 43 of file core.h.

◆ CMD_C_STEP

#define CMD_C_STEP   "s"

Definition at line 44 of file core.h.

◆ CMD_DETACH_MP

#define CMD_DETACH_MP   "D;"

Definition at line 20 of file core.h.

◆ CMD_HBP

#define CMD_HBP   "Z1"

Definition at line 32 of file core.h.

◆ CMD_HWA

#define CMD_HWA   "Z4"

Definition at line 38 of file core.h.

◆ CMD_HWR

#define CMD_HWR   "Z3"

Definition at line 36 of file core.h.

◆ CMD_HWW

#define CMD_HWW   "Z2"

Definition at line 34 of file core.h.

◆ CMD_KILL_MP

#define CMD_KILL_MP   "vKill;"

Definition at line 21 of file core.h.

◆ CMD_QRCMD

#define CMD_QRCMD   "qRcmd,"

Definition at line 40 of file core.h.

◆ CMD_RBP

#define CMD_RBP   "z0"

Definition at line 31 of file core.h.

◆ CMD_READMEM

#define CMD_READMEM   "m"

Definition at line 28 of file core.h.

◆ CMD_READREG

#define CMD_READREG   "p"

Definition at line 25 of file core.h.

◆ CMD_READREGS

#define CMD_READREGS   "g"

Definition at line 23 of file core.h.

◆ CMD_RHBP

#define CMD_RHBP   "z1"

Definition at line 33 of file core.h.

◆ CMD_RHWA

#define CMD_RHWA   "z4"

Definition at line 39 of file core.h.

◆ CMD_RHWR

#define CMD_RHWR   "z3"

Definition at line 37 of file core.h.

◆ CMD_RHWW

#define CMD_RHWW   "z2"

Definition at line 35 of file core.h.

◆ CMD_WRITEMEM

#define CMD_WRITEMEM   "M"

Definition at line 27 of file core.h.

◆ CMD_WRITEREG

#define CMD_WRITEREG   "P"

Definition at line 26 of file core.h.

◆ CMD_WRITEREGS

#define CMD_WRITEREGS   "G"

Definition at line 24 of file core.h.

Enumeration Type Documentation

◆ Breakpoint

enum Breakpoint
Enumerator
BREAKPOINT 
HARDWARE_BREAKPOINT 
WRITE_WATCHPOINT 
READ_WATCHPOINT 
ACCESS_WATCHPOINT 
BREAKPOINT 
HARDWARE_BREAKPOINT 
WRITE_WATCHPOINT 
READ_WATCHPOINT 
ACCESS_WATCHPOINT 

Definition at line 46 of file core.h.

46  {
47  BREAKPOINT,
52 };
@ WRITE_WATCHPOINT
Definition: core.h:49
@ READ_WATCHPOINT
Definition: core.h:50
@ ACCESS_WATCHPOINT
Definition: core.h:51
@ BREAKPOINT
Definition: core.h:47
@ HARDWARE_BREAKPOINT
Definition: core.h:48

Function Documentation

◆ remove_bp()

int remove_bp ( libgdbr_t g,
ut64  address,
enum Breakpoint  type,
int  sizebp 
)

Definition at line 1396 of file core.c.

1396  {
1397  char tmp[255] = { 0 };
1398  int ret = -1;
1399 
1400  if (!g) {
1401  return -1;
1402  }
1403 
1404  switch (type) {
1405  case BREAKPOINT:
1406  ret = snprintf(tmp, sizeof(tmp) - 1, "%s,%" PFMT64x ",%d", CMD_RBP, address, sizebp);
1407  break;
1408  case HARDWARE_BREAKPOINT:
1409  ret = snprintf(tmp, sizeof(tmp) - 1, "%s,%" PFMT64x ",%d", CMD_RHBP, address, sizebp);
1410  break;
1411  case WRITE_WATCHPOINT:
1412  ret = snprintf(tmp, sizeof(tmp) - 1, "%s,%" PFMT64x ",%d", CMD_RHWW, address, sizebp);
1413  break;
1414  case READ_WATCHPOINT:
1415  ret = snprintf(tmp, sizeof(tmp) - 1, "%s,%" PFMT64x ",%d", CMD_RHWR, address, sizebp);
1416  break;
1417  case ACCESS_WATCHPOINT:
1418  ret = snprintf(tmp, sizeof(tmp) - 1, "%s,%" PFMT64x ",%d", CMD_RHWA, address, sizebp);
1419  break;
1420  default:
1421  break;
1422  }
1423  if (ret < 0) {
1424  return ret;
1425  }
1426 
1427  if (!gdbr_lock_enter(g)) {
1428  goto end;
1429  }
1430 
1431  g->stop_reason.is_valid = false;
1432  if ((ret = send_msg(g, tmp)) < 0) {
1433  goto end;
1434  }
1435  if ((ret = read_packet(g, false)) < 0) {
1436  goto end;
1437  }
1438 
1439  ret = handle_removebp(g);
1440 end:
1441  gdbr_lock_leave(g);
1442  return ret;
1443 }
struct @667 g
int read_packet(libgdbr_t *instance, bool vcont)
Definition: packet.c:143
int send_msg(libgdbr_t *g, const char *command)
Definition: common.c:146
snprintf
Definition: kernel.h:364
int type
Definition: mipsasm.c:17
int handle_removebp(libgdbr_t *g)
Definition: responses.c:120
#define PFMT64x
Definition: rz_types.h:393
#define CMD_RHWA
Definition: core.h:39
#define CMD_RBP
Definition: core.h:31
#define CMD_RHBP
Definition: core.h:33
#define CMD_RHWR
Definition: core.h:37
#define CMD_RHWW
Definition: core.h:35
bool gdbr_lock_enter(libgdbr_t *g)
Acquires the gdbr lock and sets up breaking.
Definition: core.c:105
void gdbr_lock_leave(libgdbr_t *g)
Releases the gdbr lock.
Definition: core.c:117

References ACCESS_WATCHPOINT, BREAKPOINT, CMD_RBP, CMD_RHBP, CMD_RHWA, CMD_RHWR, CMD_RHWW, test_evm::end, g, gdbr_lock_enter(), gdbr_lock_leave(), handle_removebp(), HARDWARE_BREAKPOINT, PFMT64x, read_packet(), READ_WATCHPOINT, send_msg(), snprintf, autogen_x86imm::tmp, type, and WRITE_WATCHPOINT.

Referenced by gdbr_remove_bp(), gdbr_remove_hwa(), gdbr_remove_hwbp(), gdbr_remove_hwr(), and gdbr_remove_hww().

◆ send_vcont()

int send_vcont ( libgdbr_t g,
const char *  command,
const char *  thread_id 
)

Function sends a vCont command to the gdbserver.

Parameters
gthre "instance" of the current libgdbr session
commandthe command that will be sent (i.e. 's,S,c,C...')
Returns
-1 if something went wrong

Definition at line 1212 of file core.c.

1212  {
1213  char tmp[255] = { 0 };
1214  int ret = -1;
1215  void *bed = NULL;
1216 
1217  if (!g) {
1218  return -1;
1219  }
1220 
1221  if (!g->stub_features.vContSupported) {
1222  ret = snprintf(tmp, sizeof(tmp) - 1, "%s", command);
1223  } else {
1224  bool supported = false;
1225  switch (*command) {
1226  case 's':
1227  if (g->stub_features.vcont.s) {
1228  supported = true;
1229  }
1230  break;
1231  case 'S':
1232  if (g->stub_features.vcont.S) {
1233  supported = true;
1234  }
1235  break;
1236  case 'c':
1237  if (g->stub_features.vcont.c) {
1238  supported = true;
1239  }
1240  break;
1241  case 'C':
1242  if (g->stub_features.vcont.C) {
1243  supported = true;
1244  }
1245  break;
1246  case 't':
1247  if (g->stub_features.vcont.t) {
1248  supported = true;
1249  }
1250  break;
1251  case 'r':
1252  if (g->stub_features.vcont.r) {
1253  supported = true;
1254  }
1255  break;
1256  }
1257  if (supported) {
1258  if (!thread_id) {
1259  ret = snprintf(tmp, sizeof(tmp) - 1, "%s;%s", CMD_C, command);
1260  } else {
1261  ret = snprintf(tmp, sizeof(tmp) - 1, "%s;%s:%s", CMD_C, command, thread_id);
1262  }
1263  } else {
1264  ret = snprintf(tmp, sizeof(tmp) - 1, "%s", command);
1265  }
1266  }
1267  if (ret < 0) {
1268  return ret;
1269  }
1270 
1271  if (!gdbr_lock_enter(g)) {
1272  goto end;
1273  }
1274  reg_cache.valid = false;
1275  g->stop_reason.is_valid = false;
1276  ret = send_msg(g, tmp);
1277  if (ret < 0) {
1278  goto end;
1279  }
1280 
1281  bed = rz_cons_sleep_begin();
1282  while (read_packet(g, true) < 0 && !g->isbreaked && rz_socket_is_connected(g->sock))
1283  ;
1284  if (g->isbreaked) {
1285  g->isbreaked = false;
1286  // Stop target
1287  rz_socket_write(g->sock, "\x03", 1);
1288  // Read the stop reason
1289  if (read_packet(g, false) < 0) {
1290  ret = -1;
1291  goto end;
1292  }
1293  }
1294 
1295  ret = handle_cont(g);
1296 end:
1297  rz_cons_sleep_end(bed);
1298  gdbr_lock_leave(g);
1299  return ret;
1300 }
RZ_API void * rz_cons_sleep_begin(void)
Definition: cons.c:443
RZ_API void rz_cons_sleep_end(void *user)
Definition: cons.c:450
#define NULL
Definition: cris-opc.c:27
int handle_cont(libgdbr_t *g)
Definition: responses.c:383
RZ_API bool rz_socket_is_connected(RzSocket *)
Definition: socket.c:101
RZ_API int rz_socket_write(RzSocket *s, void *buf, int len)
Definition: socket.c:724
#define CMD_C
Definition: core.h:41
const char * command
Definition: main.c:7
static struct @443 reg_cache

References CMD_C, command, test_evm::end, g, gdbr_lock_enter(), gdbr_lock_leave(), handle_cont(), NULL, read_packet(), reg_cache, rz_cons_sleep_begin(), rz_cons_sleep_end(), rz_socket_is_connected(), rz_socket_write(), send_msg(), snprintf, and autogen_x86imm::tmp.

Referenced by gdbr_continue(), and gdbr_step().

◆ set_bp()

int set_bp ( libgdbr_t g,
ut64  address,
const char *  conditions,
enum Breakpoint  type,
int  sizebp 
)

Definition at line 1302 of file core.c.

1302  {
1303  char tmp[255] = { 0 };
1304  int ret = -1;
1305 
1306  if (!g) {
1307  return -1;
1308  }
1309 
1310  switch (type) {
1311  case BREAKPOINT:
1312  ret = snprintf(tmp, sizeof(tmp) - 1,
1313  "%s,%" PFMT64x ",%d", CMD_BP, address, sizebp);
1314  break;
1315  case HARDWARE_BREAKPOINT:
1316  ret = snprintf(tmp, sizeof(tmp) - 1,
1317  "%s,%" PFMT64x ",%d", CMD_HBP, address, sizebp);
1318  break;
1319  case WRITE_WATCHPOINT:
1320  ret = snprintf(tmp, sizeof(tmp) - 1,
1321  "%s,%" PFMT64x ",%d", CMD_HWW, address, sizebp);
1322  break;
1323  case READ_WATCHPOINT:
1324  ret = snprintf(tmp, sizeof(tmp) - 1,
1325  "%s,%" PFMT64x ",%d", CMD_HWR, address, sizebp);
1326  break;
1327  case ACCESS_WATCHPOINT:
1328  ret = snprintf(tmp, sizeof(tmp) - 1,
1329  "%s,%" PFMT64x ",%d", CMD_HWA, address, sizebp);
1330  break;
1331  default:
1332  break;
1333  }
1334  if (ret < 0) {
1335  return ret;
1336  }
1337 
1338  if (!gdbr_lock_enter(g)) {
1339  goto end;
1340  }
1341 
1342  g->stop_reason.is_valid = false;
1343  if ((ret = send_msg(g, tmp)) < 0) {
1344  goto end;
1345  }
1346 
1347  if ((ret = read_packet(g, false)) < 0) {
1348  goto end;
1349  }
1350 
1351  ret = handle_setbp(g);
1352 end:
1353  gdbr_lock_leave(g);
1354  return ret;
1355 }
int handle_setbp(libgdbr_t *g)
Definition: responses.c:116
#define CMD_BP
Definition: core.h:30
#define CMD_HWR
Definition: core.h:36
#define CMD_HBP
Definition: core.h:32
#define CMD_HWW
Definition: core.h:34
#define CMD_HWA
Definition: core.h:38

References ACCESS_WATCHPOINT, BREAKPOINT, CMD_BP, CMD_HBP, CMD_HWA, CMD_HWR, CMD_HWW, test_evm::end, g, gdbr_lock_enter(), gdbr_lock_leave(), handle_setbp(), HARDWARE_BREAKPOINT, PFMT64x, read_packet(), READ_WATCHPOINT, send_msg(), snprintf, autogen_x86imm::tmp, type, and WRITE_WATCHPOINT.

Referenced by gdbr_set_bp(), gdbr_set_hwa(), gdbr_set_hwbp(), gdbr_set_hwr(), and gdbr_set_hww().