1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
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. */
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,
62 int decode_xfer_read (char *buf
, const char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
64 /* Extract and NUL-terminate the annex. */
66 while (*buf
&& *buf
!= ':')
72 /* After the read/write marker and annex, qXfer looks like a
73 traditional 'm' packet. */
74 decode_m_packet (buf
, ofs
, len
);
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. */
84 int write_qxfer_response (char *buf
, unsigned char *data
, int len
, int is_more
)
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
));
122 void kill_request (const char *msg
)
124 VG_(umsg
) ("%s", msg
);
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.
132 const char *wordn (const char *s
, int n
)
135 Bool searching_word
= True
;
139 searching_word
= True
;
141 if (searching_word
) {
142 searching_word
= False
;
153 void VG_(print_all_stats
) (Bool memory_stats
, Bool tool_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
)();
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.
188 int handle_gdb_valgrind_command (char *mon
, OutputSink
*sink_wanted_at_return
)
191 char s
[strlen(mon
)+1]; /* copy for strtok_r */
198 const DiEpoch cur_ep
= VG_(current_DiEpoch
)();
200 vg_assert (initial_valgrind_sink_saved
);
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"
209 wcmd
, kwd_report_duplicated_matches
)) {
217 wcmd
= strtok_r (NULL
, " ", &ssaveptr
);
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);
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 [also_suppressed] : 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|all])\n"
238 " v.kill : kill the Valgrind process\n"
239 " v.clo <clo_option>... : changes one or more dynamic command line options\n"
240 " with no clo_option, show the dynamically changeable options.\n"
241 " v.set gdb_output : set valgrind output to gdb\n"
242 " v.set log_output : set valgrind output to log\n"
243 " v.set mixed_output : set valgrind output to log, interactive output to gdb\n"
244 " v.set merge-recursive-frames <num> : merge recursive calls in max <num> frames\n"
245 " v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
246 if (int_value
) { VG_(gdb_printf
) (
247 "debugging valgrind internals monitor commands:\n"
248 " v.do expensive_sanity_check_general : do an expensive sanity check now\n"
249 " v.info gdbserver_status : show gdbserver status\n"
250 " v.info memory [aspacemgr] : show valgrind heap memory stats\n"
251 " (with aspacemgr arg, also shows valgrind segments on log output)\n"
252 " v.info exectxt : show stacktraces and stats of all execontexts\n"
253 " v.info scheduler : show valgrind thread state and stacktrace\n"
254 " v.info stats : show various valgrind and tool stats\n"
255 " v.info unwind <addr> [<len>] : show unwind debug info for <addr> .. <addr+len>\n"
256 " v.set debuglog <level> : set valgrind debug log level to <level>\n"
257 " v.set hostvisibility [yes*|no] : (en/dis)ables access by gdb/gdbserver to\n"
258 " Valgrind internal host status/memory\n"
259 " v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
260 " (default traceflags 0b00100000 : show after instrumentation)\n"
261 " An additional flag 0b100000000 allows one to show gdbserver instrumentation\n");
266 wcmd
= strtok_r (NULL
, " ", &ssaveptr
);
267 switch (kwdid
= VG_(keyword_id
)
268 ("vgdb-error debuglog merge-recursive-frames"
269 " gdb_output log_output mixed_output hostvisibility",
270 wcmd
, kwd_report_all
)) {
274 case 0: /* vgdb-error */
275 case 1: /* debuglog */
276 case 2: /* merge-recursive-frames */
277 wcmd
= strtok_r (NULL
, " ", &ssaveptr
);
280 endptr
= "empty"; /* to report an error below */
283 int_value
= strtol (wcmd
, &the_end
, 10);
286 if (*endptr
!= '\0') {
287 VG_(gdb_printf
) ("missing or malformed integer value\n");
288 } else if (kwdid
== 0) {
289 VG_(printf
) ("vgdb-error value changed from %d to %d\n",
290 VG_(clo_vgdb_error
), int_value
);
291 VG_(clo_vgdb_error
) = int_value
;
292 } else if (kwdid
== 1) {
293 VG_(printf
) ("debuglog value changed from %d to %d\n",
294 VG_(debugLog_getLevel
)(), int_value
);
295 VG_(debugLog_startup
) (int_value
, "gdbsrv");
296 } else if (kwdid
== 2) {
298 ("merge-recursive-frames value changed from %d to %d\n",
299 VG_(clo_merge_recursive_frames
), int_value
);
300 VG_(clo_merge_recursive_frames
) = int_value
;
305 case 3: /* gdb_output */
306 (*sink_wanted_at_return
).fd
= -2;
307 command_output_to_log
= False
;
308 VG_(gdb_printf
) ("valgrind output will go to gdb\n");
310 case 4: /* log_output */
311 (*sink_wanted_at_return
).fd
= initial_valgrind_sink
.fd
;
312 command_output_to_log
= True
;
313 VG_(gdb_printf
) ("valgrind output will go to log\n");
315 case 5: /* mixed output */
316 (*sink_wanted_at_return
).fd
= initial_valgrind_sink
.fd
;
317 command_output_to_log
= False
;
319 ("valgrind output will go to log, "
320 "interactive output will go to gdb\n");
322 case 6: /* hostvisibility */
323 wcmd
= strtok_r (NULL
, " ", &ssaveptr
);
325 switch (VG_(keyword_id
) ("yes no", wcmd
, kwd_report_all
)) {
329 hostvisibility
= True
;
332 hostvisibility
= False
;
334 default: vg_assert (0);
337 hostvisibility
= True
;
339 if (hostvisibility
) {
340 const DebugInfo
*tooldi
341 = VG_(find_DebugInfo
) (cur_ep
, (Addr
)handle_gdb_valgrind_command
);
342 /* Normally, we should always find the tooldi. In case we
343 do not, suggest a 'likely somewhat working' address: */
344 const Addr tool_text_start
346 VG_(DebugInfo_get_text_avma
) (tooldi
) : 0x58000000;
347 const NSegment
*toolseg
349 VG_(am_find_nsegment
) (VG_(DebugInfo_get_text_avma
) (tooldi
))
352 ("Enabled access to Valgrind memory/status by GDB\n"
353 "If not yet done, tell GDB which valgrind file(s) to use, "
355 "add-symbol-file %s %p\n",
356 toolseg
? VG_(am_get_filename
)(toolseg
)
357 : "<toolfile> <address> e.g.",
358 (void*)tool_text_start
);
361 ("Disabled access to Valgrind memory/status by GDB\n");
367 case 2: /* v.info */ {
369 wcmd
= strtok_r (NULL
, " ", &ssaveptr
);
370 switch (kwdid
= VG_(keyword_id
)
371 ("all_errors n_errs_found last_error gdbserver_status memory"
372 " scheduler stats open_fds exectxt location unwind",
373 wcmd
, kwd_report_all
)) {
377 case 0: // all_errors
379 Int show_error_list
= 1;
380 wcmd
= strtok_r (NULL
, " ", &ssaveptr
);
382 switch (VG_(keyword_id
) ("also_suppressed", wcmd
, kwd_report_all
)) {
388 default: vg_assert (0);
391 // A verbosity of minimum 2 is needed to show the errors.
392 VG_(show_all_errors
)(/* verbosity */ 2, /* xml */ False
, show_error_list
);
395 case 1: // n_errs_found
396 VG_(printf
) ("n_errs_found %u n_errs_shown %u (vgdb-error %d) %s\n",
397 VG_(get_n_errs_found
) (),
398 VG_(get_n_errs_shown
) (),
402 case 2: // last_error
403 VG_(show_last_error
)();
405 case 3: // gdbserver_status
406 VG_(gdbserver_status_output
)();
409 VG_(printf
) ("%'13llu bytes have already been mmap-ed ANONYMOUS.\n",
410 VG_(am_get_anonsize_total
)());
411 VG_(print_all_arena_stats
) ();
412 if (VG_(clo_profile_heap
))
413 VG_(print_arena_cc_analysis
) ();
414 wcmd
= strtok_r (NULL
, " ", &ssaveptr
);
416 switch (VG_(keyword_id
) ("aspacemgr", wcmd
, kwd_report_all
)) {
420 VG_(am_show_nsegments
) (0, "gdbserver v.info memory aspacemgr");
422 default: vg_assert (0);
428 case 5: /* scheduler */
429 VG_(show_sched_status
) (True
, // host_stacktrace
431 True
); // exited_threads
435 VG_(print_all_stats
)(False
, /* Memory stats */
436 True
/* Tool stats */);
439 case 7: /* open_fds */
440 if (VG_(clo_track_fds
))
441 VG_(show_open_fds
) ("");
444 ("Valgrind must be started with --track-fds=[yes|all]"
445 " to show open fds\n");
448 case 8: /* exectxt */
449 VG_(print_ExeContext_stats
) (True
/* with_stacktraces */);
452 case 9: { /* location */
453 /* Note: we prefer 'v.info location' and not 'v.info address' as
454 v.info address is inconsistent with the GDB (native)
455 command 'info address' which gives the address for a symbol.
456 GDB equivalent command of 'v.info location' is 'info symbol'. */
458 SizeT dummy_sz
= 0x1234;
459 if (VG_(strtok_get_address_and_size
) (&address
,
460 &dummy_sz
, &ssaveptr
)) {
461 // If tool provides location information, use that.
462 if (VG_(needs
).info_location
) {
463 VG_TDICT_CALL(tool_info_location
, cur_ep
, address
);
465 // If tool does not provide location info, use the common one.
466 // Also use the common to compare with tool when debug log is set.
467 if (!VG_(needs
).info_location
|| VG_(debugLog_getLevel
)() > 0 ) {
469 ai
.tag
= Addr_Undescribed
;
470 VG_(describe_addr
) (cur_ep
, address
, &ai
);
471 VG_(pp_addrinfo
) (address
, &ai
);
472 VG_(clear_addrinfo
) (&ai
);
478 case 10: { /* unwind */
481 if (VG_(strtok_get_address_and_size
) (&address
,
483 VG_(ppUnwindInfo
) (address
, address
+ sz
- 1);
495 wcmd
= strtok_r (NULL
, " ", &ssaveptr
);
497 int_value
= strtol (wcmd
, NULL
, 10);
498 VG_(printf
) ("gdbserver: continuing in %d ms ...\n", int_value
);
499 VG_(poll
)(NULL
, 0, int_value
);
501 VG_(printf
) ("gdbserver: continuing after wait ...\n");
505 kill_request ("monitor command request to kill this process\n");
507 case 5: { /* v.translate */
509 SizeT verbosity
= 0x20;
513 if (VG_(strtok_get_address_and_size
) (&address
, &verbosity
, &ssaveptr
)) {
514 /* we need to force the output to log for the translation trace,
515 as low level VEX tracing cannot be redirected to gdb. */
516 int saved_command_output_to_log
= command_output_to_log
;
517 int saved_fd
= VG_(log_output_sink
).fd
;
518 Bool single_stepping_on_entry
= valgrind_single_stepping();
519 int vex_verbosity
= verbosity
& 0xff;
520 VG_(log_output_sink
).fd
= initial_valgrind_sink
.fd
;
521 if ((verbosity
& 0x100) && !single_stepping_on_entry
) {
522 valgrind_set_single_stepping(True
);
523 // to force gdbserver instrumentation.
525 # if defined(VGA_arm)
526 // on arm, we need to (potentially) convert this address
527 // to the thumb form.
528 address
= thumb_pc (address
);
531 VG_(translate
) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
536 /*allow redir?*/True
);
537 if ((verbosity
& 0x100) && !single_stepping_on_entry
) {
538 valgrind_set_single_stepping(False
);
539 // reset single stepping.
541 command_output_to_log
= saved_command_output_to_log
;
542 VG_(log_output_sink
).fd
= saved_fd
;
549 wcmd
= strtok_r (NULL
, " ", &ssaveptr
);
550 switch (VG_(keyword_id
) ("expensive_sanity_check_general",
551 wcmd
, kwd_report_all
)) {
554 case 0: { /* expensive_sanity_check_general */
555 // Temporarily bump up sanity level to check e.g. the malloc arenas.
556 const Int save_clo_sanity_level
= VG_(clo_sanity_level
);
557 if (VG_(clo_sanity_level
) < 4) VG_(clo_sanity_level
) = 4;
558 VG_(sanity_check_general
) (/* force_expensive */ True
);
559 VG_(clo_sanity_level
) = save_clo_sanity_level
;
562 default: vg_assert (0);
568 for (wcmd
= VG_(strtok_r
)(NULL
, " ", &ssaveptr
);
570 wcmd
= VG_(strtok_r
)(NULL
, " ", &ssaveptr
)) {
572 VG_(process_dynamic_option
) (cloD
, wcmd
);
575 VG_(list_dynamic_options
) ();
585 /* handle_gdb_monitor_command handles the provided mon string command,
586 which can be either a "standard" valgrind monitor command
587 or a tool specific monitor command.
588 If command recognised, return 1 else return 0.
589 Note that in case of ambiguous command, 1 is returned.
592 int handle_gdb_monitor_command (char *mon
)
596 // initially, we assume that when returning, the desired sink is the
597 // one we have when entering. It can however be changed by the standard
598 // valgrind command handling.
599 OutputSink sink_wanted_at_return
= VG_(log_output_sink
);
600 // When using gdbserver, we temporarily disable xml output.
601 Bool save_clo_xml
= VG_(clo_xml
);
602 VG_(clo_xml
) = False
;
604 if (!initial_valgrind_sink_saved
) {
605 /* first time we enter here, we save the valgrind default log sink */
606 initial_valgrind_sink
= sink_wanted_at_return
;
607 initial_valgrind_sink_saved
= True
;
610 if (!command_output_to_log
)
611 VG_(log_output_sink
).fd
= -2; /* redirect to monitor_output */
613 ret
= handle_gdb_valgrind_command (mon
, &sink_wanted_at_return
);
615 /* Even if command was recognised by valgrind core, we call the
616 tool command handler : this is needed to handle help command
617 and/or to let the tool do some additional processing of a
618 valgrind standard command. Note however that if valgrind
619 recognised the command, we will always return success. */
620 if (VG_(needs
).client_requests
) {
621 /* If the tool reports an error when handling a monitor command,
622 we need to avoid calling gdbserver during this command
623 handling. So, we temporarily set VG_(clo_vgdb_error) to
624 a huge value to ensure m_errormgr.c does not call gdbserver. */
625 Int save_clo_vgdb_error
= VG_(clo_vgdb_error
);
627 VG_(clo_vgdb_error
) = 999999999;
628 arg
[0] = (UWord
) VG_USERREQ__GDB_MONITOR_COMMAND
;
629 arg
[1] = (UWord
) mon
;
630 VG_TDICT_CALL(tool_handle_client_request
, VG_(running_tid
), arg
,
632 VG_(clo_vgdb_error
) = save_clo_vgdb_error
;
635 VG_(message_flush
) ();
637 /* restore or set the desired output */
638 VG_(log_output_sink
).fd
= sink_wanted_at_return
.fd
;
639 VG_(clo_xml
) = save_clo_xml
;
648 /* Handle all of the extended 'Q' packets. */
650 void handle_set (char *arg_own_buf
, int *new_packet_len_p
)
652 if (strcmp ("QStartNoAckMode", arg_own_buf
) == 0) {
654 write_ok (arg_own_buf
);
658 if (strcmp ("QCatchSyscalls:0", arg_own_buf
) == 0) {
659 dlog (3, "catch syscall all off\n");
660 catching_syscalls
= False
;
661 write_ok (arg_own_buf
);
665 const char *q1
= "QCatchSyscalls:1";
666 if (strncmp (q1
, arg_own_buf
, strlen(q1
)) == 0) {
670 if (syscalls_to_catch
!= NULL
) {
671 free (syscalls_to_catch
);
672 syscalls_to_catch
= NULL
;
674 syscalls_to_catch_size
= 0;
675 p
= arg_own_buf
+ strlen(q1
);
678 syscalls_to_catch_size
++;
680 if (syscalls_to_catch_size
> 0) {
684 syscalls_to_catch
= malloc (syscalls_to_catch_size
* sizeof (int));
686 from
= strchr (arg_own_buf
, ';') + 1;
687 for (i
= 0; i
< syscalls_to_catch_size
; i
++) {
688 to
= strchr (from
, ';');
690 to
= arg_own_buf
+ strlen (arg_own_buf
);
691 decode_address (&sysno
, from
, to
- from
);
692 syscalls_to_catch
[i
] = (Int
)sysno
;
693 dlog(4, "catch syscall sysno %d\n", (int)sysno
);
695 if (*from
== ';') from
++;
698 dlog (4, "catch syscall all sysno\n");
699 catching_syscalls
= True
;
700 write_ok (arg_own_buf
);
704 if (strncmp ("QPassSignals:", arg_own_buf
, 13) == 0) {
707 char *end
= arg_own_buf
+ strlen(arg_own_buf
);
709 for (i
= 0; i
< TARGET_SIGNAL_LAST
; i
++)
712 from
= arg_own_buf
+ 13;
714 to
= strchr(from
, ';');
715 if (to
== NULL
) to
= end
;
716 decode_address (&sig
, from
, to
- from
);
717 pass_signals
[(int)sig
] = 1;
718 dlog(3, "pass_signal gdb_nr %d %s\n",
719 (int)sig
, target_signal_to_name(sig
));
721 if (*from
== ';') from
++;
723 write_ok (arg_own_buf
);
726 /* Otherwise we didn't know what packet it was. Say we didn't
731 Bool
VG_(client_monitor_command
) (HChar
*cmd
)
733 const Bool connected
= remote_connected();
734 const int saved_command_output_to_log
= command_output_to_log
;
738 command_output_to_log
= True
;
739 handled
= handle_gdb_monitor_command (cmd
);
741 // reset the log output unless cmd changed it.
742 if (command_output_to_log
)
743 command_output_to_log
= saved_command_output_to_log
;
746 return False
; // recognised
748 return True
; // not recognised
751 /* Handle all of the extended 'q' packets. */
753 void handle_query (char *arg_own_buf
, int *new_packet_len_p
)
755 static struct inferior_list_entry
*thread_ptr
;
757 /* thread local storage query */
758 if (strncmp ("qGetTLSAddr:", arg_own_buf
, 12) == 0) {
760 char *end
= arg_own_buf
+ strlen(arg_own_buf
);
761 unsigned long gdb_id
;
764 struct thread_info
*ti
;
766 from
= arg_own_buf
+ 12;
767 to
= strchr(from
, ',');
769 gdb_id
= strtoul (from
, NULL
, 16);
771 to
= strchr(from
, ',');
772 decode_address (&offset
, from
, to
- from
);
775 decode_address (&lm
, from
, to
- from
);
776 dlog(2, "qGetTLSAddr thread %lu offset %p lm %p\n",
777 gdb_id
, (void*)offset
, (void*)lm
);
779 ti
= gdb_id_to_thread (gdb_id
);
784 tst
= (ThreadState
*) inferior_target_data (ti
);
785 if (valgrind_get_tls_addr(tst
, offset
, lm
, &tls_addr
)) {
786 VG_(sprintf
) (arg_own_buf
, "%lx", tls_addr
);
789 // else we will report we do not support qGetTLSAddr
791 write_enn (arg_own_buf
);
796 /* qRcmd, monitor command handling. */
797 if (strncmp ("qRcmd,", arg_own_buf
, 6) == 0) {
798 char *p
= arg_own_buf
+ 6;
799 int cmdlen
= strlen(p
)/2;
802 if (unhexify (cmd
, p
, cmdlen
) != cmdlen
) {
803 write_enn (arg_own_buf
);
808 if (handle_gdb_monitor_command (cmd
)) {
809 write_ok (arg_own_buf
);
812 /* cmd not recognised */
814 ("command '%s' not recognised\n"
815 "In gdb, try 'monitor help'\n"
816 "In a shell, try 'vgdb help'\n",
818 write_ok (arg_own_buf
);
823 /* provide some valgrind specific info in return to qThreadExtraInfo. */
824 if (strncmp ("qThreadExtraInfo,", arg_own_buf
, 17) == 0) {
825 unsigned long gdb_id
;
826 struct thread_info
*ti
;
829 gdb_id
= strtoul (&arg_own_buf
[17], NULL
, 16);
830 ti
= gdb_id_to_thread (gdb_id
);
832 tst
= (ThreadState
*) inferior_target_data (ti
);
833 /* Additional info is the tid, the thread status and the thread's
835 SizeT len
= strlen(VG_(name_of_ThreadStatus
)(tst
->status
)) + 20;
836 if (tst
->thread_name
) len
+= strlen(tst
->thread_name
);
837 /* As the string will be hexified and copied into own_buf we need
838 to limit the length to avoid buffer overflow. */
839 if (len
* 2 > (PBUFSIZ
+ POVERHSIZ
))
840 len
= (PBUFSIZ
+ POVERHSIZ
) / 2;
842 if (tst
->thread_name
) {
843 VG_(snprintf
) (status
, sizeof(status
), "tid %u %s %s",
845 VG_(name_of_ThreadStatus
)(tst
->status
),
848 VG_(snprintf
) (status
, sizeof(status
), "tid %u %s",
850 VG_(name_of_ThreadStatus
)(tst
->status
));
852 hexify (arg_own_buf
, status
, strlen(status
));
855 write_enn (arg_own_buf
);
860 /* Without argument, traditional remote protocol. */
861 if (strcmp ("qAttached", arg_own_buf
) == 0) {
862 /* tell gdb to always detach, never kill the process */
863 arg_own_buf
[0] = '1';
868 /* With argument, extended-remote protocol. */
869 if (strncmp ("qAttached:", arg_own_buf
, strlen ("qAttached:")) == 0) {
870 /* We just created this process */
871 arg_own_buf
[0] = '0';
876 if (strcmp ("qSymbol::", arg_own_buf
) == 0) {
877 /* We have no symbol to read. */
878 write_ok (arg_own_buf
);
882 if (strcmp ("qC", arg_own_buf
) == 0) {
883 VG_(sprintf
) (arg_own_buf
, "QC%x",
884 thread_to_gdb_id (current_inferior
));
888 if (strcmp ("qfThreadInfo", arg_own_buf
) == 0) {
889 thread_ptr
= all_threads
.head
;
890 VG_(sprintf
) (arg_own_buf
, "m%x",
891 thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
892 thread_ptr
= thread_ptr
->next
;
896 if (strcmp ("qsThreadInfo", arg_own_buf
) == 0) {
897 if (thread_ptr
!= NULL
) {
898 VG_(sprintf
) (arg_own_buf
, "m%x",
899 thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
900 thread_ptr
= thread_ptr
->next
;
903 VG_(sprintf
) (arg_own_buf
, "l");
908 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers
)) != NULL
909 && strncmp ("qXfer:features:read:", arg_own_buf
, 20) == 0) {
911 unsigned int len
, doc_len
;
912 const char *annex
= NULL
;
913 // First, the annex is extracted from the packet received.
914 // Then, it is replaced by the corresponding file name.
917 /* Grab the annex, offset, and length. */
918 if (decode_xfer_read (arg_own_buf
+ 20, &annex
, &ofs
, &len
) < 0) {
919 strcpy (arg_own_buf
, "E00");
923 if (strcmp (annex
, "target.xml") == 0) {
924 annex
= valgrind_target_xml(VG_(clo_vgdb_shadow_registers
));
925 if (annex
!= NULL
&& VG_(clo_vgdb_shadow_registers
)) {
926 /* Ensure the shadow registers are initialized. */
927 initialize_shadow_low(True
);
930 strcpy (arg_own_buf
, "E00");
936 char doc
[VG_(strlen
)(VG_(libdir
)) + 1 + VG_(strlen
)(annex
) + 1];
937 struct vg_stat stat_doc
;
941 VG_(sprintf
)(doc
, "%s/%s", VG_(libdir
), annex
);
942 fd
= VG_(fd_open
) (doc
, VKI_O_RDONLY
, 0);
944 strcpy (arg_own_buf
, "E00");
947 if (VG_(fstat
) (fd
, &stat_doc
) != 0) {
949 strcpy (arg_own_buf
, "E00");
952 doc_len
= stat_doc
.size
;
954 if (len
> PBUFSIZ
- POVERHSIZ
)
955 len
= PBUFSIZ
- POVERHSIZ
;
958 write_enn (arg_own_buf
);
962 VG_(lseek
) (fd
, ofs
, VKI_SEEK_SET
);
963 len_read
= VG_(read
) (fd
, toread
, len
);
964 *new_packet_len_p
= write_qxfer_response (arg_own_buf
,
965 (unsigned char *)toread
,
967 ofs
+ len_read
< doc_len
);
973 if (strncmp ("qXfer:auxv:read:", arg_own_buf
, 16) == 0) {
980 /* Reject any annex; grab the offset and length. */
981 if (decode_xfer_read (arg_own_buf
+ 16, &annex
, &ofs
, &len
) < 0
982 || annex
[0] != '\0') {
983 strcpy (arg_own_buf
, "E00");
987 if (len
> PBUFSIZ
- POVERHSIZ
)
988 len
= PBUFSIZ
- POVERHSIZ
;
992 UWord
*client_auxv
= VG_(client_auxv
);
993 unsigned int client_auxv_len
= 0;
994 while (*client_auxv
!= 0) {
995 dlog(4, "auxv %llu %llx\n",
997 (ULong
)*(client_auxv
+1));
1000 client_auxv_len
+= 2 * sizeof(UWord
);
1002 client_auxv_len
+= 2 * sizeof(UWord
);
1003 dlog(4, "auxv len %u\n", client_auxv_len
);
1005 if (ofs
>= client_auxv_len
)
1008 n
= client_auxv_len
- ofs
;
1009 VG_(memcpy
) (data
, (unsigned char *) VG_(client_auxv
), n
);
1014 write_enn (arg_own_buf
);
1016 *new_packet_len_p
= write_qxfer_response (arg_own_buf
, data
, len
, 1);
1018 *new_packet_len_p
= write_qxfer_response (arg_own_buf
, data
, n
, 0);
1025 if (strncmp ("qXfer:exec-file:read:", arg_own_buf
, 21) == 0) {
1026 unsigned char *data
;
1034 /* grab the annex, offset and length. */
1035 if (decode_xfer_read (arg_own_buf
+ 21, &annex
, &ofs
, &len
) < 0) {
1036 strcpy (arg_own_buf
, "E00");
1040 /* Reject any annex with invalid/unexpected pid */
1041 if (strlen(annex
) > 0)
1042 pid
= strtoul (annex
, NULL
, 16);
1045 if ((int)pid
!= VG_(getpid
)() && pid
!= 0) {
1046 VG_(sprintf
) (arg_own_buf
,
1047 "E.Valgrind gdbserver pid is %d."
1048 " Cannot give info for pid %d",
1049 VG_(getpid
)(), (int) pid
);
1053 if (len
> PBUFSIZ
- 2)
1055 data
= malloc (len
);
1057 if (!VG_(resolve_filename
)(VG_(cl_exec_fd
), &name
)) {
1058 VG_(sprintf
) (arg_own_buf
,
1059 "E.Valgrind gdbserver could not"
1060 " resolve pid %d exec filename.",
1065 if (ofs
>= strlen(name
))
1068 n
= strlen(name
) - ofs
;
1069 VG_(memcpy
) (data
, name
, n
);
1073 write_enn (arg_own_buf
);
1075 *new_packet_len_p
= write_qxfer_response (arg_own_buf
, data
, len
, 1);
1077 *new_packet_len_p
= write_qxfer_response (arg_own_buf
, data
, n
, 0);
1084 if (strncmp ("qXfer:siginfo:read:", arg_own_buf
, 19) == 0) {
1091 /* Reject any annex; grab the offset and length. */
1092 if (decode_xfer_read (arg_own_buf
+ 19, &annex
, &ofs
, &len
) < 0
1093 || annex
[0] != '\0') {
1094 strcpy (arg_own_buf
, "E00");
1098 if (len
> PBUFSIZ
- POVERHSIZ
)
1099 len
= PBUFSIZ
- POVERHSIZ
;
1101 gdbserver_pending_signal_to_report(&info
);
1103 if (ofs
>= sizeof(info
))
1106 n
= sizeof(info
) - ofs
;
1109 write_enn (arg_own_buf
);
1111 *new_packet_len_p
= write_qxfer_response (arg_own_buf
,
1112 (unsigned char *)&info
,
1115 *new_packet_len_p
= write_qxfer_response (arg_own_buf
,
1116 (unsigned char *)&info
,
1122 /* Protocol features query. Keep this in sync with coregind/vgdb.c. */
1123 if (strncmp ("qSupported", arg_own_buf
, 10) == 0
1124 && (arg_own_buf
[10] == ':' || arg_own_buf
[10] == '\0')) {
1125 VG_(sprintf
) (arg_own_buf
, "PacketSize=%x", (UInt
)PBUFSIZ
- 1);
1126 /* Note: max packet size including frame and checksum, but without
1127 trailing null byte, which is not sent/received. */
1129 strcat (arg_own_buf
, ";QStartNoAckMode+");
1130 strcat (arg_own_buf
, ";QPassSignals+");
1131 strcat (arg_own_buf
, ";QCatchSyscalls+");
1132 if (VG_(client_auxv
))
1133 strcat (arg_own_buf
, ";qXfer:auxv:read+");
1135 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers
)) != NULL
) {
1136 strcat (arg_own_buf
, ";qXfer:features:read+");
1137 /* if a new gdb connects to us, we have to reset the register
1138 set to the normal register sets to allow this new gdb to
1139 decide to use or not the shadow registers.
1141 Note that the reset is only done for gdb that are sending
1142 qSupported packets. If a user first connected with a recent
1143 gdb using shadow registers and then with a very old gdb
1144 that does not use qSupported packet, then the old gdb will
1145 not properly connect. */
1146 initialize_shadow_low(False
);
1148 strcat (arg_own_buf
, ";qXfer:exec-file:read+");
1149 strcat (arg_own_buf
, ";qXfer:siginfo:read+");
1153 /* Otherwise we didn't know what packet it was. Say we didn't
1158 /* Handle all of the extended 'v' packets. */
1160 void handle_v_requests (char *arg_own_buf
, char *status
, int *zignal
)
1162 /* vcont packet code from gdb 6.6 removed */
1164 /* Otherwise we didn't know what packet it was. Say we didn't
1171 void myresume (int step
, int sig
)
1173 struct thread_resume resume_info
[2];
1177 resume_info
[0].step
= step
;
1178 resume_info
[0].sig
= sig
;
1181 resume_info
[n
].step
= 0;
1182 resume_info
[n
].sig
= 0;
1184 resume_reply_packet_needed
= True
;
1185 valgrind_resume (resume_info
);
1188 /* server_main global variables */
1189 static char *own_buf
;
1190 static unsigned char *mem_buf
;
1192 void gdbserver_init (void)
1194 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version
);
1196 valgrind_initialize_target ();
1197 // After a fork, gdbserver_init can be called again.
1198 // We do not have to re-malloc the buffers in such a case.
1199 if (own_buf
== NULL
)
1200 own_buf
= malloc (PBUFSIZ
+POVERHSIZ
);
1201 if (mem_buf
== NULL
)
1202 mem_buf
= malloc (PBUFSIZ
+POVERHSIZ
);
1203 // Note: normally, we should only malloc PBUFSIZ. However,
1204 // GDB has a bug, and in some cases, sends e.g. 'm' packets
1205 // asking for slightly more than the PacketSize given at
1206 // connection initialisation. So, we bypass the GDB bug
1207 // by allocating slightly more.
1210 void gdbserver_terminate (void)
1212 /* last call to gdbserver is cleanup call */
1213 if (VG_MINIMAL_SETJMP(toplevel
)) {
1214 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
1220 void server_main (void)
1230 zignal
= valgrind_wait (&status
);
1231 if (VG_MINIMAL_SETJMP(toplevel
)) {
1232 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
1237 int new_packet_len
= -1;
1239 if (resume_reply_packet_needed
) {
1240 /* Send the resume reply to reply to last GDB resume
1242 resume_reply_packet_needed
= False
;
1243 prepare_resume_reply (own_buf
, status
, zignal
);
1247 /* If our status is terminal (exit or fatal signal) get out
1248 as quickly as we can. We won't be able to handle any request
1250 if (status
== 'W' || status
== 'X') {
1254 packet_len
= getpkt (own_buf
);
1255 if (packet_len
<= 0)
1262 handle_set (own_buf
, &new_packet_len
);
1265 handle_query (own_buf
, &new_packet_len
);
1268 /* set/unset debugging is done through valgrind debug level. */
1272 reset_valgrind_sink("gdb detaching from process");
1274 /* When detaching or kill the process, gdb expects to get
1275 an packet OK back. Any other output will make gdb
1276 believes detach did not work. */
1279 remote_finish (reset_after_error
);
1280 remote_open (VG_(clo_vgdb_prefix
));
1282 resume_reply_packet_needed
= False
;
1285 /* We can not use the extended protocol with valgrind,
1286 because we can not restart the running
1287 program. So return unrecognized. */
1291 prepare_resume_reply (own_buf
, status
, zignal
);
1294 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's') {
1295 unsigned long gdb_id
, thread_id
;
1297 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
1298 thread_id
= gdb_id_to_thread_id (gdb_id
);
1299 if (thread_id
== 0) {
1300 write_enn (own_buf
);
1304 if (own_buf
[1] == 'g') {
1305 general_thread
= thread_id
;
1306 set_desired_inferior (1);
1307 } else if (own_buf
[1] == 'c') {
1308 cont_thread
= thread_id
;
1309 } else if (own_buf
[1] == 's') {
1310 step_thread
= thread_id
;
1315 /* Silently ignore it so that gdb can extend the protocol
1316 without compatibility headaches. */
1321 set_desired_inferior (1);
1322 registers_to_string (own_buf
);
1325 set_desired_inferior (1);
1326 registers_from_string (&own_buf
[1]);
1333 regno
= strtol(&own_buf
[1], NULL
, 16);
1334 regbytes
= strchr(&own_buf
[0], '=') + 1;
1335 set_desired_inferior (1);
1336 tst
= (ThreadState
*) inferior_target_data (current_inferior
);
1337 /* Only accept changing registers in "runnable state3.
1338 In fact, it would be ok to change most of the registers
1339 except a few "sensitive" registers such as the PC, SP, BP.
1340 We assume we do not need to very specific here, and that we
1341 can just refuse all of these. */
1342 if (tst
->status
== VgTs_Runnable
|| tst
->status
== VgTs_Yielding
) {
1343 supply_register_from_string (regno
, regbytes
);
1346 /* at least from gdb 6.6 onwards, an E. error
1347 reply is shown to the user. So, we do an error
1348 msg which both is accepted by gdb as an error msg
1349 and is readable by the user. */
1353 "ERROR changing register %s regno %d\n"
1354 "gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
1355 "set pc, calling from gdb a function in the debugged process, ...)\n"
1356 "can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
1357 "Thread status is %s\n",
1358 find_register_by_number (regno
)->name
, regno
,
1359 VG_(name_of_ThreadStatus
)(tst
->status
));
1360 if (VG_(clo_verbosity
) > 1)
1361 VG_(umsg
) ("%s\n", own_buf
);
1366 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
1367 if (valgrind_read_memory (mem_addr
, mem_buf
, len
) == 0)
1368 convert_int_to_ascii (mem_buf
, own_buf
, len
);
1370 write_enn (own_buf
);
1373 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
1374 if (valgrind_write_memory (mem_addr
, mem_buf
, len
) == 0)
1377 write_enn (own_buf
);
1380 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
1381 &mem_addr
, &len
, mem_buf
) < 0
1382 || valgrind_write_memory (mem_addr
, mem_buf
, len
) != 0)
1383 write_enn (own_buf
);
1388 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1389 if (target_signal_to_host_p (sig
))
1390 zignal
= target_signal_to_host (sig
);
1393 set_desired_inferior (0);
1394 myresume (0, zignal
);
1395 return; // return control to valgrind
1397 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1398 if (target_signal_to_host_p (sig
))
1399 zignal
= target_signal_to_host (sig
);
1402 set_desired_inferior (0);
1403 myresume (1, zignal
);
1404 return; // return control to valgrind
1406 set_desired_inferior (0);
1408 return; // return control to valgrind
1410 set_desired_inferior (0);
1412 return; // return control to valgrind
1416 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1417 int zlen
= strtol (lenptr
+ 1, &dataptr
, 16);
1418 char type
= own_buf
[1];
1420 if (type
< '0' || type
> '4') {
1421 /* Watchpoint command type unrecognized. */
1426 res
= valgrind_insert_watchpoint (type
, addr
, zlen
);
1433 write_enn (own_buf
);
1440 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1441 int zlen
= strtol (lenptr
+ 1, &dataptr
, 16);
1442 char type
= own_buf
[1];
1444 if (type
< '0' || type
> '4') {
1445 /* Watchpoint command type unrecognized. */
1450 res
= valgrind_remove_watchpoint (type
, addr
, zlen
);
1457 write_enn (own_buf
);
1462 kill_request("Gdb request to kill this process\n");
1465 unsigned long gdb_id
, thread_id
;
1467 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
1468 thread_id
= gdb_id_to_thread_id (gdb_id
);
1469 if (thread_id
== 0) {
1470 write_enn (own_buf
);
1474 if (valgrind_thread_alive (thread_id
))
1477 write_enn (own_buf
);
1481 /* Restarting the inferior is only supported in the
1483 => It is a request we don't understand. Respond with an
1484 empty packet so that gdb knows that we don't support this
1489 /* Extended (long) request. */
1490 handle_v_requests (own_buf
, &status
, &zignal
);
1493 /* It is a request we don't understand. Respond with an
1494 empty packet so that gdb knows that we don't support this
1500 if (new_packet_len
!= -1)
1501 putpkt_binary (own_buf
, new_packet_len
);
1506 VG_(umsg
) ("\nChild exited with status %d\n", zignal
);
1508 VG_(umsg
) ("\nChild terminated with signal = 0x%x (%s)\n",
1509 (UInt
)target_signal_to_host (zignal
),
1510 target_signal_to_name (zignal
));
1511 if (status
== 'W' || status
== 'X') {
1512 VG_(umsg
) ("Process exiting\n");
1517 /* We come here when getpkt fails => close the connection,
1518 and re-open. Then return control to valgrind.
1519 We return the control to valgrind as we assume that
1520 the connection was closed due to vgdb having finished
1521 to execute a command. */
1522 if (VG_(clo_verbosity
) > 1)
1523 VG_(umsg
) ("Remote side has terminated connection. "
1524 "GDBserver will reopen the connection.\n");
1525 remote_finish (reset_after_error
);
1526 remote_open (VG_(clo_vgdb_prefix
));
1528 resume_reply_packet_needed
= False
;