Add DRD suppression patterns for races triggered by std::ostream
[valgrind.git] / coregrind / m_gdbserver / server.c
blobd11584c1b56c3b6f8d7d0a6a0bd66e9923f1b7d2
1 /* 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.
6 This file is part of GDB.
7 It has been modified to integrate it in valgrind
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.
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.
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. */
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 #include "pub_core_initimg.h"
30 #include "pub_core_execontext.h"
31 #include "pub_core_syswrap.h" // VG_(show_open_fds)
32 #include "pub_core_scheduler.h"
33 #include "pub_core_transtab.h"
34 #include "pub_core_debuginfo.h"
35 #include "pub_core_addrinfo.h"
36 #include "pub_core_aspacemgr.h"
38 unsigned long cont_thread;
39 unsigned long general_thread;
40 unsigned long step_thread;
41 unsigned long thread_from_wait;
42 unsigned long old_thread_from_wait;
44 int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */
46 /* for a gdbserver integrated in valgrind, resuming the process consists
47 in returning the control to valgrind.
48 The guess process resumes its execution.
49 Then at the next error or break or ..., valgrind calls gdbserver again.
50 A resume reply packet must then be built to inform GDB that the
51 resume request is finished.
52 resume_reply_packet_needed records the fact that the next call to gdbserver
53 must send a resume packet to gdb. */
54 static Bool resume_reply_packet_needed = False;
56 VG_MINIMAL_JMP_BUF(toplevel);
58 /* Decode a qXfer read request. Return 0 if everything looks OK,
59 or -1 otherwise. */
61 static
62 int decode_xfer_read (char *buf, const char **annex, CORE_ADDR *ofs, unsigned int *len)
64 /* Extract and NUL-terminate the annex. */
65 *annex = buf;
66 while (*buf && *buf != ':')
67 buf++;
68 if (*buf == '\0')
69 return -1;
70 *buf++ = 0;
72 /* After the read/write marker and annex, qXfer looks like a
73 traditional 'm' packet. */
74 decode_m_packet (buf, ofs, len);
76 return 0;
79 /* Write the response to a successful qXfer read. Returns the
80 length of the (binary) data stored in BUF, corresponding
81 to as much of DATA/LEN as we could fit. IS_MORE controls
82 the first character of the response. */
83 static
84 int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
86 int out_len;
88 if (is_more)
89 buf[0] = 'm';
90 else
91 buf[0] = 'l';
93 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
94 PBUFSIZ - POVERHSIZ - 1) + 1;
97 static Bool initial_valgrind_sink_saved = False;
98 /* True <=> valgrind log sink saved in initial_valgrind_sink */
99 static OutputSink initial_valgrind_sink;
101 static Bool command_output_to_log = False;
102 /* True <=> command output goes to log instead of gdb */
104 void reset_valgrind_sink(const char *info)
106 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd
107 && initial_valgrind_sink_saved) {
108 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
109 VG_(umsg) ("Reset valgrind output to log (%s)\n",
110 (info == NULL ? "" : info));
114 void print_to_initial_valgrind_sink (const char *msg)
116 vg_assert (initial_valgrind_sink_saved);
117 VG_(write) (initial_valgrind_sink.fd, msg, strlen(msg));
121 static
122 void kill_request (const char *msg)
124 VG_(umsg) ("%s", msg);
125 VG_(exit) (0);
128 // s is a NULL terminated string made of O or more words (separated by spaces).
129 // Returns a pointer to the Nth word in s.
130 // If Nth word does not exist, return a pointer to the last (0) byte of s.
131 static
132 const char *wordn (const char *s, int n)
134 int word_seen = 0;
135 Bool searching_word = True;
137 while (*s) {
138 if (*s == ' ')
139 searching_word = True;
140 else {
141 if (searching_word) {
142 searching_word = False;
143 word_seen++;
144 if (word_seen == n)
145 return s;
148 s++;
150 return s;
153 void VG_(print_all_stats) (Bool memory_stats, Bool tool_stats)
155 if (memory_stats) {
156 VG_(message)(Vg_DebugMsg, "\n");
157 VG_(message)(Vg_DebugMsg,
158 "------ Valgrind's internal memory use stats follow ------\n" );
159 VG_(sanity_check_malloc_all)();
160 VG_(message)
161 (Vg_DebugMsg,
162 "------ %'13llu bytes have already been mmap-ed ANONYMOUS.\n",
163 VG_(am_get_anonsize_total)());
164 VG_(print_all_arena_stats)();
165 if (VG_(clo_profile_heap))
166 VG_(print_arena_cc_analysis) ();
167 VG_(message)(Vg_DebugMsg, "\n");
170 VG_(print_translation_stats)();
171 VG_(print_tt_tc_stats)();
172 VG_(print_scheduler_stats)();
173 VG_(print_ExeContext_stats)( False /* with_stacktraces */ );
174 VG_(print_errormgr_stats)();
175 if (tool_stats && VG_(needs).print_stats) {
176 VG_TDICT_CALL(tool_print_stats);
180 /* handle_gdb_valgrind_command handles the provided mon string command.
181 If command is recognised, return 1 else return 0.
182 Note that in case of ambiguous command, 1 is returned.
184 *sink_wanted_at_return is modified if one of the commands
185 'v.set *_output' is handled.
187 static
188 int handle_gdb_valgrind_command (char *mon, OutputSink *sink_wanted_at_return)
190 UWord ret = 0;
191 char s[strlen(mon)+1]; /* copy for strtok_r */
192 char *wcmd;
193 HChar *ssaveptr;
194 const char *endptr;
195 int kwdid;
196 int int_value;
198 const DiEpoch cur_ep = VG_(current_DiEpoch)();
200 vg_assert (initial_valgrind_sink_saved);
202 strcpy (s, mon);
203 wcmd = strtok_r (s, " ", &ssaveptr);
204 /* NB: if possible, avoid introducing a new command below which
205 starts with the same 3 first letters as an already existing
206 command. This ensures a shorter abbreviation for the user. */
207 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate"
208 " v.do",
209 wcmd, kwd_report_duplicated_matches)) {
210 case -2:
211 ret = 1;
212 break;
213 case -1:
214 break;
215 case 0: /* help */
216 ret = 1;
217 wcmd = strtok_r (NULL, " ", &ssaveptr);
218 if (wcmd == NULL) {
219 int_value = 0;
220 } else {
221 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) {
222 case -2: int_value = 0; break;
223 case -1: int_value = 0; break;
224 case 0: int_value = 1; break;
225 default: vg_assert (0);
229 VG_(gdb_printf) (
230 "general valgrind monitor commands:\n"
231 " help [debug] : monitor command help. With debug: + debugging commands\n"
232 " v.wait [<ms>] : sleep <ms> (default 0) then continue\n"
233 " v.info all_errors : show all errors found so far\n"
234 " v.info last_error : show last error found\n"
235 " v.info location <addr> : show information about location <addr>\n"
236 " v.info n_errs_found [msg] : show the nr of errors found so far and the given msg\n"
237 " v.info open_fds : show open file descriptors (only if --track-fds=yes)\n"
238 " v.kill : kill the Valgrind process\n"
239 " v.set gdb_output : set valgrind output to gdb\n"
240 " v.set log_output : set valgrind output to log\n"
241 " v.set mixed_output : set valgrind output to log, interactive output to gdb\n"
242 " v.set merge-recursive-frames <num> : merge recursive calls in max <num> frames\n"
243 " v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
244 if (int_value) { VG_(gdb_printf) (
245 "debugging valgrind internals monitor commands:\n"
246 " v.do expensive_sanity_check_general : do an expensive sanity check now\n"
247 " v.info gdbserver_status : show gdbserver status\n"
248 " v.info memory [aspacemgr] : show valgrind heap memory stats\n"
249 " (with aspacemgr arg, also shows valgrind segments on log output)\n"
250 " v.info exectxt : show stacktraces and stats of all execontexts\n"
251 " v.info scheduler : show valgrind thread state and stacktrace\n"
252 " v.info stats : show various valgrind and tool stats\n"
253 " v.info unwind <addr> [<len>] : show unwind debug info for <addr> .. <addr+len>\n"
254 " v.set debuglog <level> : set valgrind debug log level to <level>\n"
255 " v.set hostvisibility [yes*|no] : (en/dis)ables access by gdb/gdbserver to\n"
256 " Valgrind internal host status/memory\n"
257 " v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
258 " (default traceflags 0b00100000 : show after instrumentation)\n"
259 " An additional flag 0b100000000 allows to show gdbserver instrumentation\n");
261 break;
262 case 1: /* v.set */
263 ret = 1;
264 wcmd = strtok_r (NULL, " ", &ssaveptr);
265 switch (kwdid = VG_(keyword_id)
266 ("vgdb-error debuglog merge-recursive-frames"
267 " gdb_output log_output mixed_output hostvisibility",
268 wcmd, kwd_report_all)) {
269 case -2:
270 case -1:
271 break;
272 case 0: /* vgdb-error */
273 case 1: /* debuglog */
274 case 2: /* merge-recursive-frames */
275 wcmd = strtok_r (NULL, " ", &ssaveptr);
276 if (wcmd == NULL) {
277 int_value = 0;
278 endptr = "empty"; /* to report an error below */
279 } else {
280 HChar *the_end;
281 int_value = strtol (wcmd, &the_end, 10);
282 endptr = the_end;
284 if (*endptr != '\0') {
285 VG_(gdb_printf) ("missing or malformed integer value\n");
286 } else if (kwdid == 0) {
287 VG_(printf) ("vgdb-error value changed from %d to %d\n",
288 VG_(dyn_vgdb_error), int_value);
289 VG_(dyn_vgdb_error) = int_value;
290 } else if (kwdid == 1) {
291 VG_(printf) ("debuglog value changed from %d to %d\n",
292 VG_(debugLog_getLevel)(), int_value);
293 VG_(debugLog_startup) (int_value, "gdbsrv");
294 } else if (kwdid == 2) {
295 VG_(printf)
296 ("merge-recursive-frames value changed from %d to %d\n",
297 VG_(clo_merge_recursive_frames), int_value);
298 VG_(clo_merge_recursive_frames) = int_value;
299 } else {
300 vg_assert (0);
302 break;
303 case 3: /* gdb_output */
304 (*sink_wanted_at_return).fd = -2;
305 command_output_to_log = False;
306 VG_(gdb_printf) ("valgrind output will go to gdb\n");
307 break;
308 case 4: /* log_output */
309 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
310 command_output_to_log = True;
311 VG_(gdb_printf) ("valgrind output will go to log\n");
312 break;
313 case 5: /* mixed output */
314 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
315 command_output_to_log = False;
316 VG_(gdb_printf)
317 ("valgrind output will go to log, "
318 "interactive output will go to gdb\n");
319 break;
320 case 6: /* hostvisibility */
321 wcmd = strtok_r (NULL, " ", &ssaveptr);
322 if (wcmd != NULL) {
323 switch (VG_(keyword_id) ("yes no", wcmd, kwd_report_all)) {
324 case -2:
325 case -1: break;
326 case 0:
327 hostvisibility = True;
328 break;
329 case 1:
330 hostvisibility = False;
331 break;
332 default: vg_assert (0);
334 } else {
335 hostvisibility = True;
337 if (hostvisibility) {
338 const DebugInfo *tooldi
339 = VG_(find_DebugInfo) (cur_ep, (Addr)handle_gdb_valgrind_command);
340 /* Normally, we should always find the tooldi. In case we
341 do not, suggest a 'likely somewhat working' address: */
342 const Addr tool_text_start
343 = tooldi ?
344 VG_(DebugInfo_get_text_avma) (tooldi) : 0x58000000;
345 const NSegment *toolseg
346 = tooldi ?
347 VG_(am_find_nsegment) (VG_(DebugInfo_get_text_avma) (tooldi))
348 : NULL;
349 VG_(gdb_printf)
350 ("Enabled access to Valgrind memory/status by GDB\n"
351 "If not yet done, tell GDB which valgrind file(s) to use, "
352 "typically:\n"
353 "add-symbol-file %s %p\n",
354 toolseg ? VG_(am_get_filename)(toolseg)
355 : "<toolfile> <address> e.g.",
356 (void*)tool_text_start);
357 } else
358 VG_(gdb_printf)
359 ("Disabled access to Valgrind memory/status by GDB\n");
360 break;
361 default:
362 vg_assert (0);
364 break;
365 case 2: /* v.info */ {
366 ret = 1;
367 wcmd = strtok_r (NULL, " ", &ssaveptr);
368 switch (kwdid = VG_(keyword_id)
369 ("all_errors n_errs_found last_error gdbserver_status memory"
370 " scheduler stats open_fds exectxt location unwind",
371 wcmd, kwd_report_all)) {
372 case -2:
373 case -1:
374 break;
375 case 0: // all_errors
376 // A verbosity of minimum 2 is needed to show the errors.
377 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False);
378 break;
379 case 1: // n_errs_found
380 VG_(printf) ("n_errs_found %u n_errs_shown %u (vgdb-error %d) %s\n",
381 VG_(get_n_errs_found) (),
382 VG_(get_n_errs_shown) (),
383 VG_(dyn_vgdb_error),
384 wordn (mon, 3));
385 break;
386 case 2: // last_error
387 VG_(show_last_error)();
388 break;
389 case 3: // gdbserver_status
390 VG_(gdbserver_status_output)();
391 break;
392 case 4: /* memory */
393 VG_(printf) ("%'13llu bytes have already been mmap-ed ANONYMOUS.\n",
394 VG_(am_get_anonsize_total)());
395 VG_(print_all_arena_stats) ();
396 if (VG_(clo_profile_heap))
397 VG_(print_arena_cc_analysis) ();
398 wcmd = strtok_r (NULL, " ", &ssaveptr);
399 if (wcmd != NULL) {
400 switch (VG_(keyword_id) ("aspacemgr", wcmd, kwd_report_all)) {
401 case -2:
402 case -1: break;
403 case 0:
404 VG_(am_show_nsegments) (0, "gdbserver v.info memory aspacemgr");
405 break;
406 default: vg_assert (0);
410 ret = 1;
411 break;
412 case 5: /* scheduler */
413 VG_(show_sched_status) (True, // host_stacktrace
414 True, // stack_usage
415 True); // exited_threads
416 ret = 1;
417 break;
418 case 6: /* stats */
419 VG_(print_all_stats)(False, /* Memory stats */
420 True /* Tool stats */);
421 ret = 1;
422 break;
423 case 7: /* open_fds */
424 if (VG_(clo_track_fds))
425 VG_(show_open_fds) ("");
426 else
427 VG_(gdb_printf)
428 ("Valgrind must be started with --track-fds=yes"
429 " to show open fds\n");
430 ret = 1;
431 break;
432 case 8: /* exectxt */
433 VG_(print_ExeContext_stats) (True /* with_stacktraces */);
434 ret = 1;
435 break;
436 case 9: { /* location */
437 /* Note: we prefer 'v.info location' and not 'v.info address' as
438 v.info address is inconsistent with the GDB (native)
439 command 'info address' which gives the address for a symbol.
440 GDB equivalent command of 'v.info location' is 'info symbol'. */
441 Addr address;
442 SizeT dummy_sz = 0x1234;
443 if (VG_(strtok_get_address_and_size) (&address,
444 &dummy_sz, &ssaveptr)) {
445 // If tool provides location information, use that.
446 if (VG_(needs).info_location) {
447 VG_TDICT_CALL(tool_info_location, cur_ep, address);
449 // If tool does not provide location info, use the common one.
450 // Also use the common to compare with tool when debug log is set.
451 if (!VG_(needs).info_location || VG_(debugLog_getLevel)() > 0 ) {
452 AddrInfo ai;
453 ai.tag = Addr_Undescribed;
454 VG_(describe_addr) (cur_ep, address, &ai);
455 VG_(pp_addrinfo) (address, &ai);
456 VG_(clear_addrinfo) (&ai);
459 ret = 1;
460 break;
462 case 10: { /* unwind */
463 Addr address;
464 SizeT sz = 1;
465 if (VG_(strtok_get_address_and_size) (&address,
466 &sz, &ssaveptr)) {
467 VG_(ppUnwindInfo) (address, address + sz - 1);
469 ret = 1;
470 break;
473 default:
474 vg_assert(0);
476 break;
478 case 3: /* v.wait */
479 wcmd = strtok_r (NULL, " ", &ssaveptr);
480 if (wcmd != NULL) {
481 int_value = strtol (wcmd, NULL, 10);
482 VG_(printf) ("gdbserver: continuing in %d ms ...\n", int_value);
483 VG_(poll)(NULL, 0, int_value);
485 VG_(printf) ("gdbserver: continuing after wait ...\n");
486 ret = 1;
487 break;
488 case 4: /* v.kill */
489 kill_request ("monitor command request to kill this process\n");
490 break;
491 case 5: { /* v.translate */
492 Addr address;
493 SizeT verbosity = 0x20;
495 ret = 1;
497 if (VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr)) {
498 /* we need to force the output to log for the translation trace,
499 as low level VEX tracing cannot be redirected to gdb. */
500 int saved_command_output_to_log = command_output_to_log;
501 int saved_fd = VG_(log_output_sink).fd;
502 Bool single_stepping_on_entry = valgrind_single_stepping();
503 int vex_verbosity = verbosity & 0xff;
504 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
505 if ((verbosity & 0x100) && !single_stepping_on_entry) {
506 valgrind_set_single_stepping(True);
507 // to force gdbserver instrumentation.
509 # if defined(VGA_arm)
510 // on arm, we need to (potentially) convert this address
511 // to the thumb form.
512 address = thumb_pc (address);
513 # endif
515 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
516 address,
517 /*debugging*/True,
518 (Int) vex_verbosity,
519 /*bbs_done*/0,
520 /*allow redir?*/True);
521 if ((verbosity & 0x100) && !single_stepping_on_entry) {
522 valgrind_set_single_stepping(False);
523 // reset single stepping.
525 command_output_to_log = saved_command_output_to_log;
526 VG_(log_output_sink).fd = saved_fd;
528 break;
531 case 6: /* v.do */
532 ret = 1;
533 wcmd = strtok_r (NULL, " ", &ssaveptr);
534 switch (VG_(keyword_id) ("expensive_sanity_check_general",
535 wcmd, kwd_report_all)) {
536 case -2:
537 case -1: break;
538 case 0: { /* expensive_sanity_check_general */
539 // Temporarily bump up sanity level to check e.g. the malloc arenas.
540 const Int save_clo_sanity_level = VG_(clo_sanity_level);
541 if (VG_(clo_sanity_level) < 4) VG_(clo_sanity_level) = 4;
542 VG_(sanity_check_general) (/* force_expensive */ True);
543 VG_(clo_sanity_level) = save_clo_sanity_level;
544 break;
546 default: vg_assert (0);
548 break;
550 default:
551 vg_assert (0);
553 return ret;
556 /* handle_gdb_monitor_command handles the provided mon string command,
557 which can be either a "standard" valgrind monitor command
558 or a tool specific monitor command.
559 If command recognised, return 1 else return 0.
560 Note that in case of ambiguous command, 1 is returned.
562 static
563 int handle_gdb_monitor_command (char *mon)
565 UWord ret = 0;
566 UWord tool_ret = 0;
567 // initially, we assume that when returning, the desired sink is the
568 // one we have when entering. It can however be changed by the standard
569 // valgrind command handling.
570 OutputSink sink_wanted_at_return = VG_(log_output_sink);
571 // When using gdbserver, we temporarily disable xml output.
572 Bool save_clo_xml = VG_(clo_xml);
573 VG_(clo_xml) = False;
575 if (!initial_valgrind_sink_saved) {
576 /* first time we enter here, we save the valgrind default log sink */
577 initial_valgrind_sink = sink_wanted_at_return;
578 initial_valgrind_sink_saved = True;
581 if (!command_output_to_log)
582 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */
584 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return);
586 /* Even if command was recognised by valgrind core, we call the
587 tool command handler : this is needed to handle help command
588 and/or to let the tool do some additional processing of a
589 valgrind standard command. Note however that if valgrind
590 recognised the command, we will always return success. */
591 if (VG_(needs).client_requests) {
592 /* If the tool reports an error when handling a monitor command,
593 we need to avoid calling gdbserver during this command
594 handling. So, we temporarily set VG_(dyn_vgdb_error) to
595 a huge value to ensure m_errormgr.c does not call gdbserver. */
596 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error);
597 UWord arg[2];
598 VG_(dyn_vgdb_error) = 999999999;
599 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND;
600 arg[1] = (UWord) mon;
601 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg,
602 &tool_ret);
603 VG_(dyn_vgdb_error) = save_dyn_vgdb_error;
606 VG_(message_flush) ();
608 /* restore or set the desired output */
609 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
610 VG_(clo_xml) = save_clo_xml;
612 if (ret | tool_ret)
613 return 1;
614 else
615 return 0;
619 /* Handle all of the extended 'Q' packets. */
620 static
621 void handle_set (char *arg_own_buf, int *new_packet_len_p)
623 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) {
624 noack_mode = True;
625 write_ok (arg_own_buf);
626 return;
629 if (strcmp ("QCatchSyscalls:0", arg_own_buf) == 0) {
630 dlog (3, "catch syscall all off\n");
631 catching_syscalls = False;
632 write_ok (arg_own_buf);
633 return;
636 const char *q1 = "QCatchSyscalls:1";
637 if (strncmp (q1, arg_own_buf, strlen(q1)) == 0) {
638 Int i;
639 const char *p;
641 if (syscalls_to_catch != NULL) {
642 free (syscalls_to_catch);
643 syscalls_to_catch = NULL;
645 syscalls_to_catch_size = 0;
646 p = arg_own_buf + strlen(q1);
647 while (*p) {
648 if (*p++ == ';')
649 syscalls_to_catch_size++;
651 if (syscalls_to_catch_size > 0) {
652 CORE_ADDR sysno;
653 char *from, *to;
655 syscalls_to_catch = malloc (syscalls_to_catch_size * sizeof (int));
657 from = strchr (arg_own_buf, ';') + 1;
658 for (i = 0; i < syscalls_to_catch_size; i++) {
659 to = strchr (from, ';');
660 if (to == NULL)
661 to = arg_own_buf + strlen (arg_own_buf);
662 decode_address (&sysno, from, to - from);
663 syscalls_to_catch[i] = (Int)sysno;
664 dlog(4, "catch syscall sysno %d\n", (int)sysno);
665 from = to;
666 if (*from == ';') from++;
668 } else
669 dlog (4, "catch syscall all sysno\n");
670 catching_syscalls = True;
671 write_ok (arg_own_buf);
672 return;
675 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
676 int i;
677 char *from, *to;
678 char *end = arg_own_buf + strlen(arg_own_buf);
679 CORE_ADDR sig;
680 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
681 pass_signals[i] = 0;
683 from = arg_own_buf + 13;
684 while (from < end) {
685 to = strchr(from, ';');
686 if (to == NULL) to = end;
687 decode_address (&sig, from, to - from);
688 pass_signals[(int)sig] = 1;
689 dlog(3, "pass_signal gdb_nr %d %s\n",
690 (int)sig, target_signal_to_name(sig));
691 from = to;
692 if (*from == ';') from++;
694 write_ok (arg_own_buf);
695 return;
697 /* Otherwise we didn't know what packet it was. Say we didn't
698 understand it. */
699 arg_own_buf[0] = 0;
702 Bool VG_(client_monitor_command) (HChar *cmd)
704 const Bool connected = remote_connected();
705 const int saved_command_output_to_log = command_output_to_log;
706 Bool handled;
708 if (!connected)
709 command_output_to_log = True;
710 handled = handle_gdb_monitor_command (cmd);
711 if (!connected) {
712 // reset the log output unless cmd changed it.
713 if (command_output_to_log)
714 command_output_to_log = saved_command_output_to_log;
716 if (handled)
717 return False; // recognised
718 else
719 return True; // not recognised
722 /* Handle all of the extended 'q' packets. */
723 static
724 void handle_query (char *arg_own_buf, int *new_packet_len_p)
726 static struct inferior_list_entry *thread_ptr;
728 /* thread local storage query */
729 if (strncmp ("qGetTLSAddr:", arg_own_buf, 12) == 0) {
730 char *from, *to;
731 char *end = arg_own_buf + strlen(arg_own_buf);
732 unsigned long gdb_id;
733 CORE_ADDR lm;
734 CORE_ADDR offset;
735 struct thread_info *ti;
737 from = arg_own_buf + 12;
738 to = strchr(from, ',');
739 *to = 0;
740 gdb_id = strtoul (from, NULL, 16);
741 from = to + 1;
742 to = strchr(from, ',');
743 decode_address (&offset, from, to - from);
744 from = to + 1;
745 to = end;
746 decode_address (&lm, from, to - from);
747 dlog(2, "qGetTLSAddr thread %lu offset %p lm %p\n",
748 gdb_id, (void*)offset, (void*)lm);
750 ti = gdb_id_to_thread (gdb_id);
751 if (ti != NULL) {
752 ThreadState *tst;
753 Addr tls_addr;
755 tst = (ThreadState *) inferior_target_data (ti);
756 if (valgrind_get_tls_addr(tst, offset, lm, &tls_addr)) {
757 VG_(sprintf) (arg_own_buf, "%lx", tls_addr);
758 return;
760 // else we will report we do not support qGetTLSAddr
761 } else {
762 write_enn (arg_own_buf);
763 return;
767 /* qRcmd, monitor command handling. */
768 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
769 char *p = arg_own_buf + 6;
770 int cmdlen = strlen(p)/2;
771 char cmd[cmdlen+1];
773 if (unhexify (cmd, p, cmdlen) != cmdlen) {
774 write_enn (arg_own_buf);
775 return;
777 cmd[cmdlen] = '\0';
779 if (handle_gdb_monitor_command (cmd)) {
780 write_ok (arg_own_buf);
781 return;
782 } else {
783 /* cmd not recognised */
784 VG_(gdb_printf)
785 ("command '%s' not recognised\n"
786 "In gdb, try 'monitor help'\n"
787 "In a shell, try 'vgdb help'\n",
788 cmd);
789 write_ok (arg_own_buf);
790 return;
794 /* provide some valgrind specific info in return to qThreadExtraInfo. */
795 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
796 unsigned long gdb_id;
797 struct thread_info *ti;
798 ThreadState *tst;
800 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
801 ti = gdb_id_to_thread (gdb_id);
802 if (ti != NULL) {
803 tst = (ThreadState *) inferior_target_data (ti);
804 /* Additional info is the tid, the thread status and the thread's
805 name, if any. */
806 SizeT len = strlen(VG_(name_of_ThreadStatus)(tst->status)) + 20;
807 if (tst->thread_name) len += strlen(tst->thread_name);
808 /* As the string will be hexified and copied into own_buf we need
809 to limit the length to avoid buffer overflow. */
810 if (len * 2 > (PBUFSIZ + POVERHSIZ))
811 len = (PBUFSIZ + POVERHSIZ) / 2;
812 char status[len];
813 if (tst->thread_name) {
814 VG_(snprintf) (status, sizeof(status), "tid %u %s %s",
815 tst->tid,
816 VG_(name_of_ThreadStatus)(tst->status),
817 tst->thread_name);
818 } else {
819 VG_(snprintf) (status, sizeof(status), "tid %u %s",
820 tst->tid,
821 VG_(name_of_ThreadStatus)(tst->status));
823 hexify (arg_own_buf, status, strlen(status));
824 return;
825 } else {
826 write_enn (arg_own_buf);
827 return;
831 if (strcmp ("qAttached", arg_own_buf) == 0) {
832 /* tell gdb to always detach, never kill the process */
833 arg_own_buf[0] = '1';
834 arg_own_buf[1] = 0;
835 return;
838 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
839 /* We have no symbol to read. */
840 write_ok (arg_own_buf);
841 return;
844 if (strcmp ("qC", arg_own_buf) == 0) {
845 VG_(sprintf) (arg_own_buf, "QC%x",
846 thread_to_gdb_id (current_inferior));
847 return;
850 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
851 thread_ptr = all_threads.head;
852 VG_(sprintf) (arg_own_buf, "m%x",
853 thread_to_gdb_id ((struct thread_info *)thread_ptr));
854 thread_ptr = thread_ptr->next;
855 return;
858 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
859 if (thread_ptr != NULL) {
860 VG_(sprintf) (arg_own_buf, "m%x",
861 thread_to_gdb_id ((struct thread_info *)thread_ptr));
862 thread_ptr = thread_ptr->next;
863 return;
864 } else {
865 VG_(sprintf) (arg_own_buf, "l");
866 return;
870 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL
871 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
872 CORE_ADDR ofs;
873 unsigned int len, doc_len;
874 const char *annex = NULL;
875 // First, the annex is extracted from the packet received.
876 // Then, it is replaced by the corresponding file name.
877 int fd;
879 /* Grab the annex, offset, and length. */
880 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
881 strcpy (arg_own_buf, "E00");
882 return;
885 if (strcmp (annex, "target.xml") == 0) {
886 annex = valgrind_target_xml(VG_(clo_vgdb_shadow_registers));
887 if (annex != NULL && VG_(clo_vgdb_shadow_registers)) {
888 /* Ensure the shadow registers are initialized. */
889 initialize_shadow_low(True);
891 if (annex == NULL) {
892 strcpy (arg_own_buf, "E00");
893 return;
898 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1];
899 struct vg_stat stat_doc;
900 char toread[len];
901 int len_read;
903 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
904 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
905 if (fd == -1) {
906 strcpy (arg_own_buf, "E00");
907 return;
909 if (VG_(fstat) (fd, &stat_doc) != 0) {
910 VG_(close) (fd);
911 strcpy (arg_own_buf, "E00");
912 return;
914 doc_len = stat_doc.size;
916 if (len > PBUFSIZ - POVERHSIZ)
917 len = PBUFSIZ - POVERHSIZ;
919 if (ofs > doc_len) {
920 write_enn (arg_own_buf);
921 VG_(close) (fd);
922 return;
924 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
925 len_read = VG_(read) (fd, toread, len);
926 *new_packet_len_p = write_qxfer_response (arg_own_buf,
927 (unsigned char *)toread,
928 len_read,
929 ofs + len_read < doc_len);
930 VG_(close) (fd);
931 return;
935 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) {
936 unsigned char *data;
937 int n;
938 CORE_ADDR ofs;
939 unsigned int len;
940 const char *annex;
942 /* Reject any annex; grab the offset and length. */
943 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0
944 || annex[0] != '\0') {
945 strcpy (arg_own_buf, "E00");
946 return;
949 if (len > PBUFSIZ - POVERHSIZ)
950 len = PBUFSIZ - POVERHSIZ;
951 data = malloc (len);
954 UWord *client_auxv = VG_(client_auxv);
955 unsigned int client_auxv_len = 0;
956 while (*client_auxv != 0) {
957 dlog(4, "auxv %llu %llx\n",
958 (ULong)*client_auxv,
959 (ULong)*(client_auxv+1));
960 client_auxv++;
961 client_auxv++;
962 client_auxv_len += 2 * sizeof(UWord);
964 client_auxv_len += 2 * sizeof(UWord);
965 dlog(4, "auxv len %u\n", client_auxv_len);
967 if (ofs >= client_auxv_len)
968 n = -1;
969 else {
970 n = client_auxv_len - ofs;
971 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n);
975 if (n < 0)
976 write_enn (arg_own_buf);
977 else if (n > len)
978 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
979 else
980 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
982 free (data);
984 return;
987 if (strncmp ("qXfer:exec-file:read:", arg_own_buf, 21) == 0) {
988 unsigned char *data;
989 int n;
990 CORE_ADDR ofs;
991 unsigned int len;
992 const char *annex;
993 unsigned long pid;
994 const HChar *name;
996 /* grab the annex, offset and length. */
997 if (decode_xfer_read (arg_own_buf + 21, &annex, &ofs, &len) < 0) {
998 strcpy (arg_own_buf, "E00");
999 return;
1002 /* Reject any annex with invalid/unexpected pid */
1003 if (strlen(annex) > 0)
1004 pid = strtoul (annex, NULL, 16);
1005 else
1006 pid = 0;
1007 if ((int)pid != VG_(getpid)() && pid != 0) {
1008 VG_(sprintf) (arg_own_buf,
1009 "E.Valgrind gdbserver pid is %d."
1010 " Cannot give info for pid %d",
1011 VG_(getpid)(), (int) pid);
1012 return;
1015 if (len > PBUFSIZ - 2)
1016 len = PBUFSIZ - 2;
1017 data = malloc (len);
1019 if (!VG_(resolve_filename)(VG_(cl_exec_fd), &name)) {
1020 VG_(sprintf) (arg_own_buf,
1021 "E.Valgrind gdbserver could not"
1022 " resolve pid %d exec filename.",
1023 VG_(getpid)());
1024 return;
1027 if (ofs >= strlen(name))
1028 n = -1;
1029 else {
1030 n = strlen(name) - ofs;
1031 VG_(memcpy) (data, name, n);
1034 if (n < 0)
1035 write_enn (arg_own_buf);
1036 else if (n > len)
1037 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
1038 else
1039 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
1041 free (data);
1043 return;
1046 if (strncmp ("qXfer:siginfo:read:", arg_own_buf, 19) == 0) {
1047 vki_siginfo_t info;
1048 int n;
1049 CORE_ADDR ofs;
1050 unsigned int len;
1051 const char *annex;
1053 /* Reject any annex; grab the offset and length. */
1054 if (decode_xfer_read (arg_own_buf + 19, &annex, &ofs, &len) < 0
1055 || annex[0] != '\0') {
1056 strcpy (arg_own_buf, "E00");
1057 return;
1060 if (len > PBUFSIZ - POVERHSIZ)
1061 len = PBUFSIZ - POVERHSIZ;
1063 gdbserver_pending_signal_to_report(&info);
1065 if (ofs >= sizeof(info))
1066 n = -1;
1067 else
1068 n = sizeof(info) - ofs;
1070 if (n < 0)
1071 write_enn (arg_own_buf);
1072 else if (n > len)
1073 *new_packet_len_p = write_qxfer_response (arg_own_buf,
1074 (unsigned char *)&info,
1075 len, 1);
1076 else
1077 *new_packet_len_p = write_qxfer_response (arg_own_buf,
1078 (unsigned char *)&info,
1079 n, 0);
1081 return;
1084 /* Protocol features query. */
1085 if (strncmp ("qSupported", arg_own_buf, 10) == 0
1086 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
1087 VG_(sprintf) (arg_own_buf, "PacketSize=%x", (UInt)PBUFSIZ - 1);
1088 /* Note: max packet size including frame and checksum, but without
1089 trailing null byte, which is not sent/received. */
1091 strcat (arg_own_buf, ";QStartNoAckMode+");
1092 strcat (arg_own_buf, ";QPassSignals+");
1093 strcat (arg_own_buf, ";QCatchSyscalls+");
1094 if (VG_(client_auxv))
1095 strcat (arg_own_buf, ";qXfer:auxv:read+");
1097 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) {
1098 strcat (arg_own_buf, ";qXfer:features:read+");
1099 /* if a new gdb connects to us, we have to reset the register
1100 set to the normal register sets to allow this new gdb to
1101 decide to use or not the shadow registers.
1103 Note that the reset is only done for gdb that are sending
1104 qSupported packets. If a user first connected with a recent
1105 gdb using shadow registers and then with a very old gdb
1106 that does not use qSupported packet, then the old gdb will
1107 not properly connect. */
1108 initialize_shadow_low(False);
1110 strcat (arg_own_buf, ";qXfer:exec-file:read+");
1111 strcat (arg_own_buf, ";qXfer:siginfo:read+");
1112 return;
1115 /* Otherwise we didn't know what packet it was. Say we didn't
1116 understand it. */
1117 arg_own_buf[0] = 0;
1120 /* Handle all of the extended 'v' packets. */
1121 static
1122 void handle_v_requests (char *arg_own_buf, char *status, int *zignal)
1124 /* vcont packet code from gdb 6.6 removed */
1126 /* Otherwise we didn't know what packet it was. Say we didn't
1127 understand it. */
1128 arg_own_buf[0] = 0;
1129 return;
1132 static
1133 void myresume (int step, int sig)
1135 struct thread_resume resume_info[2];
1136 int n = 0;
1138 if (step || sig) {
1139 resume_info[0].step = step;
1140 resume_info[0].sig = sig;
1141 n++;
1143 resume_info[n].step = 0;
1144 resume_info[n].sig = 0;
1146 resume_reply_packet_needed = True;
1147 valgrind_resume (resume_info);
1150 /* server_main global variables */
1151 static char *own_buf;
1152 static unsigned char *mem_buf;
1154 void gdbserver_init (void)
1156 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
1157 noack_mode = False;
1158 valgrind_initialize_target ();
1159 // After a fork, gdbserver_init can be called again.
1160 // We do not have to re-malloc the buffers in such a case.
1161 if (own_buf == NULL)
1162 own_buf = malloc (PBUFSIZ+POVERHSIZ);
1163 if (mem_buf == NULL)
1164 mem_buf = malloc (PBUFSIZ+POVERHSIZ);
1165 // Note: normally, we should only malloc PBUFSIZ. However,
1166 // GDB has a bug, and in some cases, sends e.g. 'm' packets
1167 // asking for slightly more than the PacketSize given at
1168 // connection initialisation. So, we bypass the GDB bug
1169 // by allocating slightly more.
1172 void gdbserver_terminate (void)
1174 /* last call to gdbserver is cleanup call */
1175 if (VG_MINIMAL_SETJMP(toplevel)) {
1176 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
1177 return;
1179 remote_close();
1182 void server_main (void)
1184 static char status;
1185 static int zignal;
1187 char ch;
1188 int i = 0;
1189 unsigned int len;
1190 CORE_ADDR mem_addr;
1192 zignal = valgrind_wait (&status);
1193 if (VG_MINIMAL_SETJMP(toplevel)) {
1194 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
1196 while (1) {
1197 unsigned char sig;
1198 int packet_len;
1199 int new_packet_len = -1;
1201 if (resume_reply_packet_needed) {
1202 /* Send the resume reply to reply to last GDB resume
1203 request. */
1204 resume_reply_packet_needed = False;
1205 prepare_resume_reply (own_buf, status, zignal);
1206 putpkt (own_buf);
1209 /* If our status is terminal (exit or fatal signal) get out
1210 as quickly as we can. We won't be able to handle any request
1211 anymore. */
1212 if (status == 'W' || status == 'X') {
1213 return;
1216 packet_len = getpkt (own_buf);
1217 if (packet_len <= 0)
1218 break;
1220 i = 0;
1221 ch = own_buf[i++];
1222 switch (ch) {
1223 case 'Q':
1224 handle_set (own_buf, &new_packet_len);
1225 break;
1226 case 'q':
1227 handle_query (own_buf, &new_packet_len);
1228 break;
1229 case 'd':
1230 /* set/unset debugging is done through valgrind debug level. */
1231 own_buf[0] = '\0';
1232 break;
1233 case 'D':
1234 reset_valgrind_sink("gdb detaching from process");
1236 /* When detaching or kill the process, gdb expects to get
1237 an packet OK back. Any other output will make gdb
1238 believes detach did not work. */
1239 write_ok (own_buf);
1240 putpkt (own_buf);
1241 remote_finish (reset_after_error);
1242 remote_open (VG_(clo_vgdb_prefix));
1243 myresume (0, 0);
1244 resume_reply_packet_needed = False;
1245 return;
1246 case '!':
1247 /* We can not use the extended protocol with valgrind,
1248 because we can not restart the running
1249 program. So return unrecognized. */
1250 own_buf[0] = '\0';
1251 break;
1252 case '?':
1253 prepare_resume_reply (own_buf, status, zignal);
1254 break;
1255 case 'H':
1256 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
1257 unsigned long gdb_id, thread_id;
1259 gdb_id = strtoul (&own_buf[2], NULL, 16);
1260 thread_id = gdb_id_to_thread_id (gdb_id);
1261 if (thread_id == 0) {
1262 write_enn (own_buf);
1263 break;
1266 if (own_buf[1] == 'g') {
1267 general_thread = thread_id;
1268 set_desired_inferior (1);
1269 } else if (own_buf[1] == 'c') {
1270 cont_thread = thread_id;
1271 } else if (own_buf[1] == 's') {
1272 step_thread = thread_id;
1275 write_ok (own_buf);
1276 } else {
1277 /* Silently ignore it so that gdb can extend the protocol
1278 without compatibility headaches. */
1279 own_buf[0] = '\0';
1281 break;
1282 case 'g':
1283 set_desired_inferior (1);
1284 registers_to_string (own_buf);
1285 break;
1286 case 'G':
1287 set_desired_inferior (1);
1288 registers_from_string (&own_buf[1]);
1289 write_ok (own_buf);
1290 break;
1291 case 'P': {
1292 int regno;
1293 char *regbytes;
1294 Bool mod;
1295 ThreadState *tst;
1296 regno = strtol(&own_buf[1], NULL, 16);
1297 regbytes = strchr(&own_buf[0], '=') + 1;
1298 set_desired_inferior (1);
1299 tst = (ThreadState *) inferior_target_data (current_inferior);
1300 /* Only accept changing registers in "runnable state3.
1301 In fact, it would be ok to change most of the registers
1302 except a few "sensitive" registers such as the PC, SP, BP.
1303 We assume we do not need to very specific here, and that we
1304 can just refuse all of these. */
1305 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
1306 supply_register_from_string (regno, regbytes, &mod);
1307 write_ok (own_buf);
1308 } else {
1309 /* at least from gdb 6.6 onwards, an E. error
1310 reply is shown to the user. So, we do an error
1311 msg which both is accepted by gdb as an error msg
1312 and is readable by the user. */
1313 VG_(sprintf)
1314 (own_buf,
1315 "E.\n"
1316 "ERROR changing register %s regno %d\n"
1317 "gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
1318 "set pc, calling from gdb a function in the debugged process, ...)\n"
1319 "can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
1320 "Thread status is %s\n",
1321 find_register_by_number (regno)->name, regno,
1322 VG_(name_of_ThreadStatus)(tst->status));
1323 if (VG_(clo_verbosity) > 1)
1324 VG_(umsg) ("%s\n", own_buf);
1326 break;
1328 case 'm':
1329 decode_m_packet (&own_buf[1], &mem_addr, &len);
1330 if (valgrind_read_memory (mem_addr, mem_buf, len) == 0)
1331 convert_int_to_ascii (mem_buf, own_buf, len);
1332 else
1333 write_enn (own_buf);
1334 break;
1335 case 'M':
1336 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
1337 if (valgrind_write_memory (mem_addr, mem_buf, len) == 0)
1338 write_ok (own_buf);
1339 else
1340 write_enn (own_buf);
1341 break;
1342 case 'X':
1343 if (decode_X_packet (&own_buf[1], packet_len - 1,
1344 &mem_addr, &len, mem_buf) < 0
1345 || valgrind_write_memory (mem_addr, mem_buf, len) != 0)
1346 write_enn (own_buf);
1347 else
1348 write_ok (own_buf);
1349 break;
1350 case 'C':
1351 convert_ascii_to_int (own_buf + 1, &sig, 1);
1352 if (target_signal_to_host_p (sig))
1353 zignal = target_signal_to_host (sig);
1354 else
1355 zignal = 0;
1356 set_desired_inferior (0);
1357 myresume (0, zignal);
1358 return; // return control to valgrind
1359 case 'S':
1360 convert_ascii_to_int (own_buf + 1, &sig, 1);
1361 if (target_signal_to_host_p (sig))
1362 zignal = target_signal_to_host (sig);
1363 else
1364 zignal = 0;
1365 set_desired_inferior (0);
1366 myresume (1, zignal);
1367 return; // return control to valgrind
1368 case 'c':
1369 set_desired_inferior (0);
1370 myresume (0, 0);
1371 return; // return control to valgrind
1372 case 's':
1373 set_desired_inferior (0);
1374 myresume (1, 0);
1375 return; // return control to valgrind
1376 case 'Z': {
1377 char *lenptr;
1378 char *dataptr;
1379 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1380 int zlen = strtol (lenptr + 1, &dataptr, 16);
1381 char type = own_buf[1];
1383 if (type < '0' || type > '4') {
1384 /* Watchpoint command type unrecognized. */
1385 own_buf[0] = '\0';
1386 } else {
1387 int res;
1389 res = valgrind_insert_watchpoint (type, addr, zlen);
1390 if (res == 0)
1391 write_ok (own_buf);
1392 else if (res == 1)
1393 /* Unsupported. */
1394 own_buf[0] = '\0';
1395 else
1396 write_enn (own_buf);
1398 break;
1400 case 'z': {
1401 char *lenptr;
1402 char *dataptr;
1403 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1404 int zlen = strtol (lenptr + 1, &dataptr, 16);
1405 char type = own_buf[1];
1407 if (type < '0' || type > '4') {
1408 /* Watchpoint command type unrecognized. */
1409 own_buf[0] = '\0';
1410 } else {
1411 int res;
1413 res = valgrind_remove_watchpoint (type, addr, zlen);
1414 if (res == 0)
1415 write_ok (own_buf);
1416 else if (res == 1)
1417 /* Unsupported. */
1418 own_buf[0] = '\0';
1419 else
1420 write_enn (own_buf);
1422 break;
1424 case 'k':
1425 kill_request("Gdb request to kill this process\n");
1426 break;
1427 case 'T': {
1428 unsigned long gdb_id, thread_id;
1430 gdb_id = strtoul (&own_buf[1], NULL, 16);
1431 thread_id = gdb_id_to_thread_id (gdb_id);
1432 if (thread_id == 0) {
1433 write_enn (own_buf);
1434 break;
1437 if (valgrind_thread_alive (thread_id))
1438 write_ok (own_buf);
1439 else
1440 write_enn (own_buf);
1441 break;
1443 case 'R':
1444 /* Restarting the inferior is only supported in the
1445 extended protocol.
1446 => It is a request we don't understand. Respond with an
1447 empty packet so that gdb knows that we don't support this
1448 request. */
1449 own_buf[0] = '\0';
1450 break;
1451 case 'v':
1452 /* Extended (long) request. */
1453 handle_v_requests (own_buf, &status, &zignal);
1454 break;
1455 default:
1456 /* It is a request we don't understand. Respond with an
1457 empty packet so that gdb knows that we don't support this
1458 request. */
1459 own_buf[0] = '\0';
1460 break;
1463 if (new_packet_len != -1)
1464 putpkt_binary (own_buf, new_packet_len);
1465 else
1466 putpkt (own_buf);
1468 if (status == 'W')
1469 VG_(umsg) ("\nChild exited with status %d\n", zignal);
1470 if (status == 'X')
1471 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
1472 (UInt)target_signal_to_host (zignal),
1473 target_signal_to_name (zignal));
1474 if (status == 'W' || status == 'X') {
1475 VG_(umsg) ("Process exiting\n");
1476 VG_(exit) (0);
1480 /* We come here when getpkt fails => close the connection,
1481 and re-open. Then return control to valgrind.
1482 We return the control to valgrind as we assume that
1483 the connection was closed due to vgdb having finished
1484 to execute a command. */
1485 if (VG_(clo_verbosity) > 1)
1486 VG_(umsg) ("Remote side has terminated connection. "
1487 "GDBserver will reopen the connection.\n");
1488 remote_finish (reset_after_error);
1489 remote_open (VG_(clo_vgdb_prefix));
1490 myresume (0, 0);
1491 resume_reply_packet_needed = False;
1492 return;