11#include <kernel.h>
22
33static symbol_t * symbol_table = NULL ;
4- uint32_t trace_thread_id = 0 ;
54static bool debug_signal = false;
65
76#define BT_IRQ_MARKER ((void*)0xFFFFffffFFFFff01ull)
@@ -18,201 +17,6 @@ void gdb_decode(char* out, const uint8_t* in, size_t len) {
1817 out [len ] = '\0' ;
1918}
2019
21-
22- void gdb_send_ack (uint32_t src_ip , uint16_t src_port )
23- {
24- src_ip = htonl (src_ip );
25- udp_send_packet ((uint8_t * )& src_ip , DEBUG_DST_PORT , src_port , "+" , 1 );
26- }
27-
28- void gdb_send_packet (uint32_t src_ip , uint16_t src_port , const char * packet )
29- {
30- src_ip = htonl (src_ip );
31- uint64_t checksum = 0 ;
32- const uint8_t * ptr = (const uint8_t * )packet + 1 ;
33- while (* ptr ) {
34- checksum += * ptr ++ ;
35- }
36- char p [strlen (packet ) + 5 ];
37- snprintf (p , strlen (packet ) + 5 , "%s#%02x" , packet , (uint8_t )checksum % 256 );
38- udp_send_packet ((uint8_t * )& src_ip , DEBUG_DST_PORT , src_port , p , strlen (p ));
39- }
40-
41- void gdb_send_nack (uint32_t src_ip , uint16_t src_port )
42- {
43- src_ip = htonl (src_ip );
44- udp_send_packet ((uint8_t * )& src_ip , DEBUG_DST_PORT , src_port , "-" , 1 );
45- }
46-
47- void gdb_ack (uint32_t src_ip , uint16_t src_port )
48- {
49- }
50-
51- void gdb_retransmit (uint32_t src_ip , uint16_t src_port )
52- {
53- }
54-
55- void gdb_query (uint32_t src_ip , uint16_t src_port , const char * command )
56- {
57- char cmd_first [strlen (command )];
58- int x = 0 ;
59- const char * p = command ;
60- const char * rest = NULL ;
61- while (* p && * p != ';' && * p != ':' && * p != '?' ) {
62- cmd_first [x ++ ] = * p ++ ;
63- }
64- cmd_first [x ] = 0 ;
65- rest = p ;
66- dprintf ("GDB qcmd: '%s' '%s'\n" , cmd_first , rest );
67- if (strcmp (cmd_first , "qSupported" ) == 0 ) {
68- //gdb_send_packet(src_ip, src_port, "$QSupported:multiprocess+;qXfer:exec-file:read;swbreak+;hwbreak+;fork-events+;exec-events+;vContSupported+;no-resumed+;memory-tagging+;xmlRegisters=i386");
69- gdb_send_packet (src_ip , src_port , "$PacketSize=1000;QNonStop-;QDisableRandomization+;qXfer:threads:read+;qXfer:features:read-;qXfer:exec-file:read+;vContSupported+;multiprocess+" );
70- } else if (strcmp (cmd_first , "qTStatus" ) == 0 ) {
71- // Thread status
72- gdb_send_packet (src_ip , src_port , "$T0;tnotrun:0;tframes:0;tcreated:0;tfree:50*!;tsize:50*!;circular:0;disconn:0;starttime:0;stoptime:0;username:;notes::" );
73- } else if (strcmp (cmd_first , "qTfV" ) == 0 || strcmp (cmd_first , "qTsV" ) == 0 ) {
74- // get list of trace vars
75- gdb_send_packet (src_ip , src_port , "$l" );
76- } else if (strcmp (cmd_first , "qTfP" ) == 0 || strcmp (cmd_first , "qTsP" ) == 0 ) {
77- // get list of tracepoints
78- gdb_send_packet (src_ip , src_port , "$l" );
79- } else if (strcmp (cmd_first , "qfThreadInfo" ) == 0 ) {
80- /* Thread ID list */
81- gdb_send_packet (src_ip , src_port , "$m1,2,3,4,5" );
82- } else if (strcmp (cmd_first , "qsThreadInfo" ) == 0 ) {
83- gdb_send_packet (src_ip , src_port , "$l" );
84- } else if (strcmp (cmd_first , "qAttached" ) == 0 ) {
85- // 1 if attached successfully to process, 0 if started a new process, Exx on error
86- gdb_send_packet (src_ip , src_port , "$1" );
87- } else if (strcmp (cmd_first , "qC" ) == 0 ) {
88- // Return current thread id
89- gdb_send_packet (src_ip , src_port , "$QCp1.1" );
90- } else if (strcmp (cmd_first , "qXfer" ) == 0 ) {
91- if (!strncmp (rest , ":exec-file:read:" , 16 )) {
92- gdb_send_packet (src_ip , src_port , "$l/programs/init" );
93- } else if (!strncmp (rest , ":threads:read:" , 14 )) {
94- gdb_send_packet (src_ip , src_port , "$l<threads><thread id=\"p1.1\" core=\"0\" name=\"/programs/init\"/></threads>" );
95- }
96- }
97- }
98-
99- void gdb_variable_command (uint32_t src_ip , uint16_t src_port , const char * command )
100- {
101- char cmd_first [strlen (command )];
102- int x = 0 ;
103- const char * p = command ;
104- const char * rest = NULL ;
105- while (* p && * p != ';' && * p != '?' && * p != ':' ) {
106- cmd_first [x ++ ] = * p ++ ;
107- }
108- cmd_first [x ] = 0 ;
109- rest = p ;
110- dprintf ("GDB vcmd: '%s' '%s'\n" , cmd_first , rest );
111- if (strcmp (cmd_first , "vMustReplyEmpty" ) == 0 ) {
112- gdb_send_packet (src_ip , src_port , "$" );
113- } else if (strcmp (cmd_first , "vFile" ) == 0 ) {
114- // Host I/O: Not currently supported as it tries to download current executable!
115- gdb_send_packet (src_ip , src_port , "$" );
116- }
117- }
118-
119- void gdb_set_thread (uint32_t src_ip , uint16_t src_port , const char * command )
120- {
121- const char op = command [1 ];
122- const char * thread_id = command + 2 ;
123- trace_thread_id = atoi (thread_id );
124- dprintf ("GDB: set thread: op=%c thread_id=%s\n" , op , thread_id );
125- gdb_send_packet (src_ip , src_port , "$OK" );
126- }
127-
128- void gdb_status_query (uint32_t src_ip , uint16_t src_port , const char * command )
129- {
130- gdb_send_packet (src_ip , src_port , "$S01thread:p1.1" );
131- }
132-
133- void gdb_regs (uint32_t src_ip , uint16_t src_port , const char * command )
134- {
135- gdb_send_packet (src_ip , src_port , "$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" );
136- }
137-
138- void gdb_reg (uint32_t src_ip , uint16_t src_port , const char * command )
139- {
140- if (* (command + 1 ) == '8' ) {
141- /* Program counter */
142- gdb_send_packet (src_ip , src_port , "$00000010" );
143- return ;
144- }
145- gdb_send_packet (src_ip , src_port , "$xxxxxxxx" );
146- }
147-
148- void gdb_mem (uint32_t src_ip , uint16_t src_port , const char * command )
149- {
150- uint64_t count = hextoint (strchr (command + 1 , ',' ) + 1 );
151- char out [count * 2 + 2 ];
152- memset (out , '0' , count * 2 + 2 );
153- out [count * 2 + 1 ] = 0 ;
154- out [0 ] = '$' ;
155- dprintf ("MEM (%d): %s\n" , strlen (out ), out );
156- gdb_send_packet (src_ip , src_port , out );
157- }
158-
159- void gdb_command (uint32_t src_ip , uint16_t src_port , const char * command )
160- {
161- dprintf ("GDB: from '%04x:%04x' '%s'\n" , src_ip , src_port , command );
162- gdb_send_ack (src_ip , src_port );
163- switch (* command ) {
164- case 'q' :
165- return gdb_query (src_ip , src_port , command );
166- case 'v' :
167- return gdb_variable_command (src_ip , src_port , command );
168- case 'H' :
169- return gdb_set_thread (src_ip , src_port , command );
170- case '?' :
171- return gdb_status_query (src_ip , src_port , command );
172- case 'g' :
173- return gdb_regs (src_ip , src_port , command );
174- case 'p' :
175- return gdb_reg (src_ip , src_port , command );
176- case 'm' :
177- return gdb_mem (src_ip , src_port , command );
178- }
179- }
180-
181- void gdb_data (uint32_t src_ip , uint16_t src_port , uint8_t * data , uint32_t length )
182- {
183- uint64_t checksum = 0 , n = 0 , their_sum = 0 ;
184- uint8_t * ptr = data ;
185- char xsum [4 ] = { 0 };
186- char commands [length ];
187- while (n < length && * ptr != '#' ) {
188- checksum += * ptr ++ ;
189- ++ n ;
190- }
191- ++ ptr ;
192- strlcpy (xsum , (const char * )ptr , 3 );
193- their_sum = hextoint (xsum );
194- checksum %= 256 ;
195- if (checksum == their_sum ) {
196- strlcpy (commands , (const char * )data , n + 1 );
197- gdb_command (src_ip , src_port , commands );
198- } else {
199- dprintf ("GDB packet with invalid checksum: %lx vs %lx" , their_sum , checksum );
200- }
201- }
202-
203- void debug_handle_packet (uint32_t src_ip , uint16_t src_port , uint16_t dst_port , void * data , uint32_t length , void * opaque )
204- {
205- uint8_t identifier = * (uint8_t * )data ;
206- switch (identifier ) {
207- case '+' :
208- return gdb_ack (src_ip , src_port );
209- case '-' :
210- return gdb_retransmit (src_ip , src_port );
211- case '$' :
212- return gdb_data (src_ip , src_port , data + 1 , length - 1 );
213- }
214- }
215-
21620bool set_debug_signal (bool status )
21721{
21822 bool old = debug_signal ;
@@ -366,8 +170,6 @@ void init_debug()
366170 kprintf ("/kernel.sym " );
367171 setforeground (COLOUR_WHITE );
368172 kprintf ("(%ld bytes)\n" , filesize );
369-
370- udp_register_daemon (DEBUG_DST_PORT , & debug_handle_packet , NULL );
371173}
372174
373175const char * findsymbol (uint64_t address , uint64_t * offset ) {
0 commit comments