blob: 546fe3b09857b529ad593ecbf39ee72651cade0e [file] [log] [blame]
sewardj3b290482011-05-06 21:02:55 +00001/* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2006, 2011
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7 It has been modified to integrate it in valgrind
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
23
24#include "server.h"
25#include "regdef.h"
26#include "pub_core_options.h"
27#include "pub_core_translate.h"
28#include "pub_core_mallocfree.h"
29
30unsigned long cont_thread;
31unsigned long general_thread;
32unsigned long step_thread;
33unsigned long thread_from_wait;
34unsigned long old_thread_from_wait;
35
bart2dafc542011-05-18 16:08:28 +000036int pass_signals[TARGET_SIGNAL_LAST];
37
sewardj3b290482011-05-06 21:02:55 +000038/* for a gdbserver integrated in valgrind, resuming the process consists
39 in returning the control to valgrind.
40 Then at the next error or break or ..., valgrind calls gdbserver again.
41 A resume packet must then be built.
42 resume_packet_needed records the fact that the next call to gdbserver
43 must send a resume packet to gdb. */
44static Bool resume_packet_needed = False;
45
46VG_MINIMAL_JMP_BUF(toplevel);
47
48/* Decode a qXfer read request. Return 0 if everything looks OK,
49 or -1 otherwise. */
50
51static
52int decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
53{
54 /* Extract and NUL-terminate the annex. */
55 *annex = buf;
56 while (*buf && *buf != ':')
57 buf++;
58 if (*buf == '\0')
59 return -1;
60 *buf++ = 0;
61
62 /* After the read/write marker and annex, qXfer looks like a
63 traditional 'm' packet. */
64 decode_m_packet (buf, ofs, len);
65
66 return 0;
67}
68
69/* Write the response to a successful qXfer read. Returns the
70 length of the (binary) data stored in BUF, corresponding
71 to as much of DATA/LEN as we could fit. IS_MORE controls
72 the first character of the response. */
73static
74int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
75{
76 int out_len;
77
78 if (is_more)
79 buf[0] = 'm';
80 else
81 buf[0] = 'l';
82
83 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
84 PBUFSIZ - POVERHSIZ - 1) + 1;
85}
86
87static Bool initial_valgrind_sink_saved = False;
88/* True <=> valgrind log sink saved in initial_valgrind_sink */
89static OutputSink initial_valgrind_sink;
90
91static Bool command_output_to_log = False;
92/* True <=> command output goes to log instead of gdb */
93
94void reset_valgrind_sink(char *info)
95{
96 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd
97 && initial_valgrind_sink_saved) {
98 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
99 VG_(umsg) ("Reset valgrind output to log (%s)\n",
100 (info = NULL ? "" : info));
101 }
102}
103
104static
105void kill_request (char *msg)
106{
107 VG_(umsg) ("%s", msg);
108 remote_close();
109 VG_(exit) (0);
110}
111
112/* handle_gdb_valgrind_command handles the provided mon string command.
113 If command is recognised, return 1 else return 0.
114 Note that in case of ambiguous command, 1 is returned.
115
116 *sink_wanted_at_return is modified if one of the commands
117 'vg.set *_output' is handled.
118*/
119static
120int handle_gdb_valgrind_command (char* mon, OutputSink* sink_wanted_at_return)
121{
122 UWord ret = 0;
123 char s[strlen(mon)+1]; /* copy for strtok_r */
124 char* wcmd;
125 Char* ssaveptr;
126 char* endptr;
127 int kwdid;
128 int int_value;
129
130 vg_assert (initial_valgrind_sink_saved);
131
132 strcpy (s, mon);
133 wcmd = strtok_r (s, " ", &ssaveptr);
134 /* NB: if possible, avoid introducing a new command below which
135 starts with the same 4 first letters as an already existing
136 command. This ensures a shorter abbreviation for the user. */
137 switch (VG_(keyword_id) ("help vg.set vg.info vg.wait vg.kill vg.translate",
138 wcmd, kwd_report_duplicated_matches)) {
139 case -2:
140 ret = 1;
141 break;
142 case -1:
143 break;
144 case 0: /* help */
145 ret = 1;
146 wcmd = strtok_r (NULL, " ", &ssaveptr);
147 if (wcmd == NULL) {
148 int_value = 0;
149 } else {
150 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) {
151 case -2: int_value = 0; break;
152 case -1: int_value = 0; break;
153 case 0: int_value = 1; break;
154 default: tl_assert (0);
155 }
156 }
157
158 VG_(gdb_printf) (
159"general valgrind monitor commands:\n"
160" help [debug] : monitor command help. With debug: + debugging commands\n"
161" vg.wait [<ms>] : sleep <ms> (default 0) then continue\n"
162" vg.info all_errors : show all errors found so far\n"
163" vg.info last_error : show last error found\n"
164" vg.info n_errs_found : show the nr of errors found so far\n"
165" vg.kill : kill the Valgrind process\n"
166" vg.set gdb_output : set valgrind output to gdb\n"
167" vg.set log_output : set valgrind output to log\n"
168" vg.set mixed_output : set valgrind output to log, interactive output to gdb\n"
169" vg.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
170 if (int_value) { VG_(gdb_printf) (
171"debugging valgrind internals monitor commands:\n"
172" vg.info gdbserver_status : show gdbserver status\n"
173" vg.info memory : show valgrind heap memory stats\n"
174" vg.set debuglog <level> : set valgrind debug log level to <level>\n"
175" vg.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
176" (default traceflags 0b00100000 : show after instrumentation)\n"
177" An additional flag 0b100000000 allows to show gdbserver instrumentation\n");
178 }
179 break;
180 case 1: /* vg.set */
181 ret = 1;
182 wcmd = strtok_r (NULL, " ", &ssaveptr);
183 switch (kwdid = VG_(keyword_id)
184 ("vgdb-error debuglog gdb_output log_output mixed_output",
185 wcmd, kwd_report_all)) {
186 case -2:
187 case -1:
188 break;
189 case 0: /* vgdb-error */
190 case 1: /* debuglog */
191 wcmd = strtok_r (NULL, " ", &ssaveptr);
192 if (wcmd == NULL) {
193 int_value = 0;
194 endptr = "empty"; /* to report an error below */
195 } else {
196 int_value = strtol (wcmd, &endptr, 10);
197 }
198 if (*endptr != '\0') {
199 VG_(gdb_printf) ("missing or malformed integer value\n");
200 } else if (kwdid == 0) {
201 VG_(gdb_printf) ("vgdb-error value changed from %d to %d\n",
202 VG_(dyn_vgdb_error), int_value);
203 VG_(dyn_vgdb_error) = int_value;
204 } else if (kwdid == 1) {
205 VG_(gdb_printf) ("debuglog value changed from %d to %d\n",
206 VG_(debugLog_getLevel)(), int_value);
207 VG_(debugLog_startup) (int_value, "gdbsrv");
208 } else {
209 vg_assert (0);
210 }
211 break;
212 case 2: /* gdb_output */
213 (*sink_wanted_at_return).fd = -2;
214 command_output_to_log = False;
215 VG_(gdb_printf) ("valgrind output will go to gdb\n");
216 break;
217 case 3: /* log_output */
218 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
219 command_output_to_log = True;
220 VG_(gdb_printf) ("valgrind output will go to log\n");
221 break;
222 case 4: /* mixed output */
223 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
224 command_output_to_log = False;
225 VG_(gdb_printf)
226 ("valgrind output will go to log, interactive output will go to gdb\n");
227 break;
228 default:
229 vg_assert (0);
230 }
231 break;
232 case 2: /* vg.info */ {
233 ret = 1;
234 wcmd = strtok_r (NULL, " ", &ssaveptr);
235 switch (kwdid = VG_(keyword_id)
236 ("all_errors n_errs_found last_error gdbserver_status memory",
237 wcmd, kwd_report_all)) {
238 case -2:
239 case -1:
240 break;
241 case 0: // all_errors
242 // A verbosity of minimum 2 is needed to show the errors.
243 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False);
244 break;
245 case 1: // n_errs_found
246 VG_(gdb_printf) ("n_errs_found %d (vgdb-error %d)\n",
247 VG_(get_n_errs_found) (),
248 VG_(dyn_vgdb_error));
249 break;
250 case 2: // last_error
251 VG_(show_last_error)();
252 break;
253 case 3: // gdbserver_status
254 VG_(gdbserver_status_output)();
255 break;
256 case 4: /* memory */
257 VG_(print_all_arena_stats) ();
258 if (VG_(clo_profile_heap))
259 VG_(print_arena_cc_analysis) ();
260 ret = 1;
261 break;
262 default:
263 vg_assert(0);
264 }
265 break;
266 }
267 case 3: /* vg.wait */
268 wcmd = strtok_r (NULL, " ", &ssaveptr);
269 if (wcmd != NULL) {
270 int_value = strtol (wcmd, &endptr, 10);
271 VG_(gdb_printf) ("gdbserver: continuing in %d ms ...\n", int_value);
272 VG_(poll)(NULL, 0, int_value);
273 }
274 VG_(gdb_printf) ("gdbserver: continuing after wait ...\n");
275 ret = 1;
276 break;
277 case 4: /* vg.kill */
278 kill_request ("monitor command request to kill this process\n");
279 break;
280 case 5: { /* vg.translate */
281 Addr address;
282 SizeT verbosity = 0x20;
283
284 ret = 1;
285
286 VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr);
287 if (address != (Addr) 0 || verbosity != 0) {
288 /* we need to force the output to log for the translation trace,
289 as low level VEX tracing cannot be redirected to gdb. */
290 int saved_command_output_to_log = command_output_to_log;
291 int saved_fd = VG_(log_output_sink).fd;
292 Bool single_stepping_on_entry = valgrind_single_stepping();
293 int vex_verbosity = verbosity & 0xff;
294 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
295 if ((verbosity & 0x100) && !single_stepping_on_entry) {
296 valgrind_set_single_stepping(True);
297 // to force gdbserver instrumentation.
298 }
sewardj99d61342011-05-17 16:35:11 +0000299# if defined(VGA_arm)
300 // on arm, we need to (potentially) convert this address
301 // to the thumb form.
302 address = thumb_pc (address);
303# endif
304
sewardj3b290482011-05-06 21:02:55 +0000305 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
306 address,
307 /*debugging*/True,
308 (Int) vex_verbosity,
309 /*bbs_done*/0,
310 /*allow redir?*/True);
311 if ((verbosity & 0x100) && !single_stepping_on_entry) {
312 valgrind_set_single_stepping(False);
313 // reset single stepping.
314 }
315 command_output_to_log = saved_command_output_to_log;
316 VG_(log_output_sink).fd = saved_fd;
317 }
318 break;
319 }
320
321 default:
322 vg_assert (0);
323 }
324 return ret;
325}
326
327/* handle_gdb_monitor_command handles the provided mon string command,
328 which can be either a "standard" valgrind monitor command
329 or a tool specific monitor command.
330 If command recognised, return 1 else return 0.
331 Note that in case of ambiguous command, 1 is returned.
332*/
333static
334int handle_gdb_monitor_command (char* mon)
335{
336 UWord ret = 0;
337 UWord tool_ret = 0;
338 // initially, we assume that when returning, the desired sink is the
339 // one we have when entering. It can however be changed by the standard
340 // valgrind command handling.
341 OutputSink sink_wanted_at_return = VG_(log_output_sink);
342
343 if (!initial_valgrind_sink_saved) {
344 /* first time we enter here, we save the valgrind default log sink */
345 initial_valgrind_sink = sink_wanted_at_return;
346 initial_valgrind_sink_saved = True;
347 }
348
349 if (!command_output_to_log)
350 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */
351
352 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return);
353
354 /* Even if command was recognised by valgrind core, we call the
355 tool command handler : this is needed to handle help command
356 and/or to let the tool do some additional processing of a
357 valgrind standard command. Note however that if valgrind
358 recognised the command, we will always return success. */
359 if (VG_(needs).client_requests) {
360 /* If the tool reports an error when handling a monitor command,
361 we need to avoid calling gdbserver during this command
362 handling. So, we temporarily set VG_(dyn_vgdb_error) to
363 a huge value to ensure m_errormgr.c does not call gdbserver. */
364 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error);
365 UWord arg[2];
366 VG_(dyn_vgdb_error) = 999999999;
367 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND;
368 arg[1] = (UWord) mon;
369 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg,
370 &tool_ret);
371 VG_(dyn_vgdb_error) = save_dyn_vgdb_error;
372 }
373
374 /* restore or set the desired output */
375 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
376 if (ret | tool_ret)
377 return 1;
378 else
379 return 0;
380}
381
382
383/* Handle all of the extended 'Q' packets. */
384static
385void handle_set (char *arg_own_buf, int *new_packet_len_p)
386{
387 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) {
388 noack_mode = True;
389 write_ok (arg_own_buf);
390 return;
391 }
392
393 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
394 int i;
395 char *from, *to;
396 char *end = arg_own_buf + strlen(arg_own_buf);
397 CORE_ADDR sig;
398 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
399 pass_signals[i] = 0;
400
401 from = arg_own_buf + 13;
402 while (from < end) {
403 to = strchr(from, ';');
404 if (to == NULL) to = end;
405 decode_address (&sig, from, to - from);
406 pass_signals[(int)sig] = 1;
407 dlog(1, "pass_signal %d\n", (int)sig);
408 from = to;
409 if (*from == ';') from++;
410 }
411 write_ok (arg_own_buf);
412 return;
413 }
414 /* Otherwise we didn't know what packet it was. Say we didn't
415 understand it. */
416 arg_own_buf[0] = 0;
417}
418
419/* Handle all of the extended 'q' packets. */
420static
421void handle_query (char *arg_own_buf, int *new_packet_len_p)
422{
423 static struct inferior_list_entry *thread_ptr;
424
425 /* qRcmd, monitor command handling. */
426 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
427 char *p = arg_own_buf + 6;
428 int cmdlen = strlen(p)/2;
429 char cmd[cmdlen+1];
430
431 if (unhexify (cmd, p, cmdlen) != cmdlen) {
432 write_enn (arg_own_buf);
433 return;
434 }
435 cmd[cmdlen] = '\0';
436
437 if (handle_gdb_monitor_command (cmd)) {
438 /* In case the command is from a standalone vgdb,
439 connection will be closed soon => flush the output. */
440 VG_(message_flush) ();
441 write_ok (arg_own_buf);
442 return;
443 } else {
444 /* cmd not recognised */
445 VG_(gdb_printf)
446 ("command '%s' not recognised\n"
447 "In gdb, try 'monitor help'\n"
448 "In a shell, try 'vgdb help'\n",
449 cmd);
450 write_ok (arg_own_buf);
451 return;
452 }
453 }
454
455 /* provide some valgrind specific info in return to qThreadExtraInfo. */
456 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
457 unsigned long gdb_id;
458 struct thread_info *ti;
459 ThreadState *tst;
460 char status[100];
461
462 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
463 ti = gdb_id_to_thread (gdb_id);
464 if (ti != NULL) {
465 tst = (ThreadState *) inferior_target_data (ti);
466 /* Additional info is the tid and the thread status. */
467 VG_(snprintf) (status, sizeof(status), "tid %d %s",
468 tst->tid,
469 VG_(name_of_ThreadStatus)(tst->status));
470 hexify (arg_own_buf, status, strlen(status));
471 return;
472 } else {
473 write_enn (arg_own_buf);
474 return;
475 }
476 }
477
478 if (strcmp ("qAttached", arg_own_buf) == 0) {
479 /* tell gdb to always detach, never kill the process */
480 arg_own_buf[0] = '1';
481 arg_own_buf[1] = 0;
482 return;
483 }
484
485 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
486 /* We have no symbol to read. */
487 write_ok (arg_own_buf);
488 return;
489 }
490
491 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
492 thread_ptr = all_threads.head;
493 VG_(sprintf) (arg_own_buf, "m%x",
494 thread_to_gdb_id ((struct thread_info *)thread_ptr));
495 thread_ptr = thread_ptr->next;
496 return;
497 }
498
499 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
500 if (thread_ptr != NULL) {
501 VG_(sprintf) (arg_own_buf, "m%x",
502 thread_to_gdb_id ((struct thread_info *)thread_ptr));
503 thread_ptr = thread_ptr->next;
504 return;
505 } else {
506 VG_(sprintf) (arg_own_buf, "l");
507 return;
508 }
509 }
510
511 if ( ((*the_target->target_xml)() != NULL
512 || (*the_target->shadow_target_xml)() != NULL)
513 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
514 CORE_ADDR ofs;
515 unsigned int len, doc_len;
516 char *annex = NULL;
517 // First, the annex is extracted from the packet received.
518 // Then, it is replaced by the corresponding file name.
519 int fd;
520
521 /* Grab the annex, offset, and length. */
522 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
523 strcpy (arg_own_buf, "E00");
524 return;
525 }
526
527 if (strcmp (annex, "target.xml") == 0) {
528 annex = NULL; // to replace it by the corresponding filename.
529
530 /* If VG_(clo_vgdb_shadow_registers), try to use
531 shadow_target_xml. Fallback to target_xml
532 if not defined. */
533 if (VG_(clo_vgdb_shadow_registers)) {
534 annex = (*the_target->shadow_target_xml)();
535 if (annex != NULL)
536 /* Ensure the shadow registers are initialized. */
537 initialize_shadow_low(True);
538 }
539 if (annex == NULL)
540 annex = (*the_target->target_xml)();
541 if (annex == NULL) {
542 strcpy (arg_own_buf, "E00");
543 return;
544 }
545 }
546
547 {
548 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex)];
549 struct vg_stat stat_doc;
550 char toread[len];
551 int len_read;
552
553 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
554 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
555 if (fd == -1) {
556 strcpy (arg_own_buf, "E00");
557 return;
558 }
559 if (VG_(fstat) (fd, &stat_doc) != 0) {
560 VG_(close) (fd);
561 strcpy (arg_own_buf, "E00");
562 return;
563 }
564 doc_len = stat_doc.size;
565
566 if (len > PBUFSIZ - POVERHSIZ)
567 len = PBUFSIZ - POVERHSIZ;
568
569 if (ofs > doc_len) {
570 write_enn (arg_own_buf);
571 VG_(close) (fd);
572 return;
573 }
574 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
575 len_read = VG_(read) (fd, toread, len);
576 *new_packet_len_p = write_qxfer_response (arg_own_buf, toread,
577 len_read, ofs + len_read < doc_len);
578 VG_(close) (fd);
579 return;
580 }
581 }
582
583 /* Protocol features query. */
584 if (strncmp ("qSupported", arg_own_buf, 10) == 0
585 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
586 VG_(sprintf) (arg_own_buf, "PacketSize=%x", PBUFSIZ - 1);
587 /* Note: max packet size including frame and checksum, but without
588 trailing null byte, which is not sent/received. */
589
590 strcat (arg_own_buf, ";QStartNoAckMode+");
591 strcat (arg_own_buf, ";QPassSignals+");
592
593 if ((*the_target->target_xml)() != NULL
594 || (*the_target->shadow_target_xml)() != NULL) {
595 strcat (arg_own_buf, ";qXfer:features:read+");
596 /* if a new gdb connects to us, we have to reset the register
597 set to the normal register sets to allow this new gdb to
598 decide to use or not the shadow registers.
599
600 Note that the reset is only done for gdb that are sending
601 qSupported packets. If a user first connected with a recent
602 gdb using shadow registers and then with a very old gdb
603 that does not use qSupported packet, then the old gdb will
604 not properly connect. */
605 initialize_shadow_low(False);
606 }
607 return;
608 }
609
610 /* Otherwise we didn't know what packet it was. Say we didn't
611 understand it. */
612 arg_own_buf[0] = 0;
613}
614
615/* Handle all of the extended 'v' packets. */
616static
617void handle_v_requests (char *arg_own_buf, char *status, int *signal)
618{
619 /* vcont packet code from gdb 6.6 removed */
620
621 /* Otherwise we didn't know what packet it was. Say we didn't
622 understand it. */
623 arg_own_buf[0] = 0;
624 return;
625}
626
627static
628void myresume (int step, int sig)
629{
630 struct thread_resume resume_info[2];
631 int n = 0;
632
633 if (step || sig || (cont_thread != 0 && cont_thread != -1)) {
634 resume_info[0].thread
635 = ((struct inferior_list_entry *) current_inferior)->id;
636 resume_info[0].step = step;
637 resume_info[0].sig = sig;
638 resume_info[0].leave_stopped = 0;
639 n++;
640 }
641 resume_info[n].thread = -1;
642 resume_info[n].step = 0;
643 resume_info[n].sig = 0;
644 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
645
646 resume_packet_needed = True;
647 (*the_target->resume) (resume_info);
648}
649
650/* server_main global variables */
651static char *own_buf;
652static unsigned char *mem_buf;
653
654void gdbserver_init (void)
655{
656 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
657 noack_mode = False;
658 initialize_low ();
659 own_buf = malloc (PBUFSIZ);
660 mem_buf = malloc (PBUFSIZ);
661}
662
663void gdbserver_terminate (void)
664{
665 /* last call to gdbserver is cleanup call */
666 if (VG_MINIMAL_SETJMP(toplevel)) {
667 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
668 return;
669 }
670 remote_close();
671}
672
673void server_main (void)
674{
675 static char status;
676 static int signal;
677
678 char ch;
679 int i = 0;
680 unsigned int len;
681 CORE_ADDR mem_addr;
682
683 signal = mywait (&status);
684 if (VG_MINIMAL_SETJMP(toplevel)) {
685 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
686 }
687 while (1) {
688 unsigned char sig;
689 int packet_len;
690 int new_packet_len = -1;
691
692 if (resume_packet_needed) {
693 resume_packet_needed = False;
694 prepare_resume_reply (own_buf, status, signal);
695 putpkt (own_buf);
696 }
697
698 packet_len = getpkt (own_buf);
699 if (packet_len <= 0)
700 break;
701
702 i = 0;
703 ch = own_buf[i++];
704 switch (ch) {
705 case 'Q':
706 handle_set (own_buf, &new_packet_len);
707 break;
708 case 'q':
709 handle_query (own_buf, &new_packet_len);
710 break;
711 case 'd':
712 /* set/unset debugging is done through valgrind debug level. */
713 own_buf[0] = '\0';
714 break;
715 case 'D':
716 reset_valgrind_sink("gdb detaching from process");
717
718 /* When detaching or kill the process, gdb expects to get
719 an packet OK back. Any other output will make gdb
720 believes detach did not work. */
721 write_ok (own_buf);
722 putpkt (own_buf);
723 remote_finish (reset_after_error);
724 remote_open (VG_(clo_vgdb_prefix));
725 myresume (0, 0);
726 resume_packet_needed = False;
727 return;
728 case '!':
729 /* We can not use the extended protocol with valgrind,
730 because we can not restart the running
731 program. So return unrecognized. */
732 own_buf[0] = '\0';
733 break;
734 case '?':
735 prepare_resume_reply (own_buf, status, signal);
736 break;
737 case 'H':
738 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
739 unsigned long gdb_id, thread_id;
740
741 gdb_id = strtoul (&own_buf[2], NULL, 16);
742 thread_id = gdb_id_to_thread_id (gdb_id);
743 if (thread_id == 0) {
744 write_enn (own_buf);
745 break;
746 }
747
748 if (own_buf[1] == 'g') {
749 general_thread = thread_id;
750 set_desired_inferior (1);
751 } else if (own_buf[1] == 'c') {
752 cont_thread = thread_id;
753 } else if (own_buf[1] == 's') {
754 step_thread = thread_id;
755 }
756
757 write_ok (own_buf);
758 } else {
759 /* Silently ignore it so that gdb can extend the protocol
760 without compatibility headaches. */
761 own_buf[0] = '\0';
762 }
763 break;
764 case 'g':
765 set_desired_inferior (1);
766 registers_to_string (own_buf);
767 break;
768 case 'G':
769 set_desired_inferior (1);
770 registers_from_string (&own_buf[1]);
771 write_ok (own_buf);
772 break;
773 case 'P': {
774 int regno;
775 char *regbytes;
776 Bool mod;
777 ThreadState *tst;
778 regno = strtol(&own_buf[1], NULL, 16);
779 regbytes = strchr(&own_buf[0], '=') + 1;
780 set_desired_inferior (1);
781 tst = (ThreadState *) inferior_target_data (current_inferior);
782 /* Only accept changing registers in "runnable state3.
783 In fact, it would be ok to change most of the registers
784 except a few "sensitive" registers such as the PC, SP, BP.
785 We assume we do not need to very specific here, and that we
786 can just refuse all of these. */
787 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
788 supply_register_from_string (regno, regbytes, &mod);
789 write_ok (own_buf);
790 } else {
791 /* at least from gdb 6.6 onwards, an E. error
792 reply is shown to the user. So, we do an error
793 msg which both is accepted by gdb as an error msg
794 and is readable by the user. */
795 VG_(sprintf)
796 (own_buf,
797"E.\n"
798"ERROR changing register %s regno %d\n"
799"gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
800"set pc, calling from gdb a function in the debugged process, ...)\n"
801"can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
802"Thread status is %s\n",
803 find_register_by_number (regno)->name, regno,
804 VG_(name_of_ThreadStatus)(tst->status));
805 if (VG_(clo_verbosity) > 1)
806 VG_(umsg) ("%s\n", own_buf);
807 }
808 break;
809 }
810 case 'm':
811 decode_m_packet (&own_buf[1], &mem_addr, &len);
812 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
813 convert_int_to_ascii (mem_buf, own_buf, len);
814 else
815 write_enn (own_buf);
816 break;
817 case 'M':
818 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
819 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
820 write_ok (own_buf);
821 else
822 write_enn (own_buf);
823 break;
824 case 'X':
825 if (decode_X_packet (&own_buf[1], packet_len - 1,
826 &mem_addr, &len, mem_buf) < 0
827 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
828 write_enn (own_buf);
829 else
830 write_ok (own_buf);
831 break;
832 case 'C':
833 convert_ascii_to_int (own_buf + 1, &sig, 1);
834 if (target_signal_to_host_p (sig))
835 signal = target_signal_to_host (sig);
836 else
837 signal = 0;
838 set_desired_inferior (0);
839 myresume (0, signal);
840 return; // return control to valgrind
841 case 'S':
842 convert_ascii_to_int (own_buf + 1, &sig, 1);
843 if (target_signal_to_host_p (sig))
844 signal = target_signal_to_host (sig);
845 else
846 signal = 0;
847 set_desired_inferior (0);
848 myresume (1, signal);
849 return; // return control to valgrind
850 case 'c':
851 set_desired_inferior (0);
852 myresume (0, 0);
853 return; // return control to valgrind
854 case 's':
855 set_desired_inferior (0);
856 myresume (1, 0);
857 return; // return control to valgrind
858 case 'Z': {
859 char *lenptr;
860 char *dataptr;
861 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
862 int zlen = strtol (lenptr + 1, &dataptr, 16);
863 char type = own_buf[1];
864
865 if (the_target->insert_watchpoint == NULL
866 || (type < '0' || type > '4')) {
867 /* No watchpoint support or not a watchpoint command;
868 unrecognized either way. */
869 own_buf[0] = '\0';
870 } else {
871 int res;
872
873 res = (*the_target->insert_watchpoint) (type, addr, zlen);
874 if (res == 0)
875 write_ok (own_buf);
876 else if (res == 1)
877 /* Unsupported. */
878 own_buf[0] = '\0';
879 else
880 write_enn (own_buf);
881 }
882 break;
883 }
884 case 'z': {
885 char *lenptr;
886 char *dataptr;
887 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
888 int zlen = strtol (lenptr + 1, &dataptr, 16);
889 char type = own_buf[1];
890
891 if (the_target->remove_watchpoint == NULL
892 || (type < '0' || type > '4')) {
893 /* No watchpoint support or not a watchpoint command;
894 unrecognized either way. */
895 own_buf[0] = '\0';
896 } else {
897 int res;
898
899 res = (*the_target->remove_watchpoint) (type, addr, zlen);
900 if (res == 0)
901 write_ok (own_buf);
902 else if (res == 1)
903 /* Unsupported. */
904 own_buf[0] = '\0';
905 else
906 write_enn (own_buf);
907 }
908 break;
909 }
910 case 'k':
911 kill_request("Gdb request to kill this process\n");
912 break;
913 case 'T': {
914 unsigned long gdb_id, thread_id;
915
916 gdb_id = strtoul (&own_buf[1], NULL, 16);
917 thread_id = gdb_id_to_thread_id (gdb_id);
918 if (thread_id == 0) {
919 write_enn (own_buf);
920 break;
921 }
922
923 if (mythread_alive (thread_id))
924 write_ok (own_buf);
925 else
926 write_enn (own_buf);
927 break;
928 }
929 case 'R':
930 /* Restarting the inferior is only supported in the
931 extended protocol.
932 => It is a request we don't understand. Respond with an
933 empty packet so that gdb knows that we don't support this
934 request. */
935 own_buf[0] = '\0';
936 break;
937 case 'v':
938 /* Extended (long) request. */
939 handle_v_requests (own_buf, &status, &signal);
940 break;
941 default:
942 /* It is a request we don't understand. Respond with an
943 empty packet so that gdb knows that we don't support this
944 request. */
945 own_buf[0] = '\0';
946 break;
947 }
948
949 if (new_packet_len != -1)
950 putpkt_binary (own_buf, new_packet_len);
951 else
952 putpkt (own_buf);
953
954 if (status == 'W')
955 VG_(umsg) ("\nChild exited with status %d\n", signal);
956 if (status == 'X')
957 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
958 target_signal_to_host (signal),
959 target_signal_to_name (signal));
960 if (status == 'W' || status == 'X') {
961 VG_(umsg) ("Process exiting\n");
962 VG_(exit) (0);
963 }
964 }
965
966 /* We come here when getpkt fails => close the connection,
967 and re-open. Then return control to valgrind.
968 We return the control to valgrind as we assume that
969 the connection was closed due to vgdb having finished
970 to execute a command. */
971 if (VG_(clo_verbosity) > 1)
972 VG_(umsg) ("Remote side has terminated connection. "
973 "GDBserver will reopen the connection.\n");
974 remote_finish (reset_after_error);
975 remote_open (VG_(clo_vgdb_prefix));
976 myresume (0, 0);
977 resume_packet_needed = False;
978 return;
979}