-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgdb.c
1320 lines (1167 loc) · 30.5 KB
/
gdb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MIT License
*
* Copyright (c) 2023 Davidson Francis <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include "net.h"
#include "util.h"
#ifdef VERBOSE
#define DUMP_REGS 1
#endif
/* Convert a given SEG:OFF to physical address. */
#define TO_PHYS(S,O) (((S) << 4)+(O))
/* File descriptors for GDB and serial. */
static int gdb_fd;
static int serial_fd;
/* GDB handle states. */
#define GDB_STATE_START 0x1
#define GDB_STATE_CMD 0x2
#define GDB_STATE_CSUM_D1 0x4
#define GDB_STATE_CSUM_D2 0x8
/* Serial handle states. */
#define SERIAL_STATE_START 0x10
#define SERIAL_STATE_ADD_SW_BREAK 0xA8
#define SERIAL_STATE_REM_SW_BREAK 0xB8
#define SERIAL_STATE_SS 0xC8
#define SERIAL_STATE_READ_MEM_CMD 0xD8
#define SERIAL_STATE_CONTINUE 0xE8
#define SERIAL_STATE_WRITE_MEM_CMD 0xF8
#define SERIAL_STATE_REG_WRITE 0xA7
#define SERIAL_STATE_ADD_HW_WATCH 0xB7
#define SERIAL_STATE_REM_HW_WATCH 0xC7
#define SERIAL_MSG_OK 0x04
/* Watch types. */
#define HW_WATCH_WRITE 0x01
#define HW_WATCH_ACCESS 0x03
/* Stop reasons. */
#define STOP_REASON_NORMAL 10
#define STOP_REASON_WATCHPOINT 20
/* The registers are cached, so this flag signals
* if the cache is updated or not. */
static int have_x86_regs = 0;
/* Memory dump helpers. */
static uint8_t *dump_buffer;
static uint32_t last_dump_phys_addr;
static uint16_t last_dump_amnt;
/* Breakpoint cache. */
static uint32_t breakpoint_insn_addr;
/**
* Mini-buffr to hold different byte-sized values
* to send to serial.
*/
union minibuf
{
uint8_t b8[4];
uint16_t b16[2];
uint32_t b32;
};
/**
* Real Mode-dbg x86 regs
*
* This is the one that is sent to us
*/
struct srm_x86_regs
{
uint32_t edi;
uint32_t esi;
uint32_t ebp;
uint32_t esp;
uint32_t ebx;
uint32_t edx;
uint32_t ecx;
uint32_t eax;
uint16_t gs;
uint16_t fs;
uint16_t es;
uint16_t ds;
uint16_t ss;
uint16_t eip;
uint16_t cs;
uint16_t eflags;
} __attribute__((packed));
/**
* x86 stop data
*
* This is the data the is sent to us every time the
* debugger has stopped, whether by single-step,
* breakpoint, signal and etc.
*/
union x86_stop_data
{
struct d
{
struct srm_x86_regs x86_regs;
uint8_t stop_reason;
uint32_t stop_addr;
#ifndef UART_POLLING
uint8_t saved_insns[4];
#endif
} __attribute__((packed)) d;
uint8_t data[sizeof (struct d)];
} x86_stop_data;
/**
* This is the struct that will be passed as-is to GDB
*/
struct sx86_regs
{
uint32_t eax;
uint32_t ecx;
uint32_t edx;
uint32_t ebx;
uint32_t esp;
uint32_t ebp;
uint32_t esi;
uint32_t edi;
uint32_t eip;
uint32_t eflags;
uint32_t cs;
uint32_t ss;
uint32_t ds;
uint32_t es;
uint32_t fs;
uint32_t gs;
} __attribute__((packed));
/* Amount of registers. */
#define MAX_REGS (sizeof(struct sx86_regs)/sizeof(uint32_t))
/**
* The x86 registers that are kept as cache and sent to
* GDB as well. Since these register might be accessed
* from various ways, this union provides an easy way
* to do that.
*/
static union ux86_regs
{
struct sx86_regs r;
uint32_t r32[MAX_REGS];
uint8_t r8[sizeof (struct sx86_regs)];
} x86_regs;
/* ------------------------------------------------------------------*
* GDB commands *
* ------------------------------------------------------------------*/
/**
* @brief Send a GDB command/packet in the format:
* $data#NN, where NN is the checksum modulo 256.
*
* All GDB commands follows the same structure.
*
* @param buff Buffer containing the data to be
* sent.
* @param len Buffer length.
*
* @return Returns 0 if success, -1 otherwise.
*/
static ssize_t send_gdb_cmd(const char *buff, size_t len)
{
size_t i;
int csum;
ssize_t ret;
char csum_str[3];
/* Calculate checksum. */
for (i = 0, csum = 0; i < len; i++)
csum += buff[i];
csum &= 0xFF;
send_all(gdb_fd, "$", 1);
send_all(gdb_fd, buff, len);
send_all(gdb_fd, "#", 1);
snprintf(csum_str, 3, "%02x", csum);
ret = send_all(gdb_fd, csum_str, 2);
if (ret < 0)
errx("Unable to send command to GDB!\n");
return (0);
}
/**
* @brief Send the halt reason to GDB.
*/
static inline void send_gdb_halt_reason(void)
{
char buf[32] = {0};
/* Instruction hw bp, Ctrl+C, or initial break. */
if (x86_stop_data.d.stop_reason == STOP_REASON_NORMAL)
{
send_gdb_cmd("S05", 3);
return;
}
/*
* Hardware watchpoint
* GDB requires another type of message, that tells
* the stop reason and address.
*/
snprintf(buf, sizeof buf - 1, "T05watch:%08x;",
x86_stop_data.d.stop_addr);
send_gdb_cmd(buf, strlen(buf));
}
/**
* @brief Acks a previous message/packet sent from GDB
*/
static inline void send_gdb_ack(void) {
send_all(gdb_fd, "+", 1);
}
/**
* @brief Tells GDB that we do not support the
* receive message/packet.
*/
static inline void send_gdb_unsupported_msg(void) {
send_gdb_cmd(NULL, 0);
}
/**
* @brief Confirms that the previous command was
* successfully executed.
*
* The 'OK' command is generally sent by the serial,
* and then forwarded to GDB, as the serial device is
* the only one that knows if the command succeeded
* or not.
*/
static inline void send_gdb_ok(void) {
send_gdb_cmd("OK", 2);
}
/**
* @brief Tells GDB that something went wrong with
* the latest command.
*/
static inline void send_gdb_error(void) {
send_gdb_cmd("E00", 3);
}
/**
* @brief Sends a 'Ctrl+C' to the serial device.
*/
static void send_serial_ctrlc(void) {
send_serial_byte(3);
}
/* ------------------------------------------------------------------*
* Misc *
* ------------------------------------------------------------------*/
/**
* Read all registers (already in cache), encodes
* them to hex and returns it.
*
* @return Returns all the registers hex-encoded.
*/
static char *read_registers(void)
{
char *buff;
#ifdef MOCK_REGISTERS
int i;
for (i = 0; i < MAX_REGS; i++)
{
if (i & 1)
x86_regs.r32[i] = 0xabcdefAB;
else
x86_regs.r32[i] = 0x12345678;
}
/* Some 'sane' data. */
x86_regs.r.cs = 0x0;
x86_regs.r.ds = 0x100;
x86_regs.r.es = 0x100;
x86_regs.r.fs = 0x100;
x86_regs.r.gs = 0x100;
x86_regs.r.eip = 0x7c00;
#else
if (!have_x86_regs)
errx("Error! registers are not available!\n");
#endif
buff = encode_hex((const char*)x86_regs.r8,
sizeof (struct sx86_regs));
return (buff);
}
#ifdef USE_MOCKS
/**
* @brief Reads from a 'fake' memory (filled with NOPs)
* and returns it.
*
* @param len Desired memory size.
*
* @return Returns the fake memory.
*/
static char *read_mock_memory(size_t len)
{
char *buff, *buff1;
if ((buff = malloc(len)) == NULL)
errx("Unable to allocate %zd bytes!\n", len);
for (size_t i = 0; i < len; i++)
buff[i] = 0x90;
buff1 = encode_hex(buff, len);
free(buff);
return (buff1);
}
#endif
/* ------------------------------------------------------------------*
* GDB handlers *
* ------------------------------------------------------------------*/
/**
* @brief Handles the single-step command from
* GDB.
*/
static void handle_gdb_single_step(void)
{
/* For mock, just send the we're already halted =). */
#ifdef USE_MOCKS
send_gdb_halt_reason();
#else
/* Send to our serial-line that we want a single-step. */
send_serial_byte(SERIAL_STATE_SS);
have_x86_regs = 0;
#endif
}
/**
* @brief Sends the continue to the serial device.
*/
static void send_gdb_continue(void)
{
have_x86_regs = 0;
send_serial_byte(SERIAL_STATE_CONTINUE);
}
/**
* @brief Handles the 'continue' command from GDB.
*/
static void handle_gdb_continue(void) {
/* Send to our serial-line that we want to continue. */
send_gdb_continue();
}
/**
* @brief Handles the 'halt reason (?)' command from GDB.
*/
static void handle_gdb_halt_reason(void) {
send_gdb_halt_reason();
}
/**
* @brief Handle he 'read registers (g)' command from GDB.
*/
static void handle_gdb_read_registers(void) {
send_gdb_cmd(read_registers(), 128);
}
/**
* @brief Handles the 'read memory (m)' command from GDB.
*
* @param Message buffer to be parsed.
* @param Buffer length.
*
* @return Returns 0 if the request is valid, -1 otherwise.
*
* @note Please note that the actual memory read is
* done by the serial device. This routine only parses
* the command and forward the request to the serial
* device.
*/
static int handle_gdb_read_memory(const char *buff, size_t len)
{
const char *ptr;
uint32_t addr, amnt;
ptr = buff;
/* Skip first 'm'. */
expect_char('m', ptr, len);
addr = read_int(ptr, &len, &ptr, 16);
expect_char(',', ptr, len);
/* Get amount. */
amnt = simple_read_int(ptr, len, 16);
/*
* The addresses asked by GDB are already physical,
* since we convert them at handle_serial_single_step_stop()
*/
last_dump_phys_addr = addr;
last_dump_amnt = amnt;
#ifndef USE_MOCKS
/* Already prepare our buffer. */
dump_buffer = malloc(amnt);
if (!dump_buffer)
errx("Unable to alloc %d bytes!\n", amnt);
/*
* Asks the serial device to send its memory
*
* Our 'protocol', is as follows:
*
* 0xD8 <address-4-bytes-LE> <size-2-bytes-LE>
* ^--- read memory command, 1-byte
*/
send_serial_byte(SERIAL_STATE_READ_MEM_CMD);
send_serial_dword(addr);
send_serial_word(amnt);
/*
* For serial, we do not answer immediately, instead,
* we wait for the serial answer, and only then,
* reply with the memory.
*
* For mocks, we answer immediately.
*/
#endif
#ifdef USE_MOCKS
ptr = read_mock_memory(len);
send_gdb_cmd(ptr, len * 2);
#endif
return (0);
}
/**
* @brief Handles the 'write memory (M)' command from GDB.
*
* @param Message buffer to be parsed.
* @param Buffer length.
*
* @return Returns 0 if the request is valid, -1 otherwise.
*
* @note Please note that the actual memory write is
* done by the serial device. This routine only parses
* the command and forward the request to the serial
* device.
*/
static int handle_gdb_write_memory_hex(const char *buff, size_t len)
{
const char *ptr, *memory;
uint32_t addr, amnt;
ptr = buff;
/* Skip first 'M'. */
expect_char('M', ptr, len);
addr = read_int(ptr, &len, &ptr, 16);
expect_char(',', ptr, len);
/* Get amount. */
amnt = read_int(ptr, &len, &ptr, 16);
expect_char(':', ptr, len);
/* If 0, just send that we support X command and quit. */
if (!amnt)
{
send_gdb_ok();
return (0);
}
/* Decode hex buffer to binary. */
memory = decode_hex(ptr, amnt);
/* Send to our serial device. */
send_serial_byte(SERIAL_STATE_WRITE_MEM_CMD);
send_serial_dword(addr);
send_serial_word(amnt);
send_all(serial_fd, memory, amnt);
return (0);
}
/**
* @brief Handles the 'add breakpoint (Zn)' command from GDB.
*
* This routine handles all kinds of breakpoints that GDB
* might ask for, from Z0 to Z4. Please note that even
* SW breakpoints are handled as HW breakpoints, and this
* current implementation only supports 1 instruction
* breakpoint and 1 hw watchpoint (whether access or write).
*
* The only kind of breakpoint that is not supported is the
* 'Z3' or 'read watchpoint', because x86 does not supports
* them. However, GDB is smart enough to realize that and
* asks to use Z4 instead. When a Z4 breakpoints (emulating
* Z3) happens, all the writes are silently ignored and GDB
* continues to execute again.
*
* @param Message buffer to be parsed.
* @param Buffer length.
*
* @return Returns 0 if the request is valid, -1 otherwise.
*/
static int handle_gdb_add_breakpoint(const char *buff, size_t len)
{
const char *ptr = buff;
uint32_t addr;
/* Skip 'Z0'. */
expect_char('Z', ptr, len);
expect_char_range('0', '4', ptr, len);
expect_char(',', ptr, len);
/* Get breakpoint address (that is already physical). */
addr = read_int(ptr, &len, &ptr, 16);
expect_char(',', ptr, len);
/*
* Check which type of breakpoint we have and act
* accordingly.
*/
switch (buff[1]) {
/*
* Instruction break,
* Since we only support hw break anyway, 0 (sw) and
* 1 (hw) will have the same meaning...
*/
case '0':
case '1':
breakpoint_insn_addr = addr;
send_serial_byte(SERIAL_STATE_ADD_SW_BREAK);
send_serial_dword(breakpoint_insn_addr);
break;
/* Write watchpoint. */
case '2':
send_serial_byte(SERIAL_STATE_ADD_HW_WATCH);
send_serial_byte(HW_WATCH_WRITE);
send_serial_dword(addr);
break;
/* Read watchpoint. */
case '3':
send_gdb_unsupported_msg();
break;
/* Access (Read/Write) watchpoint. */
case '4':
send_serial_byte(SERIAL_STATE_ADD_HW_WATCH);
send_serial_byte(HW_WATCH_ACCESS);
send_serial_dword(addr);
break;
}
return (0);
}
/**
* @brief Handles the 'remove breakpoint (zn)' command from GDB.
*
* Since one breakpoint for instruction and one for data is
* supported, the address provided in the packet is ignored.
*
* @param Message buffer to be parsed.
* @param Buffer length.
*
* @return Returns 0 if the request is valid, -1 otherwise.
*/
static int handle_gdb_remove_breakpoint(const char *buff, size_t len)
{
const char *ptr = buff;
/* Skip 'z0'. */
expect_char('z', ptr, len);
expect_char_range('0', '4', ptr, len);
expect_char(',', ptr, len);
/*
* Check which type of breakpoint we have and act
* accordingly.
*
* Since we only support 1 instruction (ATM) and 1
* data breakpoint, there is no need to check
* address and etc...
*/
switch (buff[1]) {
/* Instruction break. */
case '0':
case '1':
breakpoint_insn_addr = 0;
send_serial_byte(SERIAL_STATE_REM_SW_BREAK);
break;
/* Remaining. */
case '2':
case '3':
case '4':
send_serial_byte(SERIAL_STATE_REM_HW_WATCH);
break;
}
return (0);
}
/**
* @brief Handles the 'write register (P)' GDB command;
*
* Please note the the segment registers and EIP,EFLAGS
* are 16-bit. An attempt to write a 32-bit value on them
* will emit an error.
*
* Also note that the mapping from what we receive from
* the serial device and the mapping expected by GDB
* differs, so there is a need to a conversion.
*
* @param buff Buffer to be parsed.
* @param len Buffer length.
*
* @return Returns 0 if the command is valid, -1 otherwise.
*/
static int handle_gdb_write_register(const char *buff, size_t len)
{
uint32_t reg_num_gdb, reg_num_rm;
const char *ptr, *dec;
union minibuf value;
static const int gdb_to_rm[] =
/* EAX. */ /* GS. */
{7,6,5,4,3,2,1,0,13,15,14,12,11,10,9,8};
ptr = buff;
expect_char('P', ptr, len);
reg_num_gdb = read_int(ptr, &len, &ptr, 16);
expect_char('=', ptr, len);
dec = decode_hex(ptr, 4);
memcpy(&value, dec, 4);
/* Validate register. */
if (reg_num_gdb >= 16)
{
send_gdb_error();
return (-1);
}
reg_num_rm = gdb_to_rm[reg_num_gdb];
/*
* Validate value: 16-bit registers should not
* receive values greater than 16-bit =)
*/
if (reg_num_rm >= 8 && value.b32 > ((1<<16)-1))
{
send_gdb_error();
return (-1);
}
/* Update our 'cache'. */
x86_regs.r32[reg_num_gdb] = value.b32;
/* Send to our serial device. */
send_serial_byte(SERIAL_STATE_REG_WRITE);
send_serial_byte(reg_num_rm);
send_serial_dword(value.b32);
return (0);
}
/*
* Keeps all the variables for the GDB state machine here
*/
struct gdb_handle
{
int state;
int csum;
int cmd_idx;
char buff[32];
char csum_read[3];
char cmd_buff[512];
} gdb_handle = {
.state = GDB_STATE_START
};
/**
* @brief Generic handler for all GDB commands/packets.
*
* This routine handles all messages and dispatches each
* of them for the appropriated handler, if any. If not
* supported, a not-supported packet is sent to GDB.
*
* @param gh GDB state machine data.
*
* @return Always 0.
*/
static int handle_gdb_cmd(struct gdb_handle *gh)
{
int csum_chk;
csum_chk = (int) simple_read_int(gh->csum_read, 2, 16);
if (csum_chk != gh->csum)
errw("Checksum for message: %s (%d) doesn't match: %d!\n",
gh->cmd_buff, csum_chk, gh->csum);
/* Ack received message. */
send_gdb_ack();
/*
* Handle single-char messages.
*
* From GDB docs:
* > At a minimum, a stub is required to support the ‘?’
* command to tell GDB the reason for halting, ‘g’ and
* ‘G’ commands for register access, and the ‘m’ and
* ‘M’ commands for memory access.
*
* > Stubs that only control single-threaded targets can
* implement run control with the ‘c’ (continue)
* command, and if the target architecture supports
* hardware- assisted single-stepping, the ‘s’ (step)
* command.
*
* > ... All other commands are optional.
*/
switch (gh->cmd_buff[0]) {
/* Read registers. */
case 'g':
handle_gdb_read_registers();
break;
/* Read memory. */
case 'm':
handle_gdb_read_memory(gh->cmd_buff,
sizeof gh->cmd_buff);
break;
/* Memory write hex. */
case 'M':
handle_gdb_write_memory_hex(gh->cmd_buff,
sizeof gh->cmd_buff);
break;
/* Halt reason. */
case '?':
handle_gdb_halt_reason();
break;
/* Single-step. */
case 's':
handle_gdb_single_step();
break;
/* Continue. */
case 'c':
handle_gdb_continue();
break;
/* Insert breakpoint. */
case 'Z':
handle_gdb_add_breakpoint(gh->cmd_buff,
sizeof gh->cmd_buff);
break;
/* Remove breakpoint. */
case 'z':
handle_gdb_remove_breakpoint(gh->cmd_buff,
sizeof gh->cmd_buff);
break;
/* Write register. */
case 'P':
handle_gdb_write_register(gh->cmd_buff,
sizeof gh->cmd_buff);
break;
/* Not-supported messages. */
default:
send_gdb_unsupported_msg();
break;
}
return (0);
}
/* ------------------------------------------------------------------*
* GDB handling state machine *
* ------------------------------------------------------------------*/
/**
* @brief Handle the start of state for a GDB command.
*
* If any non-valid start of command is received, the char
* is silently ignored.
*
* @param gh GDB state machine data.
* @param curr_byte Current byte read.
*/
static void handle_gdb_state_start(struct gdb_handle *gh,
uint8_t curr_byte)
{
/*
* If Ctrl+C.
*
* Ctrl+C/break is a special command that doesn't need
* to be ack'ed nor anything
*/
if (curr_byte == 3)
{
send_serial_ctrlc();
return;
}
/* Skip any char before a start of command. */
if (curr_byte != '$')
return;
gh->state = GDB_STATE_CMD;
memset(gh->cmd_buff, 0, sizeof gh->cmd_buff);
gh->csum = 0;
gh->cmd_idx = 0;
}
/**
* @brief Handle the receipt of the first checksum digit.
*
* @param gh GDB state machine data.
* @param curr_byte Current byte read.
*/
static inline void handle_gdb_state_csum_d1(struct gdb_handle *gh,
uint8_t curr_byte)
{
gh->csum_read[0] = curr_byte;
gh->state = GDB_STATE_CSUM_D2;
}
/**
* @brief Handle the receipt of the last checksum digit.
*
* This also marks the end of the command, so the command
* in this stage is completely received and ready to be
* parsed.
*
* @param gh GDB state machine data.
* @param curr_byte Current byte read.
*/
static inline void handle_gdb_state_csum_d2(struct gdb_handle *gh,
uint8_t curr_byte)
{
gh->csum_read[1] = curr_byte;
gh->state = GDB_STATE_START;
gh->csum &= 0xFF;
/* Handles the command. */
handle_gdb_cmd(gh);
LOG_CMD_REC("Command: (%s), csum: %x, csum_read: %s\n",
gh->cmd_buff, gh->csum, gh->csum_read);
}
/**
* @brief Handle the command data.
*
* While already received a command, this routine saves its
* content until the marker of end-of-command (#).
*
* @param gh GDB state machine data.
* @param curr_byte Current byte read.
*/
static inline void handle_gdb_state_cmd(struct gdb_handle *gh,
uint8_t curr_byte)
{
if (curr_byte == '#')
{
gh->state = GDB_STATE_CSUM_D1;
return;
}
gh->csum += curr_byte;
/* Emit a warning if command exceeds buffer size. */
if ((size_t)gh->cmd_idx > sizeof gh->cmd_buff - 2)
errx("Command exceeds buffer size (%zu): %s\n",
sizeof gh->cmd_buff, gh->cmd_buff);
gh->cmd_buff[gh->cmd_idx++] = curr_byte;
}
/**
* @brief For each byte received, calls the appropriate
* handler, accordingly with the byte and the current
* state.
*
* @param hfd Socket handler (not used).
*/
void handle_gdb_msg(struct handler_fd *hfd)
{
((void)hfd);
int i;
ssize_t ret;
uint8_t curr_byte;
ret = recv(gdb_fd, gdb_handle.buff, sizeof gdb_handle.buff, 0);
if (ret <= 0)
errx("GDB closed!\n");
for (i = 0; i < ret; i++)
{
curr_byte = gdb_handle.buff[i] & 0xFF;
switch (gdb_handle.state) {
/* Decide which state to go. */
case GDB_STATE_START:
handle_gdb_state_start(&gdb_handle, curr_byte);
break;
/* First digit checksum. */
case GDB_STATE_CSUM_D1:
handle_gdb_state_csum_d1(&gdb_handle, curr_byte);
break;
/* Second digit checsum. */
case GDB_STATE_CSUM_D2:
handle_gdb_state_csum_d2(&gdb_handle, curr_byte);
break;
/* Inside a command. */
case GDB_STATE_CMD:
handle_gdb_state_cmd(&gdb_handle, curr_byte);
break;
}
}
}
/* ------------------------------------------------------------------*
* Serial handlers *
* ------------------------------------------------------------------*/
/**
* @brief Handles a read memory.
*
* In this point the serial device had already read the
* memory and sent to the bridge. The memory is (almost)
* ready to be sent to GDB.
*
* *Almost because while in interrupt-based mode, the
* contents of the memory will change in order to keep
* the CPU cool (hlt+jmp hlt). Due to that, if the
* memory read overlaps the overwritten instructions,
* we need to patch with the original instructions.
*
* @return Always 0.
*/
static int handle_serial_receive_read_memory(void)
{
char *memory;
#ifndef UART_POLLING
int i, j, k, count;
uint32_t break_eip;
int sindex, eindex;
uint32_t start_addr, end_addr;
/* We need to check if the retrieved memory
* belongs do instruction or data.
*
* If instruction, given the base address
* check if there is a need to patch
* with our saved insns or not.
*
* If data, nothing need to be done.
*/
break_eip = (x86_regs.r.cs << 4) + x86_regs.r.eip;
start_addr = last_dump_phys_addr;
end_addr = start_addr + last_dump_amnt - 1;
/*