blob: 23a43e0a9b33a6fc89e5c264b20599092dc068ef [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",
110 (info = NULL ? "" : info));
111 }
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 ?
342 VG_(DebugInfo_get_text_avma) (tooldi) : 0x38000000;
343 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
627 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
628 int i;
629 char *from, *to;
630 char *end = arg_own_buf + strlen(arg_own_buf);
631 CORE_ADDR sig;
632 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
633 pass_signals[i] = 0;
634
635 from = arg_own_buf + 13;
636 while (from < end) {
637 to = strchr(from, ';');
638 if (to == NULL) to = end;
639 decode_address (&sig, from, to - from);
640 pass_signals[(int)sig] = 1;
philippe75d8a4e2015-03-26 21:32:03 +0000641 dlog(3, "pass_signal gdb_nr %d %s\n",
philippe886fde32012-03-29 21:56:47 +0000642 (int)sig, target_signal_to_name(sig));
sewardj3b290482011-05-06 21:02:55 +0000643 from = to;
644 if (*from == ';') from++;
645 }
646 write_ok (arg_own_buf);
647 return;
648 }
649 /* Otherwise we didn't know what packet it was. Say we didn't
650 understand it. */
651 arg_own_buf[0] = 0;
652}
653
philippe02ea4132013-09-04 21:42:43 +0000654Bool VG_(client_monitor_command) (HChar *cmd)
philippe46207652013-01-20 17:11:58 +0000655{
656 const Bool connected = remote_connected();
657 const int saved_command_output_to_log = command_output_to_log;
658 Bool handled;
659
660 if (!connected)
661 command_output_to_log = True;
662 handled = handle_gdb_monitor_command (cmd);
663 if (!connected) {
664 // reset the log output unless cmd changed it.
665 if (command_output_to_log)
666 command_output_to_log = saved_command_output_to_log;
667 }
668 if (handled)
669 return False; // recognised
670 else
671 return True; // not recognised
672}
673
sewardj3b290482011-05-06 21:02:55 +0000674/* Handle all of the extended 'q' packets. */
675static
676void handle_query (char *arg_own_buf, int *new_packet_len_p)
677{
678 static struct inferior_list_entry *thread_ptr;
679
philippe1670b052014-08-15 10:27:52 +0000680 /* thread local storage query */
681 if (strncmp ("qGetTLSAddr:", arg_own_buf, 12) == 0) {
682 char *from, *to;
683 char *end = arg_own_buf + strlen(arg_own_buf);
684 unsigned long gdb_id;
685 CORE_ADDR lm;
686 CORE_ADDR offset;
687 struct thread_info *ti;
688
689 from = arg_own_buf + 12;
690 to = strchr(from, ',');
691 *to = 0;
692 gdb_id = strtoul (from, NULL, 16);
693 from = to + 1;
694 to = strchr(from, ',');
695 decode_address (&offset, from, to - from);
696 from = to + 1;
697 to = end;
698 decode_address (&lm, from, to - from);
699 dlog(2, "qGetTLSAddr thread %lu offset %p lm %p\n",
700 gdb_id, (void*)offset, (void*)lm);
701
702 ti = gdb_id_to_thread (gdb_id);
703 if (ti != NULL) {
704 ThreadState *tst;
705 Addr tls_addr;
706
707 tst = (ThreadState *) inferior_target_data (ti);
708 if (valgrind_get_tls_addr(tst, offset, lm, &tls_addr)) {
709 VG_(sprintf) (arg_own_buf, "%lx", tls_addr);
710 return;
711 }
712 // else we will report we do not support qGetTLSAddr
713 } else {
714 write_enn (arg_own_buf);
715 return;
716 }
717 }
718
sewardj3b290482011-05-06 21:02:55 +0000719 /* qRcmd, monitor command handling. */
720 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
721 char *p = arg_own_buf + 6;
722 int cmdlen = strlen(p)/2;
723 char cmd[cmdlen+1];
724
725 if (unhexify (cmd, p, cmdlen) != cmdlen) {
726 write_enn (arg_own_buf);
727 return;
728 }
729 cmd[cmdlen] = '\0';
730
731 if (handle_gdb_monitor_command (cmd)) {
sewardj3b290482011-05-06 21:02:55 +0000732 write_ok (arg_own_buf);
733 return;
734 } else {
735 /* cmd not recognised */
736 VG_(gdb_printf)
737 ("command '%s' not recognised\n"
738 "In gdb, try 'monitor help'\n"
739 "In a shell, try 'vgdb help'\n",
740 cmd);
741 write_ok (arg_own_buf);
742 return;
743 }
744 }
745
746 /* provide some valgrind specific info in return to qThreadExtraInfo. */
747 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
748 unsigned long gdb_id;
749 struct thread_info *ti;
750 ThreadState *tst;
sewardj3b290482011-05-06 21:02:55 +0000751
752 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
753 ti = gdb_id_to_thread (gdb_id);
754 if (ti != NULL) {
755 tst = (ThreadState *) inferior_target_data (ti);
florian49789512013-09-16 17:08:50 +0000756 /* Additional info is the tid, the thread status and the thread's
757 name, if any. */
florian7b7d5942014-12-19 20:29:22 +0000758 SizeT len = strlen(VG_(name_of_ThreadStatus)(tst->status)) + 20;
759 if (tst->thread_name) len += strlen(tst->thread_name);
760 /* As the string will be hexified and copied into own_buf we need
761 to limit the length to avoid buffer overflow. */
762 if (len * 2 > (PBUFSIZ + POVERHSIZ))
763 len = (PBUFSIZ + POVERHSIZ) / 2;
764 char status[len];
florian49789512013-09-16 17:08:50 +0000765 if (tst->thread_name) {
florian654ea862015-08-06 12:11:33 +0000766 VG_(snprintf) (status, sizeof(status), "tid %u %s %s",
florian49789512013-09-16 17:08:50 +0000767 tst->tid,
768 VG_(name_of_ThreadStatus)(tst->status),
769 tst->thread_name);
770 } else {
florian654ea862015-08-06 12:11:33 +0000771 VG_(snprintf) (status, sizeof(status), "tid %u %s",
florian49789512013-09-16 17:08:50 +0000772 tst->tid,
773 VG_(name_of_ThreadStatus)(tst->status));
774 }
sewardj3b290482011-05-06 21:02:55 +0000775 hexify (arg_own_buf, status, strlen(status));
776 return;
777 } else {
778 write_enn (arg_own_buf);
779 return;
780 }
781 }
philippe1670b052014-08-15 10:27:52 +0000782
sewardj3b290482011-05-06 21:02:55 +0000783 if (strcmp ("qAttached", arg_own_buf) == 0) {
784 /* tell gdb to always detach, never kill the process */
785 arg_own_buf[0] = '1';
786 arg_own_buf[1] = 0;
787 return;
788 }
789
790 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
791 /* We have no symbol to read. */
792 write_ok (arg_own_buf);
793 return;
794 }
795
796 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
797 thread_ptr = all_threads.head;
798 VG_(sprintf) (arg_own_buf, "m%x",
799 thread_to_gdb_id ((struct thread_info *)thread_ptr));
800 thread_ptr = thread_ptr->next;
801 return;
802 }
803
804 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
805 if (thread_ptr != NULL) {
806 VG_(sprintf) (arg_own_buf, "m%x",
807 thread_to_gdb_id ((struct thread_info *)thread_ptr));
808 thread_ptr = thread_ptr->next;
809 return;
810 } else {
811 VG_(sprintf) (arg_own_buf, "l");
812 return;
813 }
814 }
815
philippe419d5f22012-05-24 21:33:17 +0000816 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL
sewardj3b290482011-05-06 21:02:55 +0000817 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
818 CORE_ADDR ofs;
819 unsigned int len, doc_len;
florian6bd9dc12012-11-23 16:17:43 +0000820 const char *annex = NULL;
sewardj3b290482011-05-06 21:02:55 +0000821 // First, the annex is extracted from the packet received.
822 // Then, it is replaced by the corresponding file name.
823 int fd;
824
825 /* Grab the annex, offset, and length. */
826 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
827 strcpy (arg_own_buf, "E00");
828 return;
829 }
830
831 if (strcmp (annex, "target.xml") == 0) {
philippe419d5f22012-05-24 21:33:17 +0000832 annex = valgrind_target_xml(VG_(clo_vgdb_shadow_registers));
833 if (annex != NULL && VG_(clo_vgdb_shadow_registers)) {
834 /* Ensure the shadow registers are initialized. */
835 initialize_shadow_low(True);
sewardj3b290482011-05-06 21:02:55 +0000836 }
sewardj3b290482011-05-06 21:02:55 +0000837 if (annex == NULL) {
838 strcpy (arg_own_buf, "E00");
839 return;
840 }
841 }
842
843 {
philippe75a5f782012-02-24 11:25:58 +0000844 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1];
sewardj3b290482011-05-06 21:02:55 +0000845 struct vg_stat stat_doc;
846 char toread[len];
847 int len_read;
848
849 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
850 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
851 if (fd == -1) {
852 strcpy (arg_own_buf, "E00");
853 return;
854 }
855 if (VG_(fstat) (fd, &stat_doc) != 0) {
856 VG_(close) (fd);
857 strcpy (arg_own_buf, "E00");
858 return;
859 }
860 doc_len = stat_doc.size;
861
862 if (len > PBUFSIZ - POVERHSIZ)
863 len = PBUFSIZ - POVERHSIZ;
864
865 if (ofs > doc_len) {
866 write_enn (arg_own_buf);
867 VG_(close) (fd);
868 return;
869 }
870 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
871 len_read = VG_(read) (fd, toread, len);
philippeb3014692015-05-17 13:38:25 +0000872 *new_packet_len_p = write_qxfer_response (arg_own_buf,
873 (unsigned char *)toread,
874 len_read,
875 ofs + len_read < doc_len);
sewardj3b290482011-05-06 21:02:55 +0000876 VG_(close) (fd);
877 return;
878 }
879 }
880
sewardj2a312392011-06-26 09:26:48 +0000881 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) {
882 unsigned char *data;
883 int n;
884 CORE_ADDR ofs;
885 unsigned int len;
florian6bd9dc12012-11-23 16:17:43 +0000886 const char *annex;
sewardj2a312392011-06-26 09:26:48 +0000887
888 /* Reject any annex; grab the offset and length. */
889 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0
890 || annex[0] != '\0') {
891 strcpy (arg_own_buf, "E00");
892 return;
893 }
894
philippeb3014692015-05-17 13:38:25 +0000895 if (len > PBUFSIZ - POVERHSIZ)
896 len = PBUFSIZ - POVERHSIZ;
sewardj2a312392011-06-26 09:26:48 +0000897 data = malloc (len);
898
899 {
900 UWord *client_auxv = VG_(client_auxv);
901 unsigned int client_auxv_len = 0;
902 while (*client_auxv != 0) {
florian654ea862015-08-06 12:11:33 +0000903 dlog(4, "auxv %llu %llx\n",
sewardj2a312392011-06-26 09:26:48 +0000904 (ULong)*client_auxv,
905 (ULong)*(client_auxv+1));
906 client_auxv++;
907 client_auxv++;
908 client_auxv_len += 2 * sizeof(UWord);
909 }
910 client_auxv_len += 2 * sizeof(UWord);
florian654ea862015-08-06 12:11:33 +0000911 dlog(4, "auxv len %u\n", client_auxv_len);
sewardj2a312392011-06-26 09:26:48 +0000912
913 if (ofs >= client_auxv_len)
914 n = -1;
915 else {
916 n = client_auxv_len - ofs;
917 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n);
918 }
919 }
920
921 if (n < 0)
922 write_enn (arg_own_buf);
923 else if (n > len)
924 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
925 else
926 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
927
928 free (data);
929
930 return;
931 }
932
philippe5e94f052015-05-14 19:56:47 +0000933 if (strncmp ("qXfer:exec-file:read:", arg_own_buf, 21) == 0) {
934 unsigned char *data;
935 int n;
936 CORE_ADDR ofs;
937 unsigned int len;
938 const char *annex;
939 unsigned long pid;
940 const HChar *name;
941
philippeb3014692015-05-17 13:38:25 +0000942 /* grab the annex, offset and length. */
philippe5e94f052015-05-14 19:56:47 +0000943 if (decode_xfer_read (arg_own_buf + 21, &annex, &ofs, &len) < 0) {
944 strcpy (arg_own_buf, "E00");
945 return;
946 }
947
philippeb3014692015-05-17 13:38:25 +0000948 /* Reject any annex with invalid/unexpected pid */
philippe5e94f052015-05-14 19:56:47 +0000949 if (strlen(annex) > 0)
950 pid = strtoul (annex, NULL, 16);
951 else
952 pid = 0;
953 if ((int)pid != VG_(getpid)() && pid != 0) {
954 VG_(sprintf) (arg_own_buf,
955 "E.Valgrind gdbserver pid is %d."
956 " Cannot give info for pid %d",
957 VG_(getpid)(), (int) pid);
958 return;
959 }
960
961 if (len > PBUFSIZ - 2)
962 len = PBUFSIZ - 2;
963 data = malloc (len);
964
965 if (!VG_(resolve_filename)(VG_(cl_exec_fd), &name)) {
966 VG_(sprintf) (arg_own_buf,
967 "E.Valgrind gdbserver could not"
968 " resolve pid %d exec filename.",
969 VG_(getpid)());
970 return;
971 }
972
973 if (ofs >= strlen(name))
974 n = -1;
975 else {
976 n = strlen(name) - ofs;
977 VG_(memcpy) (data, name, n);
978 }
979
980 if (n < 0)
981 write_enn (arg_own_buf);
982 else if (n > len)
983 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
984 else
985 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
986
987 free (data);
988
989 return;
990 }
sewardj2a312392011-06-26 09:26:48 +0000991
philippeb3014692015-05-17 13:38:25 +0000992 if (strncmp ("qXfer:siginfo:read:", arg_own_buf, 19) == 0) {
993 vki_siginfo_t info;
994 int n;
995 CORE_ADDR ofs;
996 unsigned int len;
997 const char *annex;
998
999 /* Reject any annex; grab the offset and length. */
1000 if (decode_xfer_read (arg_own_buf + 19, &annex, &ofs, &len) < 0
1001 || annex[0] != '\0') {
1002 strcpy (arg_own_buf, "E00");
1003 return;
1004 }
1005
1006 if (len > PBUFSIZ - POVERHSIZ)
1007 len = PBUFSIZ - POVERHSIZ;
1008
1009 gdbserver_pending_signal_to_report(&info);
1010
1011 if (ofs >= sizeof(info))
1012 n = -1;
1013 else
1014 n = sizeof(info) - ofs;
1015
1016 if (n < 0)
1017 write_enn (arg_own_buf);
1018 else if (n > len)
1019 *new_packet_len_p = write_qxfer_response (arg_own_buf,
1020 (unsigned char *)&info,
1021 len, 1);
1022 else
1023 *new_packet_len_p = write_qxfer_response (arg_own_buf,
1024 (unsigned char *)&info,
1025 n, 0);
1026
1027 return;
1028 }
1029
sewardj3b290482011-05-06 21:02:55 +00001030 /* Protocol features query. */
1031 if (strncmp ("qSupported", arg_own_buf, 10) == 0
1032 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
florian654ea862015-08-06 12:11:33 +00001033 VG_(sprintf) (arg_own_buf, "PacketSize=%x", (UInt)PBUFSIZ - 1);
sewardj3b290482011-05-06 21:02:55 +00001034 /* Note: max packet size including frame and checksum, but without
1035 trailing null byte, which is not sent/received. */
philippe5e94f052015-05-14 19:56:47 +00001036
sewardj3b290482011-05-06 21:02:55 +00001037 strcat (arg_own_buf, ";QStartNoAckMode+");
1038 strcat (arg_own_buf, ";QPassSignals+");
sewardj2a312392011-06-26 09:26:48 +00001039 if (VG_(client_auxv))
1040 strcat (arg_own_buf, ";qXfer:auxv:read+");
sewardj3b290482011-05-06 21:02:55 +00001041
philippe419d5f22012-05-24 21:33:17 +00001042 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) {
sewardj3b290482011-05-06 21:02:55 +00001043 strcat (arg_own_buf, ";qXfer:features:read+");
1044 /* if a new gdb connects to us, we have to reset the register
1045 set to the normal register sets to allow this new gdb to
1046 decide to use or not the shadow registers.
1047
1048 Note that the reset is only done for gdb that are sending
1049 qSupported packets. If a user first connected with a recent
1050 gdb using shadow registers and then with a very old gdb
1051 that does not use qSupported packet, then the old gdb will
1052 not properly connect. */
1053 initialize_shadow_low(False);
1054 }
philippe5e94f052015-05-14 19:56:47 +00001055 strcat (arg_own_buf, ";qXfer:exec-file:read+");
philippeb3014692015-05-17 13:38:25 +00001056 strcat (arg_own_buf, ";qXfer:siginfo:read+");
sewardj3b290482011-05-06 21:02:55 +00001057 return;
1058 }
1059
1060 /* Otherwise we didn't know what packet it was. Say we didn't
1061 understand it. */
1062 arg_own_buf[0] = 0;
1063}
1064
1065/* Handle all of the extended 'v' packets. */
1066static
sewardj0bb3c672011-07-26 23:29:25 +00001067void handle_v_requests (char *arg_own_buf, char *status, int *zignal)
sewardj3b290482011-05-06 21:02:55 +00001068{
1069 /* vcont packet code from gdb 6.6 removed */
1070
1071 /* Otherwise we didn't know what packet it was. Say we didn't
1072 understand it. */
1073 arg_own_buf[0] = 0;
1074 return;
1075}
1076
1077static
1078void myresume (int step, int sig)
1079{
1080 struct thread_resume resume_info[2];
1081 int n = 0;
1082
philippe349a3912012-05-23 21:50:36 +00001083 if (step || sig) {
sewardj3b290482011-05-06 21:02:55 +00001084 resume_info[0].step = step;
1085 resume_info[0].sig = sig;
sewardj3b290482011-05-06 21:02:55 +00001086 n++;
1087 }
sewardj3b290482011-05-06 21:02:55 +00001088 resume_info[n].step = 0;
1089 resume_info[n].sig = 0;
sewardj3b290482011-05-06 21:02:55 +00001090
philippe349a3912012-05-23 21:50:36 +00001091 resume_reply_packet_needed = True;
1092 valgrind_resume (resume_info);
sewardj3b290482011-05-06 21:02:55 +00001093}
1094
1095/* server_main global variables */
1096static char *own_buf;
1097static unsigned char *mem_buf;
1098
1099void gdbserver_init (void)
1100{
1101 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
1102 noack_mode = False;
philippe349a3912012-05-23 21:50:36 +00001103 valgrind_initialize_target ();
philippe0e1cac92012-02-28 22:37:44 +00001104 // After a fork, gdbserver_init can be called again.
1105 // We do not have to re-malloc the buffers in such a case.
1106 if (own_buf == NULL)
philippe03ffc6e2013-07-25 22:37:02 +00001107 own_buf = malloc (PBUFSIZ+POVERHSIZ);
philippe0e1cac92012-02-28 22:37:44 +00001108 if (mem_buf == NULL)
philippe03ffc6e2013-07-25 22:37:02 +00001109 mem_buf = malloc (PBUFSIZ+POVERHSIZ);
1110 // Note: normally, we should only malloc PBUFSIZ. However,
1111 // GDB has a bug, and in some cases, sends e.g. 'm' packets
1112 // asking for slightly more than the PacketSize given at
1113 // connection initialisation. So, we bypass the GDB bug
1114 // by allocating slightly more.
sewardj3b290482011-05-06 21:02:55 +00001115}
1116
1117void gdbserver_terminate (void)
1118{
1119 /* last call to gdbserver is cleanup call */
1120 if (VG_MINIMAL_SETJMP(toplevel)) {
1121 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
1122 return;
1123 }
1124 remote_close();
1125}
1126
1127void server_main (void)
1128{
1129 static char status;
sewardj0bb3c672011-07-26 23:29:25 +00001130 static int zignal;
sewardj3b290482011-05-06 21:02:55 +00001131
1132 char ch;
1133 int i = 0;
1134 unsigned int len;
1135 CORE_ADDR mem_addr;
1136
philippe349a3912012-05-23 21:50:36 +00001137 zignal = valgrind_wait (&status);
sewardj3b290482011-05-06 21:02:55 +00001138 if (VG_MINIMAL_SETJMP(toplevel)) {
1139 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
1140 }
1141 while (1) {
1142 unsigned char sig;
1143 int packet_len;
1144 int new_packet_len = -1;
1145
philippe349a3912012-05-23 21:50:36 +00001146 if (resume_reply_packet_needed) {
1147 /* Send the resume reply to reply to last GDB resume
1148 request. */
1149 resume_reply_packet_needed = False;
sewardj0bb3c672011-07-26 23:29:25 +00001150 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +00001151 putpkt (own_buf);
1152 }
1153
philippeb3014692015-05-17 13:38:25 +00001154 /* If our status is terminal (exit or fatal signal) get out
philippe0447bbd2012-10-17 21:32:03 +00001155 as quickly as we can. We won't be able to handle any request
1156 anymore. */
1157 if (status == 'W' || status == 'X') {
1158 return;
1159 }
1160
sewardj3b290482011-05-06 21:02:55 +00001161 packet_len = getpkt (own_buf);
1162 if (packet_len <= 0)
1163 break;
1164
1165 i = 0;
1166 ch = own_buf[i++];
1167 switch (ch) {
1168 case 'Q':
1169 handle_set (own_buf, &new_packet_len);
1170 break;
1171 case 'q':
1172 handle_query (own_buf, &new_packet_len);
1173 break;
1174 case 'd':
1175 /* set/unset debugging is done through valgrind debug level. */
1176 own_buf[0] = '\0';
1177 break;
1178 case 'D':
1179 reset_valgrind_sink("gdb detaching from process");
1180
1181 /* When detaching or kill the process, gdb expects to get
1182 an packet OK back. Any other output will make gdb
1183 believes detach did not work. */
1184 write_ok (own_buf);
1185 putpkt (own_buf);
1186 remote_finish (reset_after_error);
1187 remote_open (VG_(clo_vgdb_prefix));
1188 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +00001189 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001190 return;
1191 case '!':
1192 /* We can not use the extended protocol with valgrind,
1193 because we can not restart the running
1194 program. So return unrecognized. */
1195 own_buf[0] = '\0';
1196 break;
1197 case '?':
sewardj0bb3c672011-07-26 23:29:25 +00001198 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +00001199 break;
1200 case 'H':
1201 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
1202 unsigned long gdb_id, thread_id;
1203
1204 gdb_id = strtoul (&own_buf[2], NULL, 16);
1205 thread_id = gdb_id_to_thread_id (gdb_id);
1206 if (thread_id == 0) {
1207 write_enn (own_buf);
1208 break;
1209 }
1210
1211 if (own_buf[1] == 'g') {
1212 general_thread = thread_id;
1213 set_desired_inferior (1);
1214 } else if (own_buf[1] == 'c') {
1215 cont_thread = thread_id;
1216 } else if (own_buf[1] == 's') {
1217 step_thread = thread_id;
1218 }
1219
1220 write_ok (own_buf);
1221 } else {
1222 /* Silently ignore it so that gdb can extend the protocol
1223 without compatibility headaches. */
1224 own_buf[0] = '\0';
1225 }
1226 break;
1227 case 'g':
1228 set_desired_inferior (1);
1229 registers_to_string (own_buf);
1230 break;
1231 case 'G':
1232 set_desired_inferior (1);
1233 registers_from_string (&own_buf[1]);
1234 write_ok (own_buf);
1235 break;
1236 case 'P': {
1237 int regno;
1238 char *regbytes;
1239 Bool mod;
1240 ThreadState *tst;
1241 regno = strtol(&own_buf[1], NULL, 16);
1242 regbytes = strchr(&own_buf[0], '=') + 1;
1243 set_desired_inferior (1);
1244 tst = (ThreadState *) inferior_target_data (current_inferior);
1245 /* Only accept changing registers in "runnable state3.
1246 In fact, it would be ok to change most of the registers
1247 except a few "sensitive" registers such as the PC, SP, BP.
1248 We assume we do not need to very specific here, and that we
1249 can just refuse all of these. */
1250 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
1251 supply_register_from_string (regno, regbytes, &mod);
1252 write_ok (own_buf);
1253 } else {
1254 /* at least from gdb 6.6 onwards, an E. error
1255 reply is shown to the user. So, we do an error
1256 msg which both is accepted by gdb as an error msg
1257 and is readable by the user. */
1258 VG_(sprintf)
1259 (own_buf,
1260"E.\n"
1261"ERROR changing register %s regno %d\n"
1262"gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
1263"set pc, calling from gdb a function in the debugged process, ...)\n"
1264"can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
1265"Thread status is %s\n",
1266 find_register_by_number (regno)->name, regno,
1267 VG_(name_of_ThreadStatus)(tst->status));
1268 if (VG_(clo_verbosity) > 1)
1269 VG_(umsg) ("%s\n", own_buf);
1270 }
1271 break;
1272 }
1273 case 'm':
1274 decode_m_packet (&own_buf[1], &mem_addr, &len);
philippe349a3912012-05-23 21:50:36 +00001275 if (valgrind_read_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +00001276 convert_int_to_ascii (mem_buf, own_buf, len);
1277 else
1278 write_enn (own_buf);
1279 break;
1280 case 'M':
1281 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
philippe349a3912012-05-23 21:50:36 +00001282 if (valgrind_write_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +00001283 write_ok (own_buf);
1284 else
1285 write_enn (own_buf);
1286 break;
1287 case 'X':
1288 if (decode_X_packet (&own_buf[1], packet_len - 1,
1289 &mem_addr, &len, mem_buf) < 0
philippe349a3912012-05-23 21:50:36 +00001290 || valgrind_write_memory (mem_addr, mem_buf, len) != 0)
sewardj3b290482011-05-06 21:02:55 +00001291 write_enn (own_buf);
1292 else
1293 write_ok (own_buf);
1294 break;
1295 case 'C':
1296 convert_ascii_to_int (own_buf + 1, &sig, 1);
1297 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +00001298 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +00001299 else
sewardj0bb3c672011-07-26 23:29:25 +00001300 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +00001301 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +00001302 myresume (0, zignal);
sewardj3b290482011-05-06 21:02:55 +00001303 return; // return control to valgrind
1304 case 'S':
1305 convert_ascii_to_int (own_buf + 1, &sig, 1);
1306 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +00001307 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +00001308 else
sewardj0bb3c672011-07-26 23:29:25 +00001309 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +00001310 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +00001311 myresume (1, zignal);
sewardj3b290482011-05-06 21:02:55 +00001312 return; // return control to valgrind
1313 case 'c':
1314 set_desired_inferior (0);
1315 myresume (0, 0);
1316 return; // return control to valgrind
1317 case 's':
1318 set_desired_inferior (0);
1319 myresume (1, 0);
1320 return; // return control to valgrind
1321 case 'Z': {
1322 char *lenptr;
1323 char *dataptr;
1324 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1325 int zlen = strtol (lenptr + 1, &dataptr, 16);
1326 char type = own_buf[1];
1327
philippe349a3912012-05-23 21:50:36 +00001328 if (type < '0' || type > '4') {
1329 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +00001330 own_buf[0] = '\0';
1331 } else {
1332 int res;
1333
philippe349a3912012-05-23 21:50:36 +00001334 res = valgrind_insert_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +00001335 if (res == 0)
1336 write_ok (own_buf);
1337 else if (res == 1)
1338 /* Unsupported. */
1339 own_buf[0] = '\0';
1340 else
1341 write_enn (own_buf);
1342 }
1343 break;
1344 }
1345 case 'z': {
1346 char *lenptr;
1347 char *dataptr;
1348 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1349 int zlen = strtol (lenptr + 1, &dataptr, 16);
1350 char type = own_buf[1];
1351
philippe349a3912012-05-23 21:50:36 +00001352 if (type < '0' || type > '4') {
1353 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +00001354 own_buf[0] = '\0';
1355 } else {
1356 int res;
1357
philippe349a3912012-05-23 21:50:36 +00001358 res = valgrind_remove_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +00001359 if (res == 0)
1360 write_ok (own_buf);
1361 else if (res == 1)
1362 /* Unsupported. */
1363 own_buf[0] = '\0';
1364 else
1365 write_enn (own_buf);
1366 }
1367 break;
1368 }
1369 case 'k':
1370 kill_request("Gdb request to kill this process\n");
1371 break;
1372 case 'T': {
1373 unsigned long gdb_id, thread_id;
1374
1375 gdb_id = strtoul (&own_buf[1], NULL, 16);
1376 thread_id = gdb_id_to_thread_id (gdb_id);
1377 if (thread_id == 0) {
1378 write_enn (own_buf);
1379 break;
1380 }
1381
philippe349a3912012-05-23 21:50:36 +00001382 if (valgrind_thread_alive (thread_id))
sewardj3b290482011-05-06 21:02:55 +00001383 write_ok (own_buf);
1384 else
1385 write_enn (own_buf);
1386 break;
1387 }
1388 case 'R':
1389 /* Restarting the inferior is only supported in the
1390 extended protocol.
1391 => It is a request we don't understand. Respond with an
1392 empty packet so that gdb knows that we don't support this
1393 request. */
1394 own_buf[0] = '\0';
1395 break;
1396 case 'v':
1397 /* Extended (long) request. */
sewardj0bb3c672011-07-26 23:29:25 +00001398 handle_v_requests (own_buf, &status, &zignal);
sewardj3b290482011-05-06 21:02:55 +00001399 break;
1400 default:
1401 /* It is a request we don't understand. Respond with an
1402 empty packet so that gdb knows that we don't support this
1403 request. */
1404 own_buf[0] = '\0';
1405 break;
1406 }
1407
1408 if (new_packet_len != -1)
1409 putpkt_binary (own_buf, new_packet_len);
1410 else
1411 putpkt (own_buf);
1412
1413 if (status == 'W')
sewardj0bb3c672011-07-26 23:29:25 +00001414 VG_(umsg) ("\nChild exited with status %d\n", zignal);
sewardj3b290482011-05-06 21:02:55 +00001415 if (status == 'X')
sewardj47ffedb2011-10-24 07:36:57 +00001416 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
florian654ea862015-08-06 12:11:33 +00001417 (UInt)target_signal_to_host (zignal),
sewardj0bb3c672011-07-26 23:29:25 +00001418 target_signal_to_name (zignal));
sewardj3b290482011-05-06 21:02:55 +00001419 if (status == 'W' || status == 'X') {
1420 VG_(umsg) ("Process exiting\n");
1421 VG_(exit) (0);
1422 }
1423 }
1424
1425 /* We come here when getpkt fails => close the connection,
1426 and re-open. Then return control to valgrind.
1427 We return the control to valgrind as we assume that
1428 the connection was closed due to vgdb having finished
1429 to execute a command. */
1430 if (VG_(clo_verbosity) > 1)
1431 VG_(umsg) ("Remote side has terminated connection. "
1432 "GDBserver will reopen the connection.\n");
1433 remote_finish (reset_after_error);
1434 remote_open (VG_(clo_vgdb_prefix));
1435 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +00001436 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001437 return;
1438}