blob: 1d6ff8a6c038c5748e8b8beb2f547026de150361 [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"
floriandbb35842012-10-27 18:39:11 +000030#include "pub_core_syswrap.h" // VG_(show_open_fds)
sewardj3b290482011-05-06 21:02:55 +000031
32unsigned long cont_thread;
33unsigned long general_thread;
34unsigned long step_thread;
35unsigned long thread_from_wait;
36unsigned long old_thread_from_wait;
37
philippe886fde32012-03-29 21:56:47 +000038int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */
bart2dafc542011-05-18 16:08:28 +000039
sewardj3b290482011-05-06 21:02:55 +000040/* for a gdbserver integrated in valgrind, resuming the process consists
41 in returning the control to valgrind.
philippe349a3912012-05-23 21:50:36 +000042 The guess process resumes its execution.
sewardj3b290482011-05-06 21:02:55 +000043 Then at the next error or break or ..., valgrind calls gdbserver again.
philippe349a3912012-05-23 21:50:36 +000044 A resume reply packet must then be built to inform GDB that the
45 resume request is finished.
46 resume_reply_packet_needed records the fact that the next call to gdbserver
sewardj3b290482011-05-06 21:02:55 +000047 must send a resume packet to gdb. */
philippe349a3912012-05-23 21:50:36 +000048static Bool resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +000049
50VG_MINIMAL_JMP_BUF(toplevel);
51
52/* Decode a qXfer read request. Return 0 if everything looks OK,
53 or -1 otherwise. */
54
55static
56int decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
57{
58 /* Extract and NUL-terminate the annex. */
59 *annex = buf;
60 while (*buf && *buf != ':')
61 buf++;
62 if (*buf == '\0')
63 return -1;
64 *buf++ = 0;
65
66 /* After the read/write marker and annex, qXfer looks like a
67 traditional 'm' packet. */
68 decode_m_packet (buf, ofs, len);
69
70 return 0;
71}
72
73/* Write the response to a successful qXfer read. Returns the
74 length of the (binary) data stored in BUF, corresponding
75 to as much of DATA/LEN as we could fit. IS_MORE controls
76 the first character of the response. */
77static
78int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
79{
80 int out_len;
81
82 if (is_more)
83 buf[0] = 'm';
84 else
85 buf[0] = 'l';
86
87 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
88 PBUFSIZ - POVERHSIZ - 1) + 1;
89}
90
91static Bool initial_valgrind_sink_saved = False;
92/* True <=> valgrind log sink saved in initial_valgrind_sink */
93static OutputSink initial_valgrind_sink;
94
95static Bool command_output_to_log = False;
96/* True <=> command output goes to log instead of gdb */
97
98void reset_valgrind_sink(char *info)
99{
100 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd
101 && initial_valgrind_sink_saved) {
102 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
103 VG_(umsg) ("Reset valgrind output to log (%s)\n",
104 (info = NULL ? "" : info));
105 }
106}
107
108static
109void kill_request (char *msg)
110{
111 VG_(umsg) ("%s", msg);
112 remote_close();
113 VG_(exit) (0);
114}
115
116/* handle_gdb_valgrind_command handles the provided mon string command.
117 If command is recognised, return 1 else return 0.
118 Note that in case of ambiguous command, 1 is returned.
119
120 *sink_wanted_at_return is modified if one of the commands
sewardj30b3eca2011-06-28 08:20:39 +0000121 'v.set *_output' is handled.
sewardj3b290482011-05-06 21:02:55 +0000122*/
123static
124int handle_gdb_valgrind_command (char* mon, OutputSink* sink_wanted_at_return)
125{
126 UWord ret = 0;
127 char s[strlen(mon)+1]; /* copy for strtok_r */
128 char* wcmd;
florian19f91bb2012-11-10 22:29:54 +0000129 HChar* ssaveptr;
sewardj3b290482011-05-06 21:02:55 +0000130 char* endptr;
131 int kwdid;
132 int int_value;
133
134 vg_assert (initial_valgrind_sink_saved);
135
136 strcpy (s, mon);
137 wcmd = strtok_r (s, " ", &ssaveptr);
138 /* NB: if possible, avoid introducing a new command below which
sewardj30b3eca2011-06-28 08:20:39 +0000139 starts with the same 3 first letters as an already existing
sewardj3b290482011-05-06 21:02:55 +0000140 command. This ensures a shorter abbreviation for the user. */
sewardj30b3eca2011-06-28 08:20:39 +0000141 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate",
sewardj3b290482011-05-06 21:02:55 +0000142 wcmd, kwd_report_duplicated_matches)) {
143 case -2:
144 ret = 1;
145 break;
146 case -1:
147 break;
148 case 0: /* help */
149 ret = 1;
150 wcmd = strtok_r (NULL, " ", &ssaveptr);
151 if (wcmd == NULL) {
152 int_value = 0;
153 } else {
154 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) {
155 case -2: int_value = 0; break;
156 case -1: int_value = 0; break;
157 case 0: int_value = 1; break;
158 default: tl_assert (0);
159 }
160 }
161
162 VG_(gdb_printf) (
163"general valgrind monitor commands:\n"
philippec3360382012-10-21 14:37:14 +0000164" help [debug] : monitor command help. With debug: + debugging commands\n"
sewardj30b3eca2011-06-28 08:20:39 +0000165" v.wait [<ms>] : sleep <ms> (default 0) then continue\n"
166" v.info all_errors : show all errors found so far\n"
167" v.info last_error : show last error found\n"
168" v.info n_errs_found : show the nr of errors found so far\n"
philippec3360382012-10-21 14:37:14 +0000169" v.info open_fds : show open file descriptors (only if --track-fds=yes)\n"
sewardj30b3eca2011-06-28 08:20:39 +0000170" v.kill : kill the Valgrind process\n"
171" v.set gdb_output : set valgrind output to gdb\n"
172" v.set log_output : set valgrind output to log\n"
173" v.set mixed_output : set valgrind output to log, interactive output to gdb\n"
174" v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
sewardj3b290482011-05-06 21:02:55 +0000175 if (int_value) { VG_(gdb_printf) (
176"debugging valgrind internals monitor commands:\n"
sewardj30b3eca2011-06-28 08:20:39 +0000177" v.info gdbserver_status : show gdbserver status\n"
philippe93a6a8d2012-04-27 22:59:43 +0000178" v.info memory [aspacemgr] : show valgrind heap memory stats\n"
179" (with aspacemgr arg, also shows valgrind segments on log ouput)\n"
sewardjd6e13d82011-10-22 20:23:30 +0000180" v.info scheduler : show valgrind thread state and stacktrace\n"
sewardj30b3eca2011-06-28 08:20:39 +0000181" v.set debuglog <level> : set valgrind debug log level to <level>\n"
182" v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
sewardj3b290482011-05-06 21:02:55 +0000183" (default traceflags 0b00100000 : show after instrumentation)\n"
184" An additional flag 0b100000000 allows to show gdbserver instrumentation\n");
185 }
186 break;
sewardj30b3eca2011-06-28 08:20:39 +0000187 case 1: /* v.set */
sewardj3b290482011-05-06 21:02:55 +0000188 ret = 1;
189 wcmd = strtok_r (NULL, " ", &ssaveptr);
190 switch (kwdid = VG_(keyword_id)
191 ("vgdb-error debuglog gdb_output log_output mixed_output",
192 wcmd, kwd_report_all)) {
193 case -2:
194 case -1:
195 break;
196 case 0: /* vgdb-error */
197 case 1: /* debuglog */
198 wcmd = strtok_r (NULL, " ", &ssaveptr);
199 if (wcmd == NULL) {
200 int_value = 0;
201 endptr = "empty"; /* to report an error below */
202 } else {
203 int_value = strtol (wcmd, &endptr, 10);
204 }
205 if (*endptr != '\0') {
206 VG_(gdb_printf) ("missing or malformed integer value\n");
207 } else if (kwdid == 0) {
208 VG_(gdb_printf) ("vgdb-error value changed from %d to %d\n",
209 VG_(dyn_vgdb_error), int_value);
210 VG_(dyn_vgdb_error) = int_value;
211 } else if (kwdid == 1) {
212 VG_(gdb_printf) ("debuglog value changed from %d to %d\n",
213 VG_(debugLog_getLevel)(), int_value);
214 VG_(debugLog_startup) (int_value, "gdbsrv");
215 } else {
216 vg_assert (0);
217 }
218 break;
219 case 2: /* gdb_output */
220 (*sink_wanted_at_return).fd = -2;
221 command_output_to_log = False;
222 VG_(gdb_printf) ("valgrind output will go to gdb\n");
223 break;
224 case 3: /* log_output */
225 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
226 command_output_to_log = True;
227 VG_(gdb_printf) ("valgrind output will go to log\n");
228 break;
229 case 4: /* mixed output */
230 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
231 command_output_to_log = False;
232 VG_(gdb_printf)
233 ("valgrind output will go to log, interactive output will go to gdb\n");
234 break;
235 default:
236 vg_assert (0);
237 }
238 break;
sewardj30b3eca2011-06-28 08:20:39 +0000239 case 2: /* v.info */ {
sewardj3b290482011-05-06 21:02:55 +0000240 ret = 1;
241 wcmd = strtok_r (NULL, " ", &ssaveptr);
242 switch (kwdid = VG_(keyword_id)
sewardjd6e13d82011-10-22 20:23:30 +0000243 ("all_errors n_errs_found last_error gdbserver_status memory"
philippec3360382012-10-21 14:37:14 +0000244 " scheduler open_fds",
sewardj3b290482011-05-06 21:02:55 +0000245 wcmd, kwd_report_all)) {
246 case -2:
247 case -1:
248 break;
249 case 0: // all_errors
250 // A verbosity of minimum 2 is needed to show the errors.
251 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False);
252 break;
253 case 1: // n_errs_found
philippebaf69642012-02-15 22:29:30 +0000254 VG_(gdb_printf) ("n_errs_found %d n_errs_shown %d (vgdb-error %d)\n",
sewardj3b290482011-05-06 21:02:55 +0000255 VG_(get_n_errs_found) (),
philippebaf69642012-02-15 22:29:30 +0000256 VG_(get_n_errs_shown) (),
sewardj3b290482011-05-06 21:02:55 +0000257 VG_(dyn_vgdb_error));
258 break;
259 case 2: // last_error
260 VG_(show_last_error)();
261 break;
262 case 3: // gdbserver_status
263 VG_(gdbserver_status_output)();
264 break;
265 case 4: /* memory */
266 VG_(print_all_arena_stats) ();
267 if (VG_(clo_profile_heap))
268 VG_(print_arena_cc_analysis) ();
philippe93a6a8d2012-04-27 22:59:43 +0000269 wcmd = strtok_r (NULL, " ", &ssaveptr);
270 if (wcmd != NULL) {
271 switch (VG_(keyword_id) ("aspacemgr", wcmd, kwd_report_all)) {
272 case -2:
273 case -1: break;
274 case 0:
275 VG_(am_show_nsegments) (0, "gdbserver v.info memory aspacemgr");
276 break;
277 default: tl_assert (0);
278 }
279 }
280
sewardj3b290482011-05-06 21:02:55 +0000281 ret = 1;
282 break;
sewardjd6e13d82011-10-22 20:23:30 +0000283 case 5: /* scheduler */
284 VG_(show_sched_status) ();
285 ret = 1;
286 break;
philippec3360382012-10-21 14:37:14 +0000287 case 6: /* open_fds */
288 if (VG_(clo_track_fds))
289 VG_(show_open_fds) ("");
290 else
291 VG_(gdb_printf)
292 ("Valgrind must be started with --track-fds=yes"
293 " to show open fds\n");
294 ret = 1;
295 break;
sewardj3b290482011-05-06 21:02:55 +0000296 default:
297 vg_assert(0);
298 }
299 break;
300 }
sewardj30b3eca2011-06-28 08:20:39 +0000301 case 3: /* v.wait */
sewardj3b290482011-05-06 21:02:55 +0000302 wcmd = strtok_r (NULL, " ", &ssaveptr);
303 if (wcmd != NULL) {
304 int_value = strtol (wcmd, &endptr, 10);
305 VG_(gdb_printf) ("gdbserver: continuing in %d ms ...\n", int_value);
306 VG_(poll)(NULL, 0, int_value);
307 }
308 VG_(gdb_printf) ("gdbserver: continuing after wait ...\n");
309 ret = 1;
310 break;
sewardj30b3eca2011-06-28 08:20:39 +0000311 case 4: /* v.kill */
sewardj3b290482011-05-06 21:02:55 +0000312 kill_request ("monitor command request to kill this process\n");
313 break;
sewardj30b3eca2011-06-28 08:20:39 +0000314 case 5: { /* v.translate */
sewardj3b290482011-05-06 21:02:55 +0000315 Addr address;
316 SizeT verbosity = 0x20;
317
318 ret = 1;
319
320 VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr);
321 if (address != (Addr) 0 || verbosity != 0) {
322 /* we need to force the output to log for the translation trace,
323 as low level VEX tracing cannot be redirected to gdb. */
324 int saved_command_output_to_log = command_output_to_log;
325 int saved_fd = VG_(log_output_sink).fd;
326 Bool single_stepping_on_entry = valgrind_single_stepping();
327 int vex_verbosity = verbosity & 0xff;
328 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
329 if ((verbosity & 0x100) && !single_stepping_on_entry) {
330 valgrind_set_single_stepping(True);
331 // to force gdbserver instrumentation.
332 }
sewardj99d61342011-05-17 16:35:11 +0000333# if defined(VGA_arm)
334 // on arm, we need to (potentially) convert this address
335 // to the thumb form.
336 address = thumb_pc (address);
337# endif
338
sewardj3b290482011-05-06 21:02:55 +0000339 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
340 address,
341 /*debugging*/True,
342 (Int) vex_verbosity,
343 /*bbs_done*/0,
344 /*allow redir?*/True);
345 if ((verbosity & 0x100) && !single_stepping_on_entry) {
346 valgrind_set_single_stepping(False);
347 // reset single stepping.
348 }
349 command_output_to_log = saved_command_output_to_log;
350 VG_(log_output_sink).fd = saved_fd;
351 }
352 break;
353 }
354
355 default:
356 vg_assert (0);
357 }
358 return ret;
359}
360
361/* handle_gdb_monitor_command handles the provided mon string command,
362 which can be either a "standard" valgrind monitor command
363 or a tool specific monitor command.
364 If command recognised, return 1 else return 0.
365 Note that in case of ambiguous command, 1 is returned.
366*/
367static
368int handle_gdb_monitor_command (char* mon)
369{
370 UWord ret = 0;
371 UWord tool_ret = 0;
372 // initially, we assume that when returning, the desired sink is the
373 // one we have when entering. It can however be changed by the standard
374 // valgrind command handling.
375 OutputSink sink_wanted_at_return = VG_(log_output_sink);
376
377 if (!initial_valgrind_sink_saved) {
378 /* first time we enter here, we save the valgrind default log sink */
379 initial_valgrind_sink = sink_wanted_at_return;
380 initial_valgrind_sink_saved = True;
381 }
382
383 if (!command_output_to_log)
384 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */
385
386 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return);
387
388 /* Even if command was recognised by valgrind core, we call the
389 tool command handler : this is needed to handle help command
390 and/or to let the tool do some additional processing of a
391 valgrind standard command. Note however that if valgrind
392 recognised the command, we will always return success. */
393 if (VG_(needs).client_requests) {
394 /* If the tool reports an error when handling a monitor command,
395 we need to avoid calling gdbserver during this command
396 handling. So, we temporarily set VG_(dyn_vgdb_error) to
397 a huge value to ensure m_errormgr.c does not call gdbserver. */
398 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error);
399 UWord arg[2];
400 VG_(dyn_vgdb_error) = 999999999;
401 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND;
402 arg[1] = (UWord) mon;
403 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg,
404 &tool_ret);
405 VG_(dyn_vgdb_error) = save_dyn_vgdb_error;
406 }
407
408 /* restore or set the desired output */
409 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
410 if (ret | tool_ret)
411 return 1;
412 else
413 return 0;
414}
415
416
417/* Handle all of the extended 'Q' packets. */
418static
419void handle_set (char *arg_own_buf, int *new_packet_len_p)
420{
421 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) {
422 noack_mode = True;
423 write_ok (arg_own_buf);
424 return;
425 }
426
427 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
428 int i;
429 char *from, *to;
430 char *end = arg_own_buf + strlen(arg_own_buf);
431 CORE_ADDR sig;
432 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
433 pass_signals[i] = 0;
434
435 from = arg_own_buf + 13;
436 while (from < end) {
437 to = strchr(from, ';');
438 if (to == NULL) to = end;
439 decode_address (&sig, from, to - from);
440 pass_signals[(int)sig] = 1;
philippe886fde32012-03-29 21:56:47 +0000441 dlog(1, "pass_signal gdb_nr %d %s\n",
442 (int)sig, target_signal_to_name(sig));
sewardj3b290482011-05-06 21:02:55 +0000443 from = to;
444 if (*from == ';') from++;
445 }
446 write_ok (arg_own_buf);
447 return;
448 }
449 /* Otherwise we didn't know what packet it was. Say we didn't
450 understand it. */
451 arg_own_buf[0] = 0;
452}
453
454/* Handle all of the extended 'q' packets. */
455static
456void handle_query (char *arg_own_buf, int *new_packet_len_p)
457{
458 static struct inferior_list_entry *thread_ptr;
459
460 /* qRcmd, monitor command handling. */
461 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
462 char *p = arg_own_buf + 6;
463 int cmdlen = strlen(p)/2;
464 char cmd[cmdlen+1];
465
466 if (unhexify (cmd, p, cmdlen) != cmdlen) {
467 write_enn (arg_own_buf);
468 return;
469 }
470 cmd[cmdlen] = '\0';
471
472 if (handle_gdb_monitor_command (cmd)) {
473 /* In case the command is from a standalone vgdb,
474 connection will be closed soon => flush the output. */
475 VG_(message_flush) ();
476 write_ok (arg_own_buf);
477 return;
478 } else {
479 /* cmd not recognised */
480 VG_(gdb_printf)
481 ("command '%s' not recognised\n"
482 "In gdb, try 'monitor help'\n"
483 "In a shell, try 'vgdb help'\n",
484 cmd);
485 write_ok (arg_own_buf);
486 return;
487 }
488 }
489
490 /* provide some valgrind specific info in return to qThreadExtraInfo. */
491 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
492 unsigned long gdb_id;
493 struct thread_info *ti;
494 ThreadState *tst;
495 char status[100];
496
497 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
498 ti = gdb_id_to_thread (gdb_id);
499 if (ti != NULL) {
500 tst = (ThreadState *) inferior_target_data (ti);
501 /* Additional info is the tid and the thread status. */
502 VG_(snprintf) (status, sizeof(status), "tid %d %s",
503 tst->tid,
504 VG_(name_of_ThreadStatus)(tst->status));
505 hexify (arg_own_buf, status, strlen(status));
506 return;
507 } else {
508 write_enn (arg_own_buf);
509 return;
510 }
511 }
512
513 if (strcmp ("qAttached", arg_own_buf) == 0) {
514 /* tell gdb to always detach, never kill the process */
515 arg_own_buf[0] = '1';
516 arg_own_buf[1] = 0;
517 return;
518 }
519
520 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
521 /* We have no symbol to read. */
522 write_ok (arg_own_buf);
523 return;
524 }
525
526 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
527 thread_ptr = all_threads.head;
528 VG_(sprintf) (arg_own_buf, "m%x",
529 thread_to_gdb_id ((struct thread_info *)thread_ptr));
530 thread_ptr = thread_ptr->next;
531 return;
532 }
533
534 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
535 if (thread_ptr != NULL) {
536 VG_(sprintf) (arg_own_buf, "m%x",
537 thread_to_gdb_id ((struct thread_info *)thread_ptr));
538 thread_ptr = thread_ptr->next;
539 return;
540 } else {
541 VG_(sprintf) (arg_own_buf, "l");
542 return;
543 }
544 }
545
philippe419d5f22012-05-24 21:33:17 +0000546 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL
sewardj3b290482011-05-06 21:02:55 +0000547 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
548 CORE_ADDR ofs;
549 unsigned int len, doc_len;
550 char *annex = NULL;
551 // First, the annex is extracted from the packet received.
552 // Then, it is replaced by the corresponding file name.
553 int fd;
554
555 /* Grab the annex, offset, and length. */
556 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
557 strcpy (arg_own_buf, "E00");
558 return;
559 }
560
561 if (strcmp (annex, "target.xml") == 0) {
philippe419d5f22012-05-24 21:33:17 +0000562 annex = valgrind_target_xml(VG_(clo_vgdb_shadow_registers));
563 if (annex != NULL && VG_(clo_vgdb_shadow_registers)) {
564 /* Ensure the shadow registers are initialized. */
565 initialize_shadow_low(True);
sewardj3b290482011-05-06 21:02:55 +0000566 }
sewardj3b290482011-05-06 21:02:55 +0000567 if (annex == NULL) {
568 strcpy (arg_own_buf, "E00");
569 return;
570 }
571 }
572
573 {
philippe75a5f782012-02-24 11:25:58 +0000574 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1];
sewardj3b290482011-05-06 21:02:55 +0000575 struct vg_stat stat_doc;
576 char toread[len];
577 int len_read;
578
579 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
580 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
581 if (fd == -1) {
582 strcpy (arg_own_buf, "E00");
583 return;
584 }
585 if (VG_(fstat) (fd, &stat_doc) != 0) {
586 VG_(close) (fd);
587 strcpy (arg_own_buf, "E00");
588 return;
589 }
590 doc_len = stat_doc.size;
591
592 if (len > PBUFSIZ - POVERHSIZ)
593 len = PBUFSIZ - POVERHSIZ;
594
595 if (ofs > doc_len) {
596 write_enn (arg_own_buf);
597 VG_(close) (fd);
598 return;
599 }
600 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
601 len_read = VG_(read) (fd, toread, len);
florian1636d332012-11-15 04:27:04 +0000602 *new_packet_len_p = write_qxfer_response (arg_own_buf, (unsigned char *)toread,
sewardj3b290482011-05-06 21:02:55 +0000603 len_read, ofs + len_read < doc_len);
604 VG_(close) (fd);
605 return;
606 }
607 }
608
sewardj2a312392011-06-26 09:26:48 +0000609 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) {
610 unsigned char *data;
611 int n;
612 CORE_ADDR ofs;
613 unsigned int len;
614 char *annex;
615
616 /* Reject any annex; grab the offset and length. */
617 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0
618 || annex[0] != '\0') {
619 strcpy (arg_own_buf, "E00");
620 return;
621 }
622
623 if (len > PBUFSIZ - 2)
624 len = PBUFSIZ - 2;
625 data = malloc (len);
626
627 {
628 UWord *client_auxv = VG_(client_auxv);
629 unsigned int client_auxv_len = 0;
630 while (*client_auxv != 0) {
631 dlog(4, "auxv %lld %llx\n",
632 (ULong)*client_auxv,
633 (ULong)*(client_auxv+1));
634 client_auxv++;
635 client_auxv++;
636 client_auxv_len += 2 * sizeof(UWord);
637 }
638 client_auxv_len += 2 * sizeof(UWord);
639 dlog(4, "auxv len %d\n", client_auxv_len);
640
641 if (ofs >= client_auxv_len)
642 n = -1;
643 else {
644 n = client_auxv_len - ofs;
645 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n);
646 }
647 }
648
649 if (n < 0)
650 write_enn (arg_own_buf);
651 else if (n > len)
652 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
653 else
654 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
655
656 free (data);
657
658 return;
659 }
660
661
sewardj3b290482011-05-06 21:02:55 +0000662 /* Protocol features query. */
663 if (strncmp ("qSupported", arg_own_buf, 10) == 0
664 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
665 VG_(sprintf) (arg_own_buf, "PacketSize=%x", PBUFSIZ - 1);
666 /* Note: max packet size including frame and checksum, but without
667 trailing null byte, which is not sent/received. */
668
669 strcat (arg_own_buf, ";QStartNoAckMode+");
670 strcat (arg_own_buf, ";QPassSignals+");
sewardj2a312392011-06-26 09:26:48 +0000671 if (VG_(client_auxv))
672 strcat (arg_own_buf, ";qXfer:auxv:read+");
sewardj3b290482011-05-06 21:02:55 +0000673
philippe419d5f22012-05-24 21:33:17 +0000674 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) {
sewardj3b290482011-05-06 21:02:55 +0000675 strcat (arg_own_buf, ";qXfer:features:read+");
676 /* if a new gdb connects to us, we have to reset the register
677 set to the normal register sets to allow this new gdb to
678 decide to use or not the shadow registers.
679
680 Note that the reset is only done for gdb that are sending
681 qSupported packets. If a user first connected with a recent
682 gdb using shadow registers and then with a very old gdb
683 that does not use qSupported packet, then the old gdb will
684 not properly connect. */
685 initialize_shadow_low(False);
686 }
687 return;
688 }
689
690 /* Otherwise we didn't know what packet it was. Say we didn't
691 understand it. */
692 arg_own_buf[0] = 0;
693}
694
695/* Handle all of the extended 'v' packets. */
696static
sewardj0bb3c672011-07-26 23:29:25 +0000697void handle_v_requests (char *arg_own_buf, char *status, int *zignal)
sewardj3b290482011-05-06 21:02:55 +0000698{
699 /* vcont packet code from gdb 6.6 removed */
700
701 /* Otherwise we didn't know what packet it was. Say we didn't
702 understand it. */
703 arg_own_buf[0] = 0;
704 return;
705}
706
707static
708void myresume (int step, int sig)
709{
710 struct thread_resume resume_info[2];
711 int n = 0;
712
philippe349a3912012-05-23 21:50:36 +0000713 if (step || sig) {
sewardj3b290482011-05-06 21:02:55 +0000714 resume_info[0].step = step;
715 resume_info[0].sig = sig;
sewardj3b290482011-05-06 21:02:55 +0000716 n++;
717 }
sewardj3b290482011-05-06 21:02:55 +0000718 resume_info[n].step = 0;
719 resume_info[n].sig = 0;
sewardj3b290482011-05-06 21:02:55 +0000720
philippe349a3912012-05-23 21:50:36 +0000721 resume_reply_packet_needed = True;
722 valgrind_resume (resume_info);
sewardj3b290482011-05-06 21:02:55 +0000723}
724
725/* server_main global variables */
726static char *own_buf;
727static unsigned char *mem_buf;
728
729void gdbserver_init (void)
730{
731 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
732 noack_mode = False;
philippe349a3912012-05-23 21:50:36 +0000733 valgrind_initialize_target ();
philippe0e1cac92012-02-28 22:37:44 +0000734 // After a fork, gdbserver_init can be called again.
735 // We do not have to re-malloc the buffers in such a case.
736 if (own_buf == NULL)
737 own_buf = malloc (PBUFSIZ);
738 if (mem_buf == NULL)
739 mem_buf = malloc (PBUFSIZ);
sewardj3b290482011-05-06 21:02:55 +0000740}
741
742void gdbserver_terminate (void)
743{
744 /* last call to gdbserver is cleanup call */
745 if (VG_MINIMAL_SETJMP(toplevel)) {
746 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
747 return;
748 }
749 remote_close();
750}
751
752void server_main (void)
753{
754 static char status;
sewardj0bb3c672011-07-26 23:29:25 +0000755 static int zignal;
sewardj3b290482011-05-06 21:02:55 +0000756
757 char ch;
758 int i = 0;
759 unsigned int len;
760 CORE_ADDR mem_addr;
761
philippe349a3912012-05-23 21:50:36 +0000762 zignal = valgrind_wait (&status);
sewardj3b290482011-05-06 21:02:55 +0000763 if (VG_MINIMAL_SETJMP(toplevel)) {
764 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
765 }
766 while (1) {
767 unsigned char sig;
768 int packet_len;
769 int new_packet_len = -1;
770
philippe349a3912012-05-23 21:50:36 +0000771 if (resume_reply_packet_needed) {
772 /* Send the resume reply to reply to last GDB resume
773 request. */
774 resume_reply_packet_needed = False;
sewardj0bb3c672011-07-26 23:29:25 +0000775 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000776 putpkt (own_buf);
777 }
778
philippe0447bbd2012-10-17 21:32:03 +0000779 /* If we our status is terminal (exit or fatal signal) get out
780 as quickly as we can. We won't be able to handle any request
781 anymore. */
782 if (status == 'W' || status == 'X') {
783 return;
784 }
785
sewardj3b290482011-05-06 21:02:55 +0000786 packet_len = getpkt (own_buf);
787 if (packet_len <= 0)
788 break;
789
790 i = 0;
791 ch = own_buf[i++];
792 switch (ch) {
793 case 'Q':
794 handle_set (own_buf, &new_packet_len);
795 break;
796 case 'q':
797 handle_query (own_buf, &new_packet_len);
798 break;
799 case 'd':
800 /* set/unset debugging is done through valgrind debug level. */
801 own_buf[0] = '\0';
802 break;
803 case 'D':
804 reset_valgrind_sink("gdb detaching from process");
805
806 /* When detaching or kill the process, gdb expects to get
807 an packet OK back. Any other output will make gdb
808 believes detach did not work. */
809 write_ok (own_buf);
810 putpkt (own_buf);
811 remote_finish (reset_after_error);
812 remote_open (VG_(clo_vgdb_prefix));
813 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +0000814 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +0000815 return;
816 case '!':
817 /* We can not use the extended protocol with valgrind,
818 because we can not restart the running
819 program. So return unrecognized. */
820 own_buf[0] = '\0';
821 break;
822 case '?':
sewardj0bb3c672011-07-26 23:29:25 +0000823 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000824 break;
825 case 'H':
826 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
827 unsigned long gdb_id, thread_id;
828
829 gdb_id = strtoul (&own_buf[2], NULL, 16);
830 thread_id = gdb_id_to_thread_id (gdb_id);
831 if (thread_id == 0) {
832 write_enn (own_buf);
833 break;
834 }
835
836 if (own_buf[1] == 'g') {
837 general_thread = thread_id;
838 set_desired_inferior (1);
839 } else if (own_buf[1] == 'c') {
840 cont_thread = thread_id;
841 } else if (own_buf[1] == 's') {
842 step_thread = thread_id;
843 }
844
845 write_ok (own_buf);
846 } else {
847 /* Silently ignore it so that gdb can extend the protocol
848 without compatibility headaches. */
849 own_buf[0] = '\0';
850 }
851 break;
852 case 'g':
853 set_desired_inferior (1);
854 registers_to_string (own_buf);
855 break;
856 case 'G':
857 set_desired_inferior (1);
858 registers_from_string (&own_buf[1]);
859 write_ok (own_buf);
860 break;
861 case 'P': {
862 int regno;
863 char *regbytes;
864 Bool mod;
865 ThreadState *tst;
866 regno = strtol(&own_buf[1], NULL, 16);
867 regbytes = strchr(&own_buf[0], '=') + 1;
868 set_desired_inferior (1);
869 tst = (ThreadState *) inferior_target_data (current_inferior);
870 /* Only accept changing registers in "runnable state3.
871 In fact, it would be ok to change most of the registers
872 except a few "sensitive" registers such as the PC, SP, BP.
873 We assume we do not need to very specific here, and that we
874 can just refuse all of these. */
875 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
876 supply_register_from_string (regno, regbytes, &mod);
877 write_ok (own_buf);
878 } else {
879 /* at least from gdb 6.6 onwards, an E. error
880 reply is shown to the user. So, we do an error
881 msg which both is accepted by gdb as an error msg
882 and is readable by the user. */
883 VG_(sprintf)
884 (own_buf,
885"E.\n"
886"ERROR changing register %s regno %d\n"
887"gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
888"set pc, calling from gdb a function in the debugged process, ...)\n"
889"can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
890"Thread status is %s\n",
891 find_register_by_number (regno)->name, regno,
892 VG_(name_of_ThreadStatus)(tst->status));
893 if (VG_(clo_verbosity) > 1)
894 VG_(umsg) ("%s\n", own_buf);
895 }
896 break;
897 }
898 case 'm':
899 decode_m_packet (&own_buf[1], &mem_addr, &len);
philippe349a3912012-05-23 21:50:36 +0000900 if (valgrind_read_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +0000901 convert_int_to_ascii (mem_buf, own_buf, len);
902 else
903 write_enn (own_buf);
904 break;
905 case 'M':
906 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
philippe349a3912012-05-23 21:50:36 +0000907 if (valgrind_write_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +0000908 write_ok (own_buf);
909 else
910 write_enn (own_buf);
911 break;
912 case 'X':
913 if (decode_X_packet (&own_buf[1], packet_len - 1,
914 &mem_addr, &len, mem_buf) < 0
philippe349a3912012-05-23 21:50:36 +0000915 || valgrind_write_memory (mem_addr, mem_buf, len) != 0)
sewardj3b290482011-05-06 21:02:55 +0000916 write_enn (own_buf);
917 else
918 write_ok (own_buf);
919 break;
920 case 'C':
921 convert_ascii_to_int (own_buf + 1, &sig, 1);
922 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +0000923 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +0000924 else
sewardj0bb3c672011-07-26 23:29:25 +0000925 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +0000926 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +0000927 myresume (0, zignal);
sewardj3b290482011-05-06 21:02:55 +0000928 return; // return control to valgrind
929 case 'S':
930 convert_ascii_to_int (own_buf + 1, &sig, 1);
931 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +0000932 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +0000933 else
sewardj0bb3c672011-07-26 23:29:25 +0000934 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +0000935 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +0000936 myresume (1, zignal);
sewardj3b290482011-05-06 21:02:55 +0000937 return; // return control to valgrind
938 case 'c':
939 set_desired_inferior (0);
940 myresume (0, 0);
941 return; // return control to valgrind
942 case 's':
943 set_desired_inferior (0);
944 myresume (1, 0);
945 return; // return control to valgrind
946 case 'Z': {
947 char *lenptr;
948 char *dataptr;
949 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
950 int zlen = strtol (lenptr + 1, &dataptr, 16);
951 char type = own_buf[1];
952
philippe349a3912012-05-23 21:50:36 +0000953 if (type < '0' || type > '4') {
954 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +0000955 own_buf[0] = '\0';
956 } else {
957 int res;
958
philippe349a3912012-05-23 21:50:36 +0000959 res = valgrind_insert_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +0000960 if (res == 0)
961 write_ok (own_buf);
962 else if (res == 1)
963 /* Unsupported. */
964 own_buf[0] = '\0';
965 else
966 write_enn (own_buf);
967 }
968 break;
969 }
970 case 'z': {
971 char *lenptr;
972 char *dataptr;
973 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
974 int zlen = strtol (lenptr + 1, &dataptr, 16);
975 char type = own_buf[1];
976
philippe349a3912012-05-23 21:50:36 +0000977 if (type < '0' || type > '4') {
978 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +0000979 own_buf[0] = '\0';
980 } else {
981 int res;
982
philippe349a3912012-05-23 21:50:36 +0000983 res = valgrind_remove_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +0000984 if (res == 0)
985 write_ok (own_buf);
986 else if (res == 1)
987 /* Unsupported. */
988 own_buf[0] = '\0';
989 else
990 write_enn (own_buf);
991 }
992 break;
993 }
994 case 'k':
995 kill_request("Gdb request to kill this process\n");
996 break;
997 case 'T': {
998 unsigned long gdb_id, thread_id;
999
1000 gdb_id = strtoul (&own_buf[1], NULL, 16);
1001 thread_id = gdb_id_to_thread_id (gdb_id);
1002 if (thread_id == 0) {
1003 write_enn (own_buf);
1004 break;
1005 }
1006
philippe349a3912012-05-23 21:50:36 +00001007 if (valgrind_thread_alive (thread_id))
sewardj3b290482011-05-06 21:02:55 +00001008 write_ok (own_buf);
1009 else
1010 write_enn (own_buf);
1011 break;
1012 }
1013 case 'R':
1014 /* Restarting the inferior is only supported in the
1015 extended protocol.
1016 => It is a request we don't understand. Respond with an
1017 empty packet so that gdb knows that we don't support this
1018 request. */
1019 own_buf[0] = '\0';
1020 break;
1021 case 'v':
1022 /* Extended (long) request. */
sewardj0bb3c672011-07-26 23:29:25 +00001023 handle_v_requests (own_buf, &status, &zignal);
sewardj3b290482011-05-06 21:02:55 +00001024 break;
1025 default:
1026 /* It is a request we don't understand. Respond with an
1027 empty packet so that gdb knows that we don't support this
1028 request. */
1029 own_buf[0] = '\0';
1030 break;
1031 }
1032
1033 if (new_packet_len != -1)
1034 putpkt_binary (own_buf, new_packet_len);
1035 else
1036 putpkt (own_buf);
1037
1038 if (status == 'W')
sewardj0bb3c672011-07-26 23:29:25 +00001039 VG_(umsg) ("\nChild exited with status %d\n", zignal);
sewardj3b290482011-05-06 21:02:55 +00001040 if (status == 'X')
sewardj47ffedb2011-10-24 07:36:57 +00001041 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
sewardj0bb3c672011-07-26 23:29:25 +00001042 target_signal_to_host (zignal),
1043 target_signal_to_name (zignal));
sewardj3b290482011-05-06 21:02:55 +00001044 if (status == 'W' || status == 'X') {
1045 VG_(umsg) ("Process exiting\n");
1046 VG_(exit) (0);
1047 }
1048 }
1049
1050 /* We come here when getpkt fails => close the connection,
1051 and re-open. Then return control to valgrind.
1052 We return the control to valgrind as we assume that
1053 the connection was closed due to vgdb having finished
1054 to execute a command. */
1055 if (VG_(clo_verbosity) > 1)
1056 VG_(umsg) ("Remote side has terminated connection. "
1057 "GDBserver will reopen the connection.\n");
1058 remote_finish (reset_after_error);
1059 remote_open (VG_(clo_vgdb_prefix));
1060 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +00001061 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001062 return;
1063}