blob: ca0d7bd5485e8a22629d344f5739b6ae1a747bca [file] [log] [blame]
sewardj3b290482011-05-06 21:02:55 +00001/* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2006, 2011
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7 It has been modified to integrate it in valgrind
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
23
24#include "server.h"
25#include "regdef.h"
26#include "pub_core_options.h"
27#include "pub_core_translate.h"
28#include "pub_core_mallocfree.h"
sewardj2a312392011-06-26 09:26:48 +000029#include "pub_core_initimg.h"
philippef3a6e932013-01-10 20:42:51 +000030#include "pub_core_execontext.h"
floriandbb35842012-10-27 18:39:11 +000031#include "pub_core_syswrap.h" // VG_(show_open_fds)
philippe66e4c732013-01-26 16:45:01 +000032#include "pub_core_scheduler.h"
philippe8587b542013-12-15 20:24:43 +000033#include "pub_core_transtab.h"
philippe07c08522014-05-14 20:39:27 +000034#include "pub_core_debuginfo.h"
35#include "pub_core_addrinfo.h"
florian7463e492015-02-26 17:48:07 +000036#include "pub_core_aspacemgr.h"
sewardj3b290482011-05-06 21:02:55 +000037
38unsigned long cont_thread;
39unsigned long general_thread;
40unsigned long step_thread;
41unsigned long thread_from_wait;
42unsigned long old_thread_from_wait;
43
philippe886fde32012-03-29 21:56:47 +000044int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */
bart2dafc542011-05-18 16:08:28 +000045
sewardj3b290482011-05-06 21:02:55 +000046/* for a gdbserver integrated in valgrind, resuming the process consists
47 in returning the control to valgrind.
philippe349a3912012-05-23 21:50:36 +000048 The guess process resumes its execution.
sewardj3b290482011-05-06 21:02:55 +000049 Then at the next error or break or ..., valgrind calls gdbserver again.
philippe349a3912012-05-23 21:50:36 +000050 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
sewardj3b290482011-05-06 21:02:55 +000053 must send a resume packet to gdb. */
philippe349a3912012-05-23 21:50:36 +000054static Bool resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +000055
56VG_MINIMAL_JMP_BUF(toplevel);
57
58/* Decode a qXfer read request. Return 0 if everything looks OK,
59 or -1 otherwise. */
60
61static
florian6bd9dc12012-11-23 16:17:43 +000062int decode_xfer_read (char *buf, const char **annex, CORE_ADDR *ofs, unsigned int *len)
sewardj3b290482011-05-06 21:02:55 +000063{
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. */
83static
84int 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
97static Bool initial_valgrind_sink_saved = False;
98/* True <=> valgrind log sink saved in initial_valgrind_sink */
99static OutputSink initial_valgrind_sink;
100
101static Bool command_output_to_log = False;
102/* True <=> command output goes to log instead of gdb */
103
florian6bd9dc12012-11-23 16:17:43 +0000104void reset_valgrind_sink(const char *info)
sewardj3b290482011-05-06 21:02:55 +0000105{
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",
Elliott Hughesed398002017-06-21 14:41:24 -0700110 (info == NULL ? "" : info));
sewardj3b290482011-05-06 21:02:55 +0000111 }
112}
113
philippe46207652013-01-20 17:11:58 +0000114void 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
sewardj3b290482011-05-06 21:02:55 +0000121static
florian6bd9dc12012-11-23 16:17:43 +0000122void kill_request (const char *msg)
sewardj3b290482011-05-06 21:02:55 +0000123{
124 VG_(umsg) ("%s", msg);
sewardj3b290482011-05-06 21:02:55 +0000125 VG_(exit) (0);
126}
127
philippe02ea4132013-09-04 21:42:43 +0000128// 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.
131static
132const 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
philippe4f6f3362014-04-19 00:25:54 +0000153void 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)();
philippe92527132015-05-10 22:40:38 +0000160 VG_(message)
161 (Vg_DebugMsg,
162 "------ %'13llu bytes have already been mmap-ed ANONYMOUS.\n",
163 VG_(am_get_anonsize_total)());
philippe4f6f3362014-04-19 00:25:54 +0000164 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
sewardj3b290482011-05-06 21:02:55 +0000180/* 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
sewardj30b3eca2011-06-28 08:20:39 +0000185 'v.set *_output' is handled.
sewardj3b290482011-05-06 21:02:55 +0000186*/
187static
philippe02ea4132013-09-04 21:42:43 +0000188int handle_gdb_valgrind_command (char *mon, OutputSink *sink_wanted_at_return)
sewardj3b290482011-05-06 21:02:55 +0000189{
190 UWord ret = 0;
191 char s[strlen(mon)+1]; /* copy for strtok_r */
philippe02ea4132013-09-04 21:42:43 +0000192 char *wcmd;
193 HChar *ssaveptr;
194 const char *endptr;
sewardj3b290482011-05-06 21:02:55 +0000195 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
sewardj30b3eca2011-06-28 08:20:39 +0000203 starts with the same 3 first letters as an already existing
sewardj3b290482011-05-06 21:02:55 +0000204 command. This ensures a shorter abbreviation for the user. */
philippe6ec8d632013-01-23 22:10:28 +0000205 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate"
206 " v.do",
sewardj3b290482011-05-06 21:02:55 +0000207 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;
floriane2800c92014-09-15 20:57:45 +0000223 default: vg_assert (0);
sewardj3b290482011-05-06 21:02:55 +0000224 }
225 }
226
227 VG_(gdb_printf) (
228"general valgrind monitor commands:\n"
philippec3360382012-10-21 14:37:14 +0000229" help [debug] : monitor command help. With debug: + debugging commands\n"
sewardj30b3eca2011-06-28 08:20:39 +0000230" 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"
philippe07c08522014-05-14 20:39:27 +0000233" v.info location <addr> : show information about location <addr>\n"
philippe02ea4132013-09-04 21:42:43 +0000234" v.info n_errs_found [msg] : show the nr of errors found so far and the given msg\n"
philippec3360382012-10-21 14:37:14 +0000235" v.info open_fds : show open file descriptors (only if --track-fds=yes)\n"
sewardj30b3eca2011-06-28 08:20:39 +0000236" 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"
philippe46207652013-01-20 17:11:58 +0000240" v.set merge-recursive-frames <num> : merge recursive calls in max <num> frames\n"
sewardj30b3eca2011-06-28 08:20:39 +0000241" v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
sewardj3b290482011-05-06 21:02:55 +0000242 if (int_value) { VG_(gdb_printf) (
243"debugging valgrind internals monitor commands:\n"
philippe6ec8d632013-01-23 22:10:28 +0000244" v.do expensive_sanity_check_general : do an expensive sanity check now\n"
sewardj30b3eca2011-06-28 08:20:39 +0000245" v.info gdbserver_status : show gdbserver status\n"
philippe93a6a8d2012-04-27 22:59:43 +0000246" v.info memory [aspacemgr] : show valgrind heap memory stats\n"
florianad4e9792015-07-05 21:53:33 +0000247" (with aspacemgr arg, also shows valgrind segments on log output)\n"
philippef3a6e932013-01-10 20:42:51 +0000248" v.info exectxt : show stacktraces and stats of all execontexts\n"
sewardjd6e13d82011-10-22 20:23:30 +0000249" v.info scheduler : show valgrind thread state and stacktrace\n"
philippe8587b542013-12-15 20:24:43 +0000250" v.info stats : show various valgrind and tool stats\n"
philippe97bfa192015-06-02 22:09:42 +0000251" v.info unwind <addr> [<len>] : show unwind debug info for <addr> .. <addr+len>\n"
sewardj30b3eca2011-06-28 08:20:39 +0000252" v.set debuglog <level> : set valgrind debug log level to <level>\n"
philippe180a7502014-04-20 13:41:10 +0000253" v.set hostvisibility [yes*|no] : (en/dis)ables access by gdb/gdbserver to\n"
254" Valgrind internal host status/memory\n"
sewardj30b3eca2011-06-28 08:20:39 +0000255" v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
sewardj3b290482011-05-06 21:02:55 +0000256" (default traceflags 0b00100000 : show after instrumentation)\n"
257" An additional flag 0b100000000 allows to show gdbserver instrumentation\n");
258 }
259 break;
sewardj30b3eca2011-06-28 08:20:39 +0000260 case 1: /* v.set */
sewardj3b290482011-05-06 21:02:55 +0000261 ret = 1;
262 wcmd = strtok_r (NULL, " ", &ssaveptr);
263 switch (kwdid = VG_(keyword_id)
philippe46207652013-01-20 17:11:58 +0000264 ("vgdb-error debuglog merge-recursive-frames"
philippe64ba5742014-06-19 20:33:27 +0000265 " gdb_output log_output mixed_output hostvisibility",
sewardj3b290482011-05-06 21:02:55 +0000266 wcmd, kwd_report_all)) {
267 case -2:
268 case -1:
269 break;
270 case 0: /* vgdb-error */
271 case 1: /* debuglog */
philippe46207652013-01-20 17:11:58 +0000272 case 2: /* merge-recursive-frames */
sewardj3b290482011-05-06 21:02:55 +0000273 wcmd = strtok_r (NULL, " ", &ssaveptr);
274 if (wcmd == NULL) {
275 int_value = 0;
276 endptr = "empty"; /* to report an error below */
277 } else {
florian6bd9dc12012-11-23 16:17:43 +0000278 HChar *the_end;
279 int_value = strtol (wcmd, &the_end, 10);
280 endptr = the_end;
sewardj3b290482011-05-06 21:02:55 +0000281 }
282 if (*endptr != '\0') {
283 VG_(gdb_printf) ("missing or malformed integer value\n");
284 } else if (kwdid == 0) {
philippe02ea4132013-09-04 21:42:43 +0000285 VG_(printf) ("vgdb-error value changed from %d to %d\n",
sewardj3b290482011-05-06 21:02:55 +0000286 VG_(dyn_vgdb_error), int_value);
287 VG_(dyn_vgdb_error) = int_value;
288 } else if (kwdid == 1) {
philippe02ea4132013-09-04 21:42:43 +0000289 VG_(printf) ("debuglog value changed from %d to %d\n",
sewardj3b290482011-05-06 21:02:55 +0000290 VG_(debugLog_getLevel)(), int_value);
291 VG_(debugLog_startup) (int_value, "gdbsrv");
philippe46207652013-01-20 17:11:58 +0000292 } else if (kwdid == 2) {
philippe02ea4132013-09-04 21:42:43 +0000293 VG_(printf)
philippe46207652013-01-20 17:11:58 +0000294 ("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;
sewardj3b290482011-05-06 21:02:55 +0000297 } else {
298 vg_assert (0);
299 }
300 break;
philippe46207652013-01-20 17:11:58 +0000301 case 3: /* gdb_output */
sewardj3b290482011-05-06 21:02:55 +0000302 (*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;
philippe46207652013-01-20 17:11:58 +0000306 case 4: /* log_output */
sewardj3b290482011-05-06 21:02:55 +0000307 (*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;
philippe46207652013-01-20 17:11:58 +0000311 case 5: /* mixed output */
sewardj3b290482011-05-06 21:02:55 +0000312 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
313 command_output_to_log = False;
314 VG_(gdb_printf)
philipped4c37442015-03-21 16:13:08 +0000315 ("valgrind output will go to log, "
316 "interactive output will go to gdb\n");
sewardj3b290482011-05-06 21:02:55 +0000317 break;
philippe180a7502014-04-20 13:41:10 +0000318 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;
floriane2800c92014-09-15 20:57:45 +0000330 default: vg_assert (0);
philippe180a7502014-04-20 13:41:10 +0000331 }
332 } else {
333 hostvisibility = True;
334 }
philippe3a73b392014-06-09 15:47:46 +0000335 if (hostvisibility) {
philippe72a10e52014-07-31 21:15:42 +0000336 const DebugInfo *tooldi
337 = VG_(find_DebugInfo) ((Addr)handle_gdb_valgrind_command);
philippe45da6322015-03-28 12:23:07 +0000338 /* 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 ?
Elliott Hughesed398002017-06-21 14:41:24 -0700342 VG_(DebugInfo_get_text_avma) (tooldi) : 0x58000000;
philippe45da6322015-03-28 12:23:07 +0000343 const NSegment *toolseg
philippe72a10e52014-07-31 21:15:42 +0000344 = tooldi ?
345 VG_(am_find_nsegment) (VG_(DebugInfo_get_text_avma) (tooldi))
346 : NULL;
philippe180a7502014-04-20 13:41:10 +0000347 VG_(gdb_printf)
348 ("Enabled access to Valgrind memory/status by GDB\n"
philippe3a73b392014-06-09 15:47:46 +0000349 "If not yet done, tell GDB which valgrind file(s) to use, "
350 "typically:\n"
philippe72a10e52014-07-31 21:15:42 +0000351 "add-symbol-file %s %p\n",
352 toolseg ? VG_(am_get_filename)(toolseg)
353 : "<toolfile> <address> e.g.",
philippe45da6322015-03-28 12:23:07 +0000354 (void*)tool_text_start);
philippe3a73b392014-06-09 15:47:46 +0000355 } else
philippe180a7502014-04-20 13:41:10 +0000356 VG_(gdb_printf)
357 ("Disabled access to Valgrind memory/status by GDB\n");
358 break;
sewardj3b290482011-05-06 21:02:55 +0000359 default:
360 vg_assert (0);
361 }
362 break;
sewardj30b3eca2011-06-28 08:20:39 +0000363 case 2: /* v.info */ {
sewardj3b290482011-05-06 21:02:55 +0000364 ret = 1;
365 wcmd = strtok_r (NULL, " ", &ssaveptr);
366 switch (kwdid = VG_(keyword_id)
sewardjd6e13d82011-10-22 20:23:30 +0000367 ("all_errors n_errs_found last_error gdbserver_status memory"
philippe97bfa192015-06-02 22:09:42 +0000368 " scheduler stats open_fds exectxt location unwind",
sewardj3b290482011-05-06 21:02:55 +0000369 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
florian654ea862015-08-06 12:11:33 +0000378 VG_(printf) ("n_errs_found %u n_errs_shown %u (vgdb-error %d) %s\n",
philippe02ea4132013-09-04 21:42:43 +0000379 VG_(get_n_errs_found) (),
380 VG_(get_n_errs_shown) (),
381 VG_(dyn_vgdb_error),
382 wordn (mon, 3));
sewardj3b290482011-05-06 21:02:55 +0000383 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 */
philippe92527132015-05-10 22:40:38 +0000391 VG_(printf) ("%'13llu bytes have already been mmap-ed ANONYMOUS.\n",
philippe8587b542013-12-15 20:24:43 +0000392 VG_(am_get_anonsize_total)());
sewardj3b290482011-05-06 21:02:55 +0000393 VG_(print_all_arena_stats) ();
394 if (VG_(clo_profile_heap))
395 VG_(print_arena_cc_analysis) ();
philippe93a6a8d2012-04-27 22:59:43 +0000396 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;
floriane2800c92014-09-15 20:57:45 +0000404 default: vg_assert (0);
philippe93a6a8d2012-04-27 22:59:43 +0000405 }
406 }
407
sewardj3b290482011-05-06 21:02:55 +0000408 ret = 1;
409 break;
sewardjd6e13d82011-10-22 20:23:30 +0000410 case 5: /* scheduler */
philippe4f6f3362014-04-19 00:25:54 +0000411 VG_(show_sched_status) (True, // host_stacktrace
philippe38a74d22014-08-29 22:53:19 +0000412 True, // stack_usage
philippe4f6f3362014-04-19 00:25:54 +0000413 True); // exited_threads
sewardjd6e13d82011-10-22 20:23:30 +0000414 ret = 1;
415 break;
philippe8587b542013-12-15 20:24:43 +0000416 case 6: /* stats */
philippe4f6f3362014-04-19 00:25:54 +0000417 VG_(print_all_stats)(False, /* Memory stats */
418 True /* Tool stats */);
philippe8587b542013-12-15 20:24:43 +0000419 ret = 1;
420 break;
421 case 7: /* open_fds */
philippec3360382012-10-21 14:37:14 +0000422 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;
philippe8587b542013-12-15 20:24:43 +0000430 case 8: /* exectxt */
philippef3a6e932013-01-10 20:42:51 +0000431 VG_(print_ExeContext_stats) (True /* with_stacktraces */);
432 ret = 1;
433 break;
philippe07c08522014-05-14 20:39:27 +0000434 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;
philipped4c37442015-03-21 16:13:08 +0000441 if (VG_(strtok_get_address_and_size) (&address,
442 &dummy_sz, &ssaveptr)) {
philippe07c08522014-05-14 20:39:27 +0000443 // If tool provides location information, use that.
444 if (VG_(needs).info_location) {
445 VG_TDICT_CALL(tool_info_location, address);
446 }
philipped4c37442015-03-21 16:13:08 +0000447 // If tool does not provide location info, use the common one.
philippe07c08522014-05-14 20:39:27 +0000448 // 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 }
philippe97bfa192015-06-02 22:09:42 +0000460 case 10: { /* unwind */
461 Addr address;
philippec403a552015-06-02 22:17:51 +0000462 SizeT sz = 1;
philippe97bfa192015-06-02 22:09:42 +0000463 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
sewardj3b290482011-05-06 21:02:55 +0000471 default:
472 vg_assert(0);
473 }
474 break;
475 }
sewardj30b3eca2011-06-28 08:20:39 +0000476 case 3: /* v.wait */
sewardj3b290482011-05-06 21:02:55 +0000477 wcmd = strtok_r (NULL, " ", &ssaveptr);
478 if (wcmd != NULL) {
florian6bd9dc12012-11-23 16:17:43 +0000479 int_value = strtol (wcmd, NULL, 10);
philippe02ea4132013-09-04 21:42:43 +0000480 VG_(printf) ("gdbserver: continuing in %d ms ...\n", int_value);
sewardj3b290482011-05-06 21:02:55 +0000481 VG_(poll)(NULL, 0, int_value);
482 }
philippe02ea4132013-09-04 21:42:43 +0000483 VG_(printf) ("gdbserver: continuing after wait ...\n");
sewardj3b290482011-05-06 21:02:55 +0000484 ret = 1;
485 break;
sewardj30b3eca2011-06-28 08:20:39 +0000486 case 4: /* v.kill */
sewardj3b290482011-05-06 21:02:55 +0000487 kill_request ("monitor command request to kill this process\n");
488 break;
sewardj30b3eca2011-06-28 08:20:39 +0000489 case 5: { /* v.translate */
sewardj3b290482011-05-06 21:02:55 +0000490 Addr address;
491 SizeT verbosity = 0x20;
492
493 ret = 1;
494
philippe07c08522014-05-14 20:39:27 +0000495 if (VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr)) {
sewardj3b290482011-05-06 21:02:55 +0000496 /* 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 }
sewardj99d61342011-05-17 16:35:11 +0000507# 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
sewardj3b290482011-05-06 21:02:55 +0000513 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
philippe6ec8d632013-01-23 22:10:28 +0000529 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 }
floriane2800c92014-09-15 20:57:45 +0000544 default: vg_assert (0);
philippe6ec8d632013-01-23 22:10:28 +0000545 }
546 break;
547
sewardj3b290482011-05-06 21:02:55 +0000548 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*/
560static
philippe02ea4132013-09-04 21:42:43 +0000561int handle_gdb_monitor_command (char *mon)
sewardj3b290482011-05-06 21:02:55 +0000562{
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);
philipped4c37442015-03-21 16:13:08 +0000569 // When using gdbserver, we temporarily disable xml output.
570 Bool save_clo_xml = VG_(clo_xml);
571 VG_(clo_xml) = False;
sewardj3b290482011-05-06 21:02:55 +0000572
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
philippe02ea4132013-09-04 21:42:43 +0000604 VG_(message_flush) ();
605
sewardj3b290482011-05-06 21:02:55 +0000606 /* restore or set the desired output */
607 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
philipped4c37442015-03-21 16:13:08 +0000608 VG_(clo_xml) = save_clo_xml;
609
sewardj3b290482011-05-06 21:02:55 +0000610 if (ret | tool_ret)
611 return 1;
612 else
613 return 0;
614}
615
616
617/* Handle all of the extended 'Q' packets. */
618static
619void 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
Elliott Hughesa0664b92017-04-18 17:46:52 -0700627 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
sewardj3b290482011-05-06 21:02:55 +0000673 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;
philippe75d8a4e2015-03-26 21:32:03 +0000687 dlog(3, "pass_signal gdb_nr %d %s\n",
philippe886fde32012-03-29 21:56:47 +0000688 (int)sig, target_signal_to_name(sig));
sewardj3b290482011-05-06 21:02:55 +0000689 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
philippe02ea4132013-09-04 21:42:43 +0000700Bool VG_(client_monitor_command) (HChar *cmd)
philippe46207652013-01-20 17:11:58 +0000701{
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
sewardj3b290482011-05-06 21:02:55 +0000720/* Handle all of the extended 'q' packets. */
721static
722void handle_query (char *arg_own_buf, int *new_packet_len_p)
723{
724 static struct inferior_list_entry *thread_ptr;
725
philippe1670b052014-08-15 10:27:52 +0000726 /* 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
sewardj3b290482011-05-06 21:02:55 +0000765 /* 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)) {
sewardj3b290482011-05-06 21:02:55 +0000778 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;
sewardj3b290482011-05-06 21:02:55 +0000797
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);
florian49789512013-09-16 17:08:50 +0000802 /* Additional info is the tid, the thread status and the thread's
803 name, if any. */
florian7b7d5942014-12-19 20:29:22 +0000804 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];
florian49789512013-09-16 17:08:50 +0000811 if (tst->thread_name) {
florian654ea862015-08-06 12:11:33 +0000812 VG_(snprintf) (status, sizeof(status), "tid %u %s %s",
florian49789512013-09-16 17:08:50 +0000813 tst->tid,
814 VG_(name_of_ThreadStatus)(tst->status),
815 tst->thread_name);
816 } else {
florian654ea862015-08-06 12:11:33 +0000817 VG_(snprintf) (status, sizeof(status), "tid %u %s",
florian49789512013-09-16 17:08:50 +0000818 tst->tid,
819 VG_(name_of_ThreadStatus)(tst->status));
820 }
sewardj3b290482011-05-06 21:02:55 +0000821 hexify (arg_own_buf, status, strlen(status));
822 return;
823 } else {
824 write_enn (arg_own_buf);
825 return;
826 }
827 }
philippe1670b052014-08-15 10:27:52 +0000828
sewardj3b290482011-05-06 21:02:55 +0000829 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
Elliott Hughesa0664b92017-04-18 17:46:52 -0700842 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
sewardj3b290482011-05-06 21:02:55 +0000848 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
philippe419d5f22012-05-24 21:33:17 +0000868 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL
sewardj3b290482011-05-06 21:02:55 +0000869 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
870 CORE_ADDR ofs;
871 unsigned int len, doc_len;
florian6bd9dc12012-11-23 16:17:43 +0000872 const char *annex = NULL;
sewardj3b290482011-05-06 21:02:55 +0000873 // 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) {
philippe419d5f22012-05-24 21:33:17 +0000884 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);
sewardj3b290482011-05-06 21:02:55 +0000888 }
sewardj3b290482011-05-06 21:02:55 +0000889 if (annex == NULL) {
890 strcpy (arg_own_buf, "E00");
891 return;
892 }
893 }
894
895 {
philippe75a5f782012-02-24 11:25:58 +0000896 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1];
sewardj3b290482011-05-06 21:02:55 +0000897 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);
philippeb3014692015-05-17 13:38:25 +0000924 *new_packet_len_p = write_qxfer_response (arg_own_buf,
925 (unsigned char *)toread,
926 len_read,
927 ofs + len_read < doc_len);
sewardj3b290482011-05-06 21:02:55 +0000928 VG_(close) (fd);
929 return;
930 }
931 }
932
sewardj2a312392011-06-26 09:26:48 +0000933 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;
florian6bd9dc12012-11-23 16:17:43 +0000938 const char *annex;
sewardj2a312392011-06-26 09:26:48 +0000939
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
philippeb3014692015-05-17 13:38:25 +0000947 if (len > PBUFSIZ - POVERHSIZ)
948 len = PBUFSIZ - POVERHSIZ;
sewardj2a312392011-06-26 09:26:48 +0000949 data = malloc (len);
950
951 {
952 UWord *client_auxv = VG_(client_auxv);
953 unsigned int client_auxv_len = 0;
954 while (*client_auxv != 0) {
florian654ea862015-08-06 12:11:33 +0000955 dlog(4, "auxv %llu %llx\n",
sewardj2a312392011-06-26 09:26:48 +0000956 (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);
florian654ea862015-08-06 12:11:33 +0000963 dlog(4, "auxv len %u\n", client_auxv_len);
sewardj2a312392011-06-26 09:26:48 +0000964
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
philippe5e94f052015-05-14 19:56:47 +0000985 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
philippeb3014692015-05-17 13:38:25 +0000994 /* grab the annex, offset and length. */
philippe5e94f052015-05-14 19:56:47 +0000995 if (decode_xfer_read (arg_own_buf + 21, &annex, &ofs, &len) < 0) {
996 strcpy (arg_own_buf, "E00");
997 return;
998 }
999
philippeb3014692015-05-17 13:38:25 +00001000 /* Reject any annex with invalid/unexpected pid */
philippe5e94f052015-05-14 19:56:47 +00001001 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 }
sewardj2a312392011-06-26 09:26:48 +00001043
philippeb3014692015-05-17 13:38:25 +00001044 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
sewardj3b290482011-05-06 21:02:55 +00001082 /* Protocol features query. */
1083 if (strncmp ("qSupported", arg_own_buf, 10) == 0
1084 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
florian654ea862015-08-06 12:11:33 +00001085 VG_(sprintf) (arg_own_buf, "PacketSize=%x", (UInt)PBUFSIZ - 1);
sewardj3b290482011-05-06 21:02:55 +00001086 /* Note: max packet size including frame and checksum, but without
1087 trailing null byte, which is not sent/received. */
philippe5e94f052015-05-14 19:56:47 +00001088
sewardj3b290482011-05-06 21:02:55 +00001089 strcat (arg_own_buf, ";QStartNoAckMode+");
1090 strcat (arg_own_buf, ";QPassSignals+");
Elliott Hughesa0664b92017-04-18 17:46:52 -07001091 strcat (arg_own_buf, ";QCatchSyscalls+");
sewardj2a312392011-06-26 09:26:48 +00001092 if (VG_(client_auxv))
1093 strcat (arg_own_buf, ";qXfer:auxv:read+");
sewardj3b290482011-05-06 21:02:55 +00001094
philippe419d5f22012-05-24 21:33:17 +00001095 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) {
sewardj3b290482011-05-06 21:02:55 +00001096 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 }
philippe5e94f052015-05-14 19:56:47 +00001108 strcat (arg_own_buf, ";qXfer:exec-file:read+");
philippeb3014692015-05-17 13:38:25 +00001109 strcat (arg_own_buf, ";qXfer:siginfo:read+");
sewardj3b290482011-05-06 21:02:55 +00001110 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. */
1119static
sewardj0bb3c672011-07-26 23:29:25 +00001120void handle_v_requests (char *arg_own_buf, char *status, int *zignal)
sewardj3b290482011-05-06 21:02:55 +00001121{
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
1130static
1131void myresume (int step, int sig)
1132{
1133 struct thread_resume resume_info[2];
1134 int n = 0;
1135
philippe349a3912012-05-23 21:50:36 +00001136 if (step || sig) {
sewardj3b290482011-05-06 21:02:55 +00001137 resume_info[0].step = step;
1138 resume_info[0].sig = sig;
sewardj3b290482011-05-06 21:02:55 +00001139 n++;
1140 }
sewardj3b290482011-05-06 21:02:55 +00001141 resume_info[n].step = 0;
1142 resume_info[n].sig = 0;
sewardj3b290482011-05-06 21:02:55 +00001143
philippe349a3912012-05-23 21:50:36 +00001144 resume_reply_packet_needed = True;
1145 valgrind_resume (resume_info);
sewardj3b290482011-05-06 21:02:55 +00001146}
1147
1148/* server_main global variables */
1149static char *own_buf;
1150static unsigned char *mem_buf;
1151
1152void gdbserver_init (void)
1153{
1154 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
1155 noack_mode = False;
philippe349a3912012-05-23 21:50:36 +00001156 valgrind_initialize_target ();
philippe0e1cac92012-02-28 22:37:44 +00001157 // 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)
philippe03ffc6e2013-07-25 22:37:02 +00001160 own_buf = malloc (PBUFSIZ+POVERHSIZ);
philippe0e1cac92012-02-28 22:37:44 +00001161 if (mem_buf == NULL)
philippe03ffc6e2013-07-25 22:37:02 +00001162 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.
sewardj3b290482011-05-06 21:02:55 +00001168}
1169
1170void 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
1180void server_main (void)
1181{
1182 static char status;
sewardj0bb3c672011-07-26 23:29:25 +00001183 static int zignal;
sewardj3b290482011-05-06 21:02:55 +00001184
1185 char ch;
1186 int i = 0;
1187 unsigned int len;
1188 CORE_ADDR mem_addr;
1189
philippe349a3912012-05-23 21:50:36 +00001190 zignal = valgrind_wait (&status);
sewardj3b290482011-05-06 21:02:55 +00001191 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
philippe349a3912012-05-23 21:50:36 +00001199 if (resume_reply_packet_needed) {
1200 /* Send the resume reply to reply to last GDB resume
1201 request. */
1202 resume_reply_packet_needed = False;
sewardj0bb3c672011-07-26 23:29:25 +00001203 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +00001204 putpkt (own_buf);
1205 }
1206
philippeb3014692015-05-17 13:38:25 +00001207 /* If our status is terminal (exit or fatal signal) get out
philippe0447bbd2012-10-17 21:32:03 +00001208 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
sewardj3b290482011-05-06 21:02:55 +00001214 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);
philippe349a3912012-05-23 21:50:36 +00001242 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001243 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 '?':
sewardj0bb3c672011-07-26 23:29:25 +00001251 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +00001252 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);
philippe349a3912012-05-23 21:50:36 +00001328 if (valgrind_read_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +00001329 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);
philippe349a3912012-05-23 21:50:36 +00001335 if (valgrind_write_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +00001336 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
philippe349a3912012-05-23 21:50:36 +00001343 || valgrind_write_memory (mem_addr, mem_buf, len) != 0)
sewardj3b290482011-05-06 21:02:55 +00001344 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))
sewardj0bb3c672011-07-26 23:29:25 +00001351 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +00001352 else
sewardj0bb3c672011-07-26 23:29:25 +00001353 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +00001354 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +00001355 myresume (0, zignal);
sewardj3b290482011-05-06 21:02:55 +00001356 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))
sewardj0bb3c672011-07-26 23:29:25 +00001360 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +00001361 else
sewardj0bb3c672011-07-26 23:29:25 +00001362 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +00001363 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +00001364 myresume (1, zignal);
sewardj3b290482011-05-06 21:02:55 +00001365 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
philippe349a3912012-05-23 21:50:36 +00001381 if (type < '0' || type > '4') {
1382 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +00001383 own_buf[0] = '\0';
1384 } else {
1385 int res;
1386
philippe349a3912012-05-23 21:50:36 +00001387 res = valgrind_insert_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +00001388 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
philippe349a3912012-05-23 21:50:36 +00001405 if (type < '0' || type > '4') {
1406 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +00001407 own_buf[0] = '\0';
1408 } else {
1409 int res;
1410
philippe349a3912012-05-23 21:50:36 +00001411 res = valgrind_remove_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +00001412 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
philippe349a3912012-05-23 21:50:36 +00001435 if (valgrind_thread_alive (thread_id))
sewardj3b290482011-05-06 21:02:55 +00001436 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. */
sewardj0bb3c672011-07-26 23:29:25 +00001451 handle_v_requests (own_buf, &status, &zignal);
sewardj3b290482011-05-06 21:02:55 +00001452 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')
sewardj0bb3c672011-07-26 23:29:25 +00001467 VG_(umsg) ("\nChild exited with status %d\n", zignal);
sewardj3b290482011-05-06 21:02:55 +00001468 if (status == 'X')
sewardj47ffedb2011-10-24 07:36:57 +00001469 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
florian654ea862015-08-06 12:11:33 +00001470 (UInt)target_signal_to_host (zignal),
sewardj0bb3c672011-07-26 23:29:25 +00001471 target_signal_to_name (zignal));
sewardj3b290482011-05-06 21:02:55 +00001472 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);
philippe349a3912012-05-23 21:50:36 +00001489 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001490 return;
1491}