Skip to content

Commit 6d5811d

Browse files
Merge pull request #60 from CowBoy4mH3LL/master
Modified gdbstub.c to support hooking
2 parents a6f0632 + 9de0107 commit 6d5811d

File tree

1 file changed

+65
-42
lines changed

1 file changed

+65
-42
lines changed

gdbstub.c

Lines changed: 65 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@
6363
static int phy_memory_mode;
6464
#endif
6565

66-
static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
66+
67+
void (*signal_handler)(int);
68+
void set_signal_callback(void (*sg)(int)){
69+
signal_handler = sg;
70+
}
71+
72+
int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
6773
uint8_t *buf, int len, bool is_write)
6874
{
6975
CPUClass *cc;
@@ -377,7 +383,7 @@ typedef struct GDBState {
377383
static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
378384

379385
/* Retrieves flags for single step mode. */
380-
static int get_sstep_flags(void)
386+
int get_sstep_flags(void)
381387
{
382388
/*
383389
* In replay mode all events written into the log should be replayed.
@@ -467,7 +473,7 @@ int use_gdb_syscalls(void)
467473
}
468474

469475
/* Resume execution. */
470-
static inline void gdb_continue(void)
476+
void gdb_continue(void)
471477
{
472478

473479
#ifdef CONFIG_USER_ONLY
@@ -920,7 +926,7 @@ static const char *get_feature_xml(const char *p, const char **newp,
920926
return name ? xml_builtin[i][1] : NULL;
921927
}
922928

923-
static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
929+
int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
924930
{
925931
CPUClass *cc = CPU_GET_CLASS(cpu);
926932
CPUArchState *env = cpu->env_ptr;
@@ -938,7 +944,7 @@ static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
938944
return 0;
939945
}
940946

941-
static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
947+
int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
942948
{
943949
CPUClass *cc = CPU_GET_CLASS(cpu);
944950
CPUArchState *env = cpu->env_ptr;
@@ -1017,7 +1023,7 @@ static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
10171023
}
10181024
#endif
10191025

1020-
static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
1026+
int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
10211027
{
10221028
CPUState *cpu;
10231029
int err = 0;
@@ -1054,7 +1060,7 @@ static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
10541060
}
10551061
}
10561062

1057-
static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
1063+
int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
10581064
{
10591065
CPUState *cpu;
10601066
int err = 0;
@@ -1122,7 +1128,7 @@ static void gdb_breakpoint_remove_all(void)
11221128
}
11231129
}
11241130

1125-
static void gdb_set_cpu_pc(target_ulong pc)
1131+
void gdb_set_cpu_pc(target_ulong pc)
11261132
{
11271133
CPUState *cpu = gdbserver_state.c_cpu;
11281134

@@ -3129,49 +3135,66 @@ static void create_default_process(GDBState *s)
31293135
}
31303136

31313137
#ifdef CONFIG_USER_ONLY
3138+
char dbg[100];
31323139
int
31333140
gdb_handlesig(CPUState *cpu, int sig)
31343141
{
3135-
char buf[256];
3136-
int n;
3142+
// sprintf(dbg, "sig: %d init: %d fd: %d\n", sig, gdbserver_state.init, gdbserver_state.fd);
3143+
// qemu_plugin_outs(dbg);
31373144

3138-
if (!gdbserver_state.init || gdbserver_state.fd < 0) {
3139-
return sig;
3145+
if (signal_handler){
3146+
if (sig == GDB_SIGNAL_TRAP){
3147+
signal_handler(sig);
3148+
}
3149+
else{
3150+
gdbserver_state.signal = sig;
3151+
}
31403152
}
3153+
else{
31413154

3142-
/* disable single step if it was enabled */
3143-
cpu_single_step(cpu, 0);
3144-
tb_flush(cpu);
3155+
char buf[256];
3156+
int n;
31453157

3146-
if (sig != 0) {
3147-
snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
3148-
put_packet(buf);
3149-
}
3150-
/* put_packet() might have detected that the peer terminated the
3151-
connection. */
3152-
if (gdbserver_state.fd < 0) {
3153-
return sig;
3154-
}
3158+
if (!gdbserver_state.init || gdbserver_state.fd < 0) {
3159+
return sig;
3160+
}
31553161

3156-
sig = 0;
3157-
gdbserver_state.state = RS_IDLE;
3158-
gdbserver_state.running_state = 0;
3159-
while (gdbserver_state.running_state == 0) {
3160-
n = read(gdbserver_state.fd, buf, 256);
3161-
if (n > 0) {
3162-
int i;
3162+
/* disable single step if it was enabled */
3163+
cpu_single_step(cpu, 0);
3164+
tb_flush(cpu);
3165+
31633166

3164-
for (i = 0; i < n; i++) {
3165-
gdb_read_byte(buf[i]);
3167+
if (sig != 0) {
3168+
snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
3169+
put_packet(buf);
3170+
}
3171+
/* put_packet() might have detected that the peer terminated the
3172+
connection. */
3173+
if (gdbserver_state.fd < 0) {
3174+
return sig;
3175+
}
3176+
3177+
sig = 0;
3178+
gdbserver_state.state = RS_IDLE;
3179+
gdbserver_state.running_state = 0;
3180+
while (gdbserver_state.running_state == 0) {
3181+
n = read(gdbserver_state.fd, buf, 256);
3182+
if (n > 0) {
3183+
int i;
3184+
3185+
for (i = 0; i < n; i++) {
3186+
gdb_read_byte(buf[i]);
3187+
}
31663188
}
3167-
} else {
3168-
/* XXX: Connection closed. Should probably wait for another
3169-
connection before continuing. */
3170-
if (n == 0) {
3171-
close(gdbserver_state.fd);
3189+
else {
3190+
/* XXX: Connection closed. Should probably wait for another
3191+
connection before continuing. */
3192+
if (n == 0) { //Do not close connection if in afl patching mode
3193+
close(gdbserver_state.fd);
3194+
}
3195+
gdbserver_state.fd = -1;
3196+
return sig;
31723197
}
3173-
gdbserver_state.fd = -1;
3174-
return sig;
31753198
}
31763199
}
31773200
sig = gdbserver_state.signal;
@@ -3192,7 +3215,7 @@ void gdb_signalled(CPUArchState *env, int sig)
31923215
put_packet(buf);
31933216
}
31943217

3195-
static void gdb_accept_init(int fd)
3218+
void gdb_accept_init(int fd)
31963219
{
31973220
init_gdbserver_state();
31983221
create_default_process(&gdbserver_state);
@@ -3562,4 +3585,4 @@ static void register_types(void)
35623585
}
35633586

35643587
type_init(register_types);
3565-
#endif
3588+
#endif

0 commit comments

Comments
 (0)