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