]>
Commit | Line | Data |
---|---|---|
53197fc4 JW |
1 | /* |
2 | * Kernel Debug Core | |
3 | * | |
4 | * Maintainer: Jason Wessel <jason.wessel@windriver.com> | |
5 | * | |
6 | * Copyright (C) 2000-2001 VERITAS Software Corporation. | |
7 | * Copyright (C) 2002-2004 Timesys Corporation | |
8 | * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com> | |
9 | * Copyright (C) 2004 Pavel Machek <pavel@suse.cz> | |
10 | * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org> | |
11 | * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd. | |
12 | * Copyright (C) 2005-2009 Wind River Systems, Inc. | |
13 | * Copyright (C) 2007 MontaVista Software, Inc. | |
14 | * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> | |
15 | * | |
16 | * Contributors at various stages not listed above: | |
17 | * Jason Wessel ( jason.wessel@windriver.com ) | |
18 | * George Anzinger <george@mvista.com> | |
19 | * Anurekh Saxena (anurekh.saxena@timesys.com) | |
20 | * Lake Stevens Instrument Division (Glenn Engel) | |
21 | * Jim Kingdon, Cygnus Support. | |
22 | * | |
23 | * Original KGDB stub: David Grothe <dave@gcom.com>, | |
24 | * Tigran Aivazian <tigran@sco.com> | |
25 | * | |
26 | * This file is licensed under the terms of the GNU General Public License | |
27 | * version 2. This program is licensed "as is" without any warranty of any | |
28 | * kind, whether express or implied. | |
29 | */ | |
30 | ||
31 | #include <linux/kernel.h> | |
32 | #include <linux/kgdb.h> | |
f5316b4a | 33 | #include <linux/kdb.h> |
53197fc4 JW |
34 | #include <linux/reboot.h> |
35 | #include <linux/uaccess.h> | |
36 | #include <asm/cacheflush.h> | |
37 | #include <asm/unaligned.h> | |
38 | #include "debug_core.h" | |
39 | ||
40 | #define KGDB_MAX_THREAD_QUERY 17 | |
41 | ||
42 | /* Our I/O buffers. */ | |
43 | static char remcom_in_buffer[BUFMAX]; | |
44 | static char remcom_out_buffer[BUFMAX]; | |
45 | ||
46 | /* Storage for the registers, in GDB format. */ | |
47 | static unsigned long gdb_regs[(NUMREGBYTES + | |
48 | sizeof(unsigned long) - 1) / | |
49 | sizeof(unsigned long)]; | |
50 | ||
51 | /* | |
52 | * GDB remote protocol parser: | |
53 | */ | |
54 | ||
f5316b4a JW |
55 | #ifdef CONFIG_KGDB_KDB |
56 | static int gdbstub_read_wait(void) | |
57 | { | |
58 | int ret = -1; | |
59 | int i; | |
60 | ||
61 | /* poll any additional I/O interfaces that are defined */ | |
62 | while (ret < 0) | |
63 | for (i = 0; kdb_poll_funcs[i] != NULL; i++) { | |
64 | ret = kdb_poll_funcs[i](); | |
65 | if (ret > 0) | |
66 | break; | |
67 | } | |
68 | return ret; | |
69 | } | |
70 | #else | |
71 | static int gdbstub_read_wait(void) | |
72 | { | |
73 | int ret = dbg_io_ops->read_char(); | |
74 | while (ret == NO_POLL_CHAR) | |
75 | ret = dbg_io_ops->read_char(); | |
76 | return ret; | |
77 | } | |
78 | #endif | |
53197fc4 JW |
79 | /* scan for the sequence $<data>#<checksum> */ |
80 | static void get_packet(char *buffer) | |
81 | { | |
82 | unsigned char checksum; | |
83 | unsigned char xmitcsum; | |
84 | int count; | |
85 | char ch; | |
86 | ||
87 | do { | |
88 | /* | |
89 | * Spin and wait around for the start character, ignore all | |
90 | * other characters: | |
91 | */ | |
f5316b4a | 92 | while ((ch = (gdbstub_read_wait())) != '$') |
53197fc4 JW |
93 | /* nothing */; |
94 | ||
95 | kgdb_connected = 1; | |
96 | checksum = 0; | |
97 | xmitcsum = -1; | |
98 | ||
99 | count = 0; | |
100 | ||
101 | /* | |
102 | * now, read until a # or end of buffer is found: | |
103 | */ | |
104 | while (count < (BUFMAX - 1)) { | |
f5316b4a | 105 | ch = gdbstub_read_wait(); |
53197fc4 JW |
106 | if (ch == '#') |
107 | break; | |
108 | checksum = checksum + ch; | |
109 | buffer[count] = ch; | |
110 | count = count + 1; | |
111 | } | |
112 | buffer[count] = 0; | |
113 | ||
114 | if (ch == '#') { | |
a9fa20a7 AS |
115 | xmitcsum = hex_to_bin(gdbstub_read_wait()) << 4; |
116 | xmitcsum += hex_to_bin(gdbstub_read_wait()); | |
53197fc4 JW |
117 | |
118 | if (checksum != xmitcsum) | |
119 | /* failed checksum */ | |
120 | dbg_io_ops->write_char('-'); | |
121 | else | |
122 | /* successful transfer */ | |
123 | dbg_io_ops->write_char('+'); | |
124 | if (dbg_io_ops->flush) | |
125 | dbg_io_ops->flush(); | |
126 | } | |
127 | } while (checksum != xmitcsum); | |
128 | } | |
129 | ||
130 | /* | |
131 | * Send the packet in buffer. | |
132 | * Check for gdb connection if asked for. | |
133 | */ | |
134 | static void put_packet(char *buffer) | |
135 | { | |
136 | unsigned char checksum; | |
137 | int count; | |
138 | char ch; | |
139 | ||
140 | /* | |
141 | * $<packet info>#<checksum>. | |
142 | */ | |
143 | while (1) { | |
144 | dbg_io_ops->write_char('$'); | |
145 | checksum = 0; | |
146 | count = 0; | |
147 | ||
148 | while ((ch = buffer[count])) { | |
149 | dbg_io_ops->write_char(ch); | |
150 | checksum += ch; | |
151 | count++; | |
152 | } | |
153 | ||
154 | dbg_io_ops->write_char('#'); | |
155 | dbg_io_ops->write_char(hex_asc_hi(checksum)); | |
156 | dbg_io_ops->write_char(hex_asc_lo(checksum)); | |
157 | if (dbg_io_ops->flush) | |
158 | dbg_io_ops->flush(); | |
159 | ||
160 | /* Now see what we get in reply. */ | |
f5316b4a | 161 | ch = gdbstub_read_wait(); |
53197fc4 JW |
162 | |
163 | if (ch == 3) | |
f5316b4a | 164 | ch = gdbstub_read_wait(); |
53197fc4 JW |
165 | |
166 | /* If we get an ACK, we are done. */ | |
167 | if (ch == '+') | |
168 | return; | |
169 | ||
170 | /* | |
171 | * If we get the start of another packet, this means | |
172 | * that GDB is attempting to reconnect. We will NAK | |
173 | * the packet being sent, and stop trying to send this | |
174 | * packet. | |
175 | */ | |
176 | if (ch == '$') { | |
177 | dbg_io_ops->write_char('-'); | |
178 | if (dbg_io_ops->flush) | |
179 | dbg_io_ops->flush(); | |
180 | return; | |
181 | } | |
182 | } | |
183 | } | |
184 | ||
185 | static char gdbmsgbuf[BUFMAX + 1]; | |
186 | ||
187 | void gdbstub_msg_write(const char *s, int len) | |
188 | { | |
189 | char *bufptr; | |
190 | int wcount; | |
191 | int i; | |
192 | ||
a0de055c JW |
193 | if (len == 0) |
194 | len = strlen(s); | |
195 | ||
53197fc4 JW |
196 | /* 'O'utput */ |
197 | gdbmsgbuf[0] = 'O'; | |
198 | ||
199 | /* Fill and send buffers... */ | |
200 | while (len > 0) { | |
201 | bufptr = gdbmsgbuf + 1; | |
202 | ||
203 | /* Calculate how many this time */ | |
204 | if ((len << 1) > (BUFMAX - 2)) | |
205 | wcount = (BUFMAX - 2) >> 1; | |
206 | else | |
207 | wcount = len; | |
208 | ||
209 | /* Pack in hex chars */ | |
210 | for (i = 0; i < wcount; i++) | |
211 | bufptr = pack_hex_byte(bufptr, s[i]); | |
212 | *bufptr = '\0'; | |
213 | ||
214 | /* Move up */ | |
215 | s += wcount; | |
216 | len -= wcount; | |
217 | ||
218 | /* Write packet */ | |
219 | put_packet(gdbmsgbuf); | |
220 | } | |
221 | } | |
222 | ||
223 | /* | |
224 | * Convert the memory pointed to by mem into hex, placing result in | |
225 | * buf. Return a pointer to the last char put in buf (null). May | |
226 | * return an error. | |
227 | */ | |
228 | int kgdb_mem2hex(char *mem, char *buf, int count) | |
229 | { | |
230 | char *tmp; | |
231 | int err; | |
232 | ||
233 | /* | |
234 | * We use the upper half of buf as an intermediate buffer for the | |
235 | * raw memory copy. Hex conversion will work against this one. | |
236 | */ | |
237 | tmp = buf + count; | |
238 | ||
239 | err = probe_kernel_read(tmp, mem, count); | |
240 | if (!err) { | |
241 | while (count > 0) { | |
242 | buf = pack_hex_byte(buf, *tmp); | |
243 | tmp++; | |
244 | count--; | |
245 | } | |
246 | ||
247 | *buf = 0; | |
248 | } | |
249 | ||
250 | return err; | |
251 | } | |
252 | ||
253 | /* | |
254 | * Convert the hex array pointed to by buf into binary to be placed in | |
255 | * mem. Return a pointer to the character AFTER the last byte | |
256 | * written. May return an error. | |
257 | */ | |
258 | int kgdb_hex2mem(char *buf, char *mem, int count) | |
259 | { | |
260 | char *tmp_raw; | |
261 | char *tmp_hex; | |
262 | ||
263 | /* | |
264 | * We use the upper half of buf as an intermediate buffer for the | |
265 | * raw memory that is converted from hex. | |
266 | */ | |
267 | tmp_raw = buf + count * 2; | |
268 | ||
269 | tmp_hex = tmp_raw - 1; | |
270 | while (tmp_hex >= buf) { | |
271 | tmp_raw--; | |
a9fa20a7 AS |
272 | *tmp_raw = hex_to_bin(*tmp_hex--); |
273 | *tmp_raw |= hex_to_bin(*tmp_hex--) << 4; | |
53197fc4 JW |
274 | } |
275 | ||
276 | return probe_kernel_write(mem, tmp_raw, count); | |
277 | } | |
278 | ||
279 | /* | |
280 | * While we find nice hex chars, build a long_val. | |
281 | * Return number of chars processed. | |
282 | */ | |
283 | int kgdb_hex2long(char **ptr, unsigned long *long_val) | |
284 | { | |
285 | int hex_val; | |
286 | int num = 0; | |
287 | int negate = 0; | |
288 | ||
289 | *long_val = 0; | |
290 | ||
291 | if (**ptr == '-') { | |
292 | negate = 1; | |
293 | (*ptr)++; | |
294 | } | |
295 | while (**ptr) { | |
a9fa20a7 | 296 | hex_val = hex_to_bin(**ptr); |
53197fc4 JW |
297 | if (hex_val < 0) |
298 | break; | |
299 | ||
300 | *long_val = (*long_val << 4) | hex_val; | |
301 | num++; | |
302 | (*ptr)++; | |
303 | } | |
304 | ||
305 | if (negate) | |
306 | *long_val = -*long_val; | |
307 | ||
308 | return num; | |
309 | } | |
310 | ||
311 | /* | |
312 | * Copy the binary array pointed to by buf into mem. Fix $, #, and | |
313 | * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success. | |
314 | * The input buf is overwitten with the result to write to mem. | |
315 | */ | |
316 | static int kgdb_ebin2mem(char *buf, char *mem, int count) | |
317 | { | |
318 | int size = 0; | |
319 | char *c = buf; | |
320 | ||
321 | while (count-- > 0) { | |
322 | c[size] = *buf++; | |
323 | if (c[size] == 0x7d) | |
324 | c[size] = *buf++ ^ 0x20; | |
325 | size++; | |
326 | } | |
327 | ||
328 | return probe_kernel_write(mem, c, size); | |
329 | } | |
330 | ||
331 | /* Write memory due to an 'M' or 'X' packet. */ | |
332 | static int write_mem_msg(int binary) | |
333 | { | |
334 | char *ptr = &remcom_in_buffer[1]; | |
335 | unsigned long addr; | |
336 | unsigned long length; | |
337 | int err; | |
338 | ||
339 | if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' && | |
340 | kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') { | |
341 | if (binary) | |
342 | err = kgdb_ebin2mem(ptr, (char *)addr, length); | |
343 | else | |
344 | err = kgdb_hex2mem(ptr, (char *)addr, length); | |
345 | if (err) | |
346 | return err; | |
347 | if (CACHE_FLUSH_IS_SAFE) | |
348 | flush_icache_range(addr, addr + length); | |
349 | return 0; | |
350 | } | |
351 | ||
352 | return -EINVAL; | |
353 | } | |
354 | ||
355 | static void error_packet(char *pkt, int error) | |
356 | { | |
357 | error = -error; | |
358 | pkt[0] = 'E'; | |
359 | pkt[1] = hex_asc[(error / 10)]; | |
360 | pkt[2] = hex_asc[(error % 10)]; | |
361 | pkt[3] = '\0'; | |
362 | } | |
363 | ||
364 | /* | |
365 | * Thread ID accessors. We represent a flat TID space to GDB, where | |
366 | * the per CPU idle threads (which under Linux all have PID 0) are | |
367 | * remapped to negative TIDs. | |
368 | */ | |
369 | ||
370 | #define BUF_THREAD_ID_SIZE 16 | |
371 | ||
372 | static char *pack_threadid(char *pkt, unsigned char *id) | |
373 | { | |
374 | char *limit; | |
375 | ||
376 | limit = pkt + BUF_THREAD_ID_SIZE; | |
377 | while (pkt < limit) | |
378 | pkt = pack_hex_byte(pkt, *id++); | |
379 | ||
380 | return pkt; | |
381 | } | |
382 | ||
383 | static void int_to_threadref(unsigned char *id, int value) | |
384 | { | |
385 | unsigned char *scan; | |
386 | int i = 4; | |
387 | ||
388 | scan = (unsigned char *)id; | |
389 | while (i--) | |
390 | *scan++ = 0; | |
391 | put_unaligned_be32(value, scan); | |
392 | } | |
393 | ||
394 | static struct task_struct *getthread(struct pt_regs *regs, int tid) | |
395 | { | |
396 | /* | |
397 | * Non-positive TIDs are remapped to the cpu shadow information | |
398 | */ | |
399 | if (tid == 0 || tid == -1) | |
400 | tid = -atomic_read(&kgdb_active) - 2; | |
401 | if (tid < -1 && tid > -NR_CPUS - 2) { | |
402 | if (kgdb_info[-tid - 2].task) | |
403 | return kgdb_info[-tid - 2].task; | |
404 | else | |
405 | return idle_task(-tid - 2); | |
406 | } | |
407 | if (tid <= 0) { | |
408 | printk(KERN_ERR "KGDB: Internal thread select error\n"); | |
409 | dump_stack(); | |
410 | return NULL; | |
411 | } | |
412 | ||
413 | /* | |
414 | * find_task_by_pid_ns() does not take the tasklist lock anymore | |
415 | * but is nicely RCU locked - hence is a pretty resilient | |
416 | * thing to use: | |
417 | */ | |
418 | return find_task_by_pid_ns(tid, &init_pid_ns); | |
419 | } | |
420 | ||
421 | ||
422 | /* | |
423 | * Remap normal tasks to their real PID, | |
424 | * CPU shadow threads are mapped to -CPU - 2 | |
425 | */ | |
426 | static inline int shadow_pid(int realpid) | |
427 | { | |
428 | if (realpid) | |
429 | return realpid; | |
430 | ||
431 | return -raw_smp_processor_id() - 2; | |
432 | } | |
433 | ||
434 | /* | |
435 | * All the functions that start with gdb_cmd are the various | |
436 | * operations to implement the handlers for the gdbserial protocol | |
437 | * where KGDB is communicating with an external debugger | |
438 | */ | |
439 | ||
440 | /* Handle the '?' status packets */ | |
441 | static void gdb_cmd_status(struct kgdb_state *ks) | |
442 | { | |
443 | /* | |
444 | * We know that this packet is only sent | |
445 | * during initial connect. So to be safe, | |
446 | * we clear out our breakpoints now in case | |
447 | * GDB is reconnecting. | |
448 | */ | |
449 | dbg_remove_all_break(); | |
450 | ||
451 | remcom_out_buffer[0] = 'S'; | |
452 | pack_hex_byte(&remcom_out_buffer[1], ks->signo); | |
453 | } | |
454 | ||
455 | /* Handle the 'g' get registers request */ | |
456 | static void gdb_cmd_getregs(struct kgdb_state *ks) | |
457 | { | |
458 | struct task_struct *thread; | |
459 | void *local_debuggerinfo; | |
460 | int i; | |
461 | ||
462 | thread = kgdb_usethread; | |
463 | if (!thread) { | |
464 | thread = kgdb_info[ks->cpu].task; | |
465 | local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo; | |
466 | } else { | |
467 | local_debuggerinfo = NULL; | |
468 | for_each_online_cpu(i) { | |
469 | /* | |
470 | * Try to find the task on some other | |
471 | * or possibly this node if we do not | |
472 | * find the matching task then we try | |
473 | * to approximate the results. | |
474 | */ | |
475 | if (thread == kgdb_info[i].task) | |
476 | local_debuggerinfo = kgdb_info[i].debuggerinfo; | |
477 | } | |
478 | } | |
479 | ||
480 | /* | |
481 | * All threads that don't have debuggerinfo should be | |
482 | * in schedule() sleeping, since all other CPUs | |
483 | * are in kgdb_wait, and thus have debuggerinfo. | |
484 | */ | |
485 | if (local_debuggerinfo) { | |
486 | pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo); | |
487 | } else { | |
488 | /* | |
489 | * Pull stuff saved during switch_to; nothing | |
490 | * else is accessible (or even particularly | |
491 | * relevant). | |
492 | * | |
493 | * This should be enough for a stack trace. | |
494 | */ | |
495 | sleeping_thread_to_gdb_regs(gdb_regs, thread); | |
496 | } | |
497 | kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES); | |
498 | } | |
499 | ||
500 | /* Handle the 'G' set registers request */ | |
501 | static void gdb_cmd_setregs(struct kgdb_state *ks) | |
502 | { | |
503 | kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES); | |
504 | ||
505 | if (kgdb_usethread && kgdb_usethread != current) { | |
506 | error_packet(remcom_out_buffer, -EINVAL); | |
507 | } else { | |
508 | gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs); | |
509 | strcpy(remcom_out_buffer, "OK"); | |
510 | } | |
511 | } | |
512 | ||
513 | /* Handle the 'm' memory read bytes */ | |
514 | static void gdb_cmd_memread(struct kgdb_state *ks) | |
515 | { | |
516 | char *ptr = &remcom_in_buffer[1]; | |
517 | unsigned long length; | |
518 | unsigned long addr; | |
519 | int err; | |
520 | ||
521 | if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' && | |
522 | kgdb_hex2long(&ptr, &length) > 0) { | |
523 | err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length); | |
524 | if (err) | |
525 | error_packet(remcom_out_buffer, err); | |
526 | } else { | |
527 | error_packet(remcom_out_buffer, -EINVAL); | |
528 | } | |
529 | } | |
530 | ||
531 | /* Handle the 'M' memory write bytes */ | |
532 | static void gdb_cmd_memwrite(struct kgdb_state *ks) | |
533 | { | |
534 | int err = write_mem_msg(0); | |
535 | ||
536 | if (err) | |
537 | error_packet(remcom_out_buffer, err); | |
538 | else | |
539 | strcpy(remcom_out_buffer, "OK"); | |
540 | } | |
541 | ||
542 | /* Handle the 'X' memory binary write bytes */ | |
543 | static void gdb_cmd_binwrite(struct kgdb_state *ks) | |
544 | { | |
545 | int err = write_mem_msg(1); | |
546 | ||
547 | if (err) | |
548 | error_packet(remcom_out_buffer, err); | |
549 | else | |
550 | strcpy(remcom_out_buffer, "OK"); | |
551 | } | |
552 | ||
553 | /* Handle the 'D' or 'k', detach or kill packets */ | |
554 | static void gdb_cmd_detachkill(struct kgdb_state *ks) | |
555 | { | |
556 | int error; | |
557 | ||
558 | /* The detach case */ | |
559 | if (remcom_in_buffer[0] == 'D') { | |
560 | error = dbg_remove_all_break(); | |
561 | if (error < 0) { | |
562 | error_packet(remcom_out_buffer, error); | |
563 | } else { | |
564 | strcpy(remcom_out_buffer, "OK"); | |
565 | kgdb_connected = 0; | |
566 | } | |
567 | put_packet(remcom_out_buffer); | |
568 | } else { | |
569 | /* | |
570 | * Assume the kill case, with no exit code checking, | |
571 | * trying to force detach the debugger: | |
572 | */ | |
573 | dbg_remove_all_break(); | |
574 | kgdb_connected = 0; | |
575 | } | |
576 | } | |
577 | ||
578 | /* Handle the 'R' reboot packets */ | |
579 | static int gdb_cmd_reboot(struct kgdb_state *ks) | |
580 | { | |
581 | /* For now, only honor R0 */ | |
582 | if (strcmp(remcom_in_buffer, "R0") == 0) { | |
583 | printk(KERN_CRIT "Executing emergency reboot\n"); | |
584 | strcpy(remcom_out_buffer, "OK"); | |
585 | put_packet(remcom_out_buffer); | |
586 | ||
587 | /* | |
588 | * Execution should not return from | |
589 | * machine_emergency_restart() | |
590 | */ | |
591 | machine_emergency_restart(); | |
592 | kgdb_connected = 0; | |
593 | ||
594 | return 1; | |
595 | } | |
596 | return 0; | |
597 | } | |
598 | ||
599 | /* Handle the 'q' query packets */ | |
600 | static void gdb_cmd_query(struct kgdb_state *ks) | |
601 | { | |
602 | struct task_struct *g; | |
603 | struct task_struct *p; | |
604 | unsigned char thref[8]; | |
605 | char *ptr; | |
606 | int i; | |
607 | int cpu; | |
608 | int finished = 0; | |
609 | ||
610 | switch (remcom_in_buffer[1]) { | |
611 | case 's': | |
612 | case 'f': | |
fb82c0ff | 613 | if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) |
53197fc4 | 614 | break; |
53197fc4 JW |
615 | |
616 | i = 0; | |
617 | remcom_out_buffer[0] = 'm'; | |
618 | ptr = remcom_out_buffer + 1; | |
619 | if (remcom_in_buffer[1] == 'f') { | |
620 | /* Each cpu is a shadow thread */ | |
621 | for_each_online_cpu(cpu) { | |
622 | ks->thr_query = 0; | |
623 | int_to_threadref(thref, -cpu - 2); | |
624 | pack_threadid(ptr, thref); | |
625 | ptr += BUF_THREAD_ID_SIZE; | |
626 | *(ptr++) = ','; | |
627 | i++; | |
628 | } | |
629 | } | |
630 | ||
631 | do_each_thread(g, p) { | |
632 | if (i >= ks->thr_query && !finished) { | |
633 | int_to_threadref(thref, p->pid); | |
634 | pack_threadid(ptr, thref); | |
635 | ptr += BUF_THREAD_ID_SIZE; | |
636 | *(ptr++) = ','; | |
637 | ks->thr_query++; | |
638 | if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0) | |
639 | finished = 1; | |
640 | } | |
641 | i++; | |
642 | } while_each_thread(g, p); | |
643 | ||
644 | *(--ptr) = '\0'; | |
645 | break; | |
646 | ||
647 | case 'C': | |
648 | /* Current thread id */ | |
649 | strcpy(remcom_out_buffer, "QC"); | |
650 | ks->threadid = shadow_pid(current->pid); | |
651 | int_to_threadref(thref, ks->threadid); | |
652 | pack_threadid(remcom_out_buffer + 2, thref); | |
653 | break; | |
654 | case 'T': | |
fb82c0ff | 655 | if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) |
53197fc4 | 656 | break; |
fb82c0ff | 657 | |
53197fc4 JW |
658 | ks->threadid = 0; |
659 | ptr = remcom_in_buffer + 17; | |
660 | kgdb_hex2long(&ptr, &ks->threadid); | |
661 | if (!getthread(ks->linux_regs, ks->threadid)) { | |
662 | error_packet(remcom_out_buffer, -EINVAL); | |
663 | break; | |
664 | } | |
665 | if ((int)ks->threadid > 0) { | |
666 | kgdb_mem2hex(getthread(ks->linux_regs, | |
667 | ks->threadid)->comm, | |
668 | remcom_out_buffer, 16); | |
669 | } else { | |
670 | static char tmpstr[23 + BUF_THREAD_ID_SIZE]; | |
671 | ||
672 | sprintf(tmpstr, "shadowCPU%d", | |
673 | (int)(-ks->threadid - 2)); | |
674 | kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr)); | |
675 | } | |
676 | break; | |
a0de055c JW |
677 | #ifdef CONFIG_KGDB_KDB |
678 | case 'R': | |
679 | if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) { | |
680 | int len = strlen(remcom_in_buffer + 6); | |
681 | ||
682 | if ((len % 2) != 0) { | |
683 | strcpy(remcom_out_buffer, "E01"); | |
684 | break; | |
685 | } | |
686 | kgdb_hex2mem(remcom_in_buffer + 6, | |
687 | remcom_out_buffer, len); | |
688 | len = len / 2; | |
689 | remcom_out_buffer[len++] = 0; | |
690 | ||
691 | kdb_parse(remcom_out_buffer); | |
692 | strcpy(remcom_out_buffer, "OK"); | |
693 | } | |
694 | break; | |
695 | #endif | |
53197fc4 JW |
696 | } |
697 | } | |
698 | ||
699 | /* Handle the 'H' task query packets */ | |
700 | static void gdb_cmd_task(struct kgdb_state *ks) | |
701 | { | |
702 | struct task_struct *thread; | |
703 | char *ptr; | |
704 | ||
705 | switch (remcom_in_buffer[1]) { | |
706 | case 'g': | |
707 | ptr = &remcom_in_buffer[2]; | |
708 | kgdb_hex2long(&ptr, &ks->threadid); | |
709 | thread = getthread(ks->linux_regs, ks->threadid); | |
710 | if (!thread && ks->threadid > 0) { | |
711 | error_packet(remcom_out_buffer, -EINVAL); | |
712 | break; | |
713 | } | |
714 | kgdb_usethread = thread; | |
715 | ks->kgdb_usethreadid = ks->threadid; | |
716 | strcpy(remcom_out_buffer, "OK"); | |
717 | break; | |
718 | case 'c': | |
719 | ptr = &remcom_in_buffer[2]; | |
720 | kgdb_hex2long(&ptr, &ks->threadid); | |
721 | if (!ks->threadid) { | |
722 | kgdb_contthread = NULL; | |
723 | } else { | |
724 | thread = getthread(ks->linux_regs, ks->threadid); | |
725 | if (!thread && ks->threadid > 0) { | |
726 | error_packet(remcom_out_buffer, -EINVAL); | |
727 | break; | |
728 | } | |
729 | kgdb_contthread = thread; | |
730 | } | |
731 | strcpy(remcom_out_buffer, "OK"); | |
732 | break; | |
733 | } | |
734 | } | |
735 | ||
736 | /* Handle the 'T' thread query packets */ | |
737 | static void gdb_cmd_thread(struct kgdb_state *ks) | |
738 | { | |
739 | char *ptr = &remcom_in_buffer[1]; | |
740 | struct task_struct *thread; | |
741 | ||
742 | kgdb_hex2long(&ptr, &ks->threadid); | |
743 | thread = getthread(ks->linux_regs, ks->threadid); | |
744 | if (thread) | |
745 | strcpy(remcom_out_buffer, "OK"); | |
746 | else | |
747 | error_packet(remcom_out_buffer, -EINVAL); | |
748 | } | |
749 | ||
750 | /* Handle the 'z' or 'Z' breakpoint remove or set packets */ | |
751 | static void gdb_cmd_break(struct kgdb_state *ks) | |
752 | { | |
753 | /* | |
754 | * Since GDB-5.3, it's been drafted that '0' is a software | |
755 | * breakpoint, '1' is a hardware breakpoint, so let's do that. | |
756 | */ | |
757 | char *bpt_type = &remcom_in_buffer[1]; | |
758 | char *ptr = &remcom_in_buffer[2]; | |
759 | unsigned long addr; | |
760 | unsigned long length; | |
761 | int error = 0; | |
762 | ||
763 | if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') { | |
764 | /* Unsupported */ | |
765 | if (*bpt_type > '4') | |
766 | return; | |
767 | } else { | |
768 | if (*bpt_type != '0' && *bpt_type != '1') | |
769 | /* Unsupported. */ | |
770 | return; | |
771 | } | |
772 | ||
773 | /* | |
774 | * Test if this is a hardware breakpoint, and | |
775 | * if we support it: | |
776 | */ | |
777 | if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)) | |
778 | /* Unsupported. */ | |
779 | return; | |
780 | ||
781 | if (*(ptr++) != ',') { | |
782 | error_packet(remcom_out_buffer, -EINVAL); | |
783 | return; | |
784 | } | |
785 | if (!kgdb_hex2long(&ptr, &addr)) { | |
786 | error_packet(remcom_out_buffer, -EINVAL); | |
787 | return; | |
788 | } | |
789 | if (*(ptr++) != ',' || | |
790 | !kgdb_hex2long(&ptr, &length)) { | |
791 | error_packet(remcom_out_buffer, -EINVAL); | |
792 | return; | |
793 | } | |
794 | ||
795 | if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0') | |
796 | error = dbg_set_sw_break(addr); | |
797 | else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0') | |
798 | error = dbg_remove_sw_break(addr); | |
799 | else if (remcom_in_buffer[0] == 'Z') | |
800 | error = arch_kgdb_ops.set_hw_breakpoint(addr, | |
801 | (int)length, *bpt_type - '0'); | |
802 | else if (remcom_in_buffer[0] == 'z') | |
803 | error = arch_kgdb_ops.remove_hw_breakpoint(addr, | |
804 | (int) length, *bpt_type - '0'); | |
805 | ||
806 | if (error == 0) | |
807 | strcpy(remcom_out_buffer, "OK"); | |
808 | else | |
809 | error_packet(remcom_out_buffer, error); | |
810 | } | |
811 | ||
812 | /* Handle the 'C' signal / exception passing packets */ | |
813 | static int gdb_cmd_exception_pass(struct kgdb_state *ks) | |
814 | { | |
815 | /* C09 == pass exception | |
816 | * C15 == detach kgdb, pass exception | |
817 | */ | |
818 | if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') { | |
819 | ||
820 | ks->pass_exception = 1; | |
821 | remcom_in_buffer[0] = 'c'; | |
822 | ||
823 | } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') { | |
824 | ||
825 | ks->pass_exception = 1; | |
826 | remcom_in_buffer[0] = 'D'; | |
827 | dbg_remove_all_break(); | |
828 | kgdb_connected = 0; | |
829 | return 1; | |
830 | ||
831 | } else { | |
832 | gdbstub_msg_write("KGDB only knows signal 9 (pass)" | |
833 | " and 15 (pass and disconnect)\n" | |
834 | "Executing a continue without signal passing\n", 0); | |
835 | remcom_in_buffer[0] = 'c'; | |
836 | } | |
837 | ||
838 | /* Indicate fall through */ | |
839 | return -1; | |
840 | } | |
841 | ||
842 | /* | |
843 | * This function performs all gdbserial command procesing | |
844 | */ | |
845 | int gdb_serial_stub(struct kgdb_state *ks) | |
846 | { | |
847 | int error = 0; | |
848 | int tmp; | |
849 | ||
850 | /* Clear the out buffer. */ | |
851 | memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); | |
852 | ||
853 | if (kgdb_connected) { | |
854 | unsigned char thref[8]; | |
855 | char *ptr; | |
856 | ||
857 | /* Reply to host that an exception has occurred */ | |
858 | ptr = remcom_out_buffer; | |
859 | *ptr++ = 'T'; | |
860 | ptr = pack_hex_byte(ptr, ks->signo); | |
861 | ptr += strlen(strcpy(ptr, "thread:")); | |
862 | int_to_threadref(thref, shadow_pid(current->pid)); | |
863 | ptr = pack_threadid(ptr, thref); | |
864 | *ptr++ = ';'; | |
865 | put_packet(remcom_out_buffer); | |
866 | } | |
867 | ||
868 | kgdb_usethread = kgdb_info[ks->cpu].task; | |
869 | ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid); | |
870 | ks->pass_exception = 0; | |
871 | ||
872 | while (1) { | |
873 | error = 0; | |
874 | ||
875 | /* Clear the out buffer. */ | |
876 | memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); | |
877 | ||
878 | get_packet(remcom_in_buffer); | |
879 | ||
880 | switch (remcom_in_buffer[0]) { | |
881 | case '?': /* gdbserial status */ | |
882 | gdb_cmd_status(ks); | |
883 | break; | |
884 | case 'g': /* return the value of the CPU registers */ | |
885 | gdb_cmd_getregs(ks); | |
886 | break; | |
887 | case 'G': /* set the value of the CPU registers - return OK */ | |
888 | gdb_cmd_setregs(ks); | |
889 | break; | |
890 | case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */ | |
891 | gdb_cmd_memread(ks); | |
892 | break; | |
893 | case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */ | |
894 | gdb_cmd_memwrite(ks); | |
895 | break; | |
896 | case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */ | |
897 | gdb_cmd_binwrite(ks); | |
898 | break; | |
899 | /* kill or detach. KGDB should treat this like a | |
900 | * continue. | |
901 | */ | |
902 | case 'D': /* Debugger detach */ | |
903 | case 'k': /* Debugger detach via kill */ | |
904 | gdb_cmd_detachkill(ks); | |
905 | goto default_handle; | |
906 | case 'R': /* Reboot */ | |
907 | if (gdb_cmd_reboot(ks)) | |
908 | goto default_handle; | |
909 | break; | |
910 | case 'q': /* query command */ | |
911 | gdb_cmd_query(ks); | |
912 | break; | |
913 | case 'H': /* task related */ | |
914 | gdb_cmd_task(ks); | |
915 | break; | |
916 | case 'T': /* Query thread status */ | |
917 | gdb_cmd_thread(ks); | |
918 | break; | |
919 | case 'z': /* Break point remove */ | |
920 | case 'Z': /* Break point set */ | |
921 | gdb_cmd_break(ks); | |
922 | break; | |
dcc78711 JW |
923 | #ifdef CONFIG_KGDB_KDB |
924 | case '3': /* Escape into back into kdb */ | |
925 | if (remcom_in_buffer[1] == '\0') { | |
926 | gdb_cmd_detachkill(ks); | |
927 | return DBG_PASS_EVENT; | |
928 | } | |
929 | #endif | |
53197fc4 JW |
930 | case 'C': /* Exception passing */ |
931 | tmp = gdb_cmd_exception_pass(ks); | |
932 | if (tmp > 0) | |
933 | goto default_handle; | |
934 | if (tmp == 0) | |
935 | break; | |
936 | /* Fall through on tmp < 0 */ | |
937 | case 'c': /* Continue packet */ | |
938 | case 's': /* Single step packet */ | |
939 | if (kgdb_contthread && kgdb_contthread != current) { | |
940 | /* Can't switch threads in kgdb */ | |
941 | error_packet(remcom_out_buffer, -EINVAL); | |
942 | break; | |
943 | } | |
944 | dbg_activate_sw_breakpoints(); | |
945 | /* Fall through to default processing */ | |
946 | default: | |
947 | default_handle: | |
948 | error = kgdb_arch_handle_exception(ks->ex_vector, | |
949 | ks->signo, | |
950 | ks->err_code, | |
951 | remcom_in_buffer, | |
952 | remcom_out_buffer, | |
953 | ks->linux_regs); | |
954 | /* | |
955 | * Leave cmd processing on error, detach, | |
956 | * kill, continue, or single step. | |
957 | */ | |
958 | if (error >= 0 || remcom_in_buffer[0] == 'D' || | |
959 | remcom_in_buffer[0] == 'k') { | |
960 | error = 0; | |
961 | goto kgdb_exit; | |
962 | } | |
963 | ||
964 | } | |
965 | ||
966 | /* reply to the request */ | |
967 | put_packet(remcom_out_buffer); | |
968 | } | |
969 | ||
970 | kgdb_exit: | |
971 | if (ks->pass_exception) | |
972 | error = 1; | |
973 | return error; | |
974 | } | |
dcc78711 JW |
975 | |
976 | int gdbstub_state(struct kgdb_state *ks, char *cmd) | |
977 | { | |
978 | int error; | |
979 | ||
980 | switch (cmd[0]) { | |
981 | case 'e': | |
982 | error = kgdb_arch_handle_exception(ks->ex_vector, | |
983 | ks->signo, | |
984 | ks->err_code, | |
985 | remcom_in_buffer, | |
986 | remcom_out_buffer, | |
987 | ks->linux_regs); | |
988 | return error; | |
989 | case 's': | |
990 | case 'c': | |
991 | strcpy(remcom_in_buffer, cmd); | |
992 | return 0; | |
993 | case '?': | |
994 | gdb_cmd_status(ks); | |
995 | break; | |
996 | case '\0': | |
997 | strcpy(remcom_out_buffer, ""); | |
998 | break; | |
999 | } | |
1000 | dbg_io_ops->write_char('+'); | |
1001 | put_packet(remcom_out_buffer); | |
1002 | return 0; | |
1003 | } |