blob: ef18d7708507bf78667c0f73fdcb2a4c9dad5f31 [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"
sewardj3b290482011-05-06 21:02:55 +000030
31unsigned long cont_thread;
32unsigned long general_thread;
33unsigned long step_thread;
34unsigned long thread_from_wait;
35unsigned long old_thread_from_wait;
36
philippe886fde32012-03-29 21:56:47 +000037int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */
bart2dafc542011-05-18 16:08:28 +000038
sewardj3b290482011-05-06 21:02:55 +000039/* for a gdbserver integrated in valgrind, resuming the process consists
40 in returning the control to valgrind.
philippe349a3912012-05-23 21:50:36 +000041 The guess process resumes its execution.
sewardj3b290482011-05-06 21:02:55 +000042 Then at the next error or break or ..., valgrind calls gdbserver again.
philippe349a3912012-05-23 21:50:36 +000043 A resume reply packet must then be built to inform GDB that the
44 resume request is finished.
45 resume_reply_packet_needed records the fact that the next call to gdbserver
sewardj3b290482011-05-06 21:02:55 +000046 must send a resume packet to gdb. */
philippe349a3912012-05-23 21:50:36 +000047static Bool resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +000048
49VG_MINIMAL_JMP_BUF(toplevel);
50
51/* Decode a qXfer read request. Return 0 if everything looks OK,
52 or -1 otherwise. */
53
54static
55int decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
56{
57 /* Extract and NUL-terminate the annex. */
58 *annex = buf;
59 while (*buf && *buf != ':')
60 buf++;
61 if (*buf == '\0')
62 return -1;
63 *buf++ = 0;
64
65 /* After the read/write marker and annex, qXfer looks like a
66 traditional 'm' packet. */
67 decode_m_packet (buf, ofs, len);
68
69 return 0;
70}
71
72/* Write the response to a successful qXfer read. Returns the
73 length of the (binary) data stored in BUF, corresponding
74 to as much of DATA/LEN as we could fit. IS_MORE controls
75 the first character of the response. */
76static
77int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
78{
79 int out_len;
80
81 if (is_more)
82 buf[0] = 'm';
83 else
84 buf[0] = 'l';
85
86 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
87 PBUFSIZ - POVERHSIZ - 1) + 1;
88}
89
90static Bool initial_valgrind_sink_saved = False;
91/* True <=> valgrind log sink saved in initial_valgrind_sink */
92static OutputSink initial_valgrind_sink;
93
94static Bool command_output_to_log = False;
95/* True <=> command output goes to log instead of gdb */
96
97void reset_valgrind_sink(char *info)
98{
99 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd
100 && initial_valgrind_sink_saved) {
101 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
102 VG_(umsg) ("Reset valgrind output to log (%s)\n",
103 (info = NULL ? "" : info));
104 }
105}
106
107static
108void kill_request (char *msg)
109{
110 VG_(umsg) ("%s", msg);
111 remote_close();
112 VG_(exit) (0);
113}
114
115/* handle_gdb_valgrind_command handles the provided mon string command.
116 If command is recognised, return 1 else return 0.
117 Note that in case of ambiguous command, 1 is returned.
118
119 *sink_wanted_at_return is modified if one of the commands
sewardj30b3eca2011-06-28 08:20:39 +0000120 'v.set *_output' is handled.
sewardj3b290482011-05-06 21:02:55 +0000121*/
122static
123int handle_gdb_valgrind_command (char* mon, OutputSink* sink_wanted_at_return)
124{
125 UWord ret = 0;
126 char s[strlen(mon)+1]; /* copy for strtok_r */
127 char* wcmd;
128 Char* ssaveptr;
129 char* endptr;
130 int kwdid;
131 int int_value;
132
133 vg_assert (initial_valgrind_sink_saved);
134
135 strcpy (s, mon);
136 wcmd = strtok_r (s, " ", &ssaveptr);
137 /* NB: if possible, avoid introducing a new command below which
sewardj30b3eca2011-06-28 08:20:39 +0000138 starts with the same 3 first letters as an already existing
sewardj3b290482011-05-06 21:02:55 +0000139 command. This ensures a shorter abbreviation for the user. */
sewardj30b3eca2011-06-28 08:20:39 +0000140 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate",
sewardj3b290482011-05-06 21:02:55 +0000141 wcmd, kwd_report_duplicated_matches)) {
142 case -2:
143 ret = 1;
144 break;
145 case -1:
146 break;
147 case 0: /* help */
148 ret = 1;
149 wcmd = strtok_r (NULL, " ", &ssaveptr);
150 if (wcmd == NULL) {
151 int_value = 0;
152 } else {
153 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) {
154 case -2: int_value = 0; break;
155 case -1: int_value = 0; break;
156 case 0: int_value = 1; break;
157 default: tl_assert (0);
158 }
159 }
160
161 VG_(gdb_printf) (
162"general valgrind monitor commands:\n"
163" help [debug] : monitor command help. With debug: + debugging commands\n"
sewardj30b3eca2011-06-28 08:20:39 +0000164" v.wait [<ms>] : sleep <ms> (default 0) then continue\n"
165" v.info all_errors : show all errors found so far\n"
166" v.info last_error : show last error found\n"
167" v.info n_errs_found : show the nr of errors found so far\n"
168" v.kill : kill the Valgrind process\n"
169" v.set gdb_output : set valgrind output to gdb\n"
170" v.set log_output : set valgrind output to log\n"
171" v.set mixed_output : set valgrind output to log, interactive output to gdb\n"
172" v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
sewardj3b290482011-05-06 21:02:55 +0000173 if (int_value) { VG_(gdb_printf) (
174"debugging valgrind internals monitor commands:\n"
sewardj30b3eca2011-06-28 08:20:39 +0000175" v.info gdbserver_status : show gdbserver status\n"
philippe93a6a8d2012-04-27 22:59:43 +0000176" v.info memory [aspacemgr] : show valgrind heap memory stats\n"
177" (with aspacemgr arg, also shows valgrind segments on log ouput)\n"
sewardjd6e13d82011-10-22 20:23:30 +0000178" v.info scheduler : show valgrind thread state and stacktrace\n"
sewardj30b3eca2011-06-28 08:20:39 +0000179" v.set debuglog <level> : set valgrind debug log level to <level>\n"
180" v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
sewardj3b290482011-05-06 21:02:55 +0000181" (default traceflags 0b00100000 : show after instrumentation)\n"
182" An additional flag 0b100000000 allows to show gdbserver instrumentation\n");
183 }
184 break;
sewardj30b3eca2011-06-28 08:20:39 +0000185 case 1: /* v.set */
sewardj3b290482011-05-06 21:02:55 +0000186 ret = 1;
187 wcmd = strtok_r (NULL, " ", &ssaveptr);
188 switch (kwdid = VG_(keyword_id)
189 ("vgdb-error debuglog gdb_output log_output mixed_output",
190 wcmd, kwd_report_all)) {
191 case -2:
192 case -1:
193 break;
194 case 0: /* vgdb-error */
195 case 1: /* debuglog */
196 wcmd = strtok_r (NULL, " ", &ssaveptr);
197 if (wcmd == NULL) {
198 int_value = 0;
199 endptr = "empty"; /* to report an error below */
200 } else {
201 int_value = strtol (wcmd, &endptr, 10);
202 }
203 if (*endptr != '\0') {
204 VG_(gdb_printf) ("missing or malformed integer value\n");
205 } else if (kwdid == 0) {
206 VG_(gdb_printf) ("vgdb-error value changed from %d to %d\n",
207 VG_(dyn_vgdb_error), int_value);
208 VG_(dyn_vgdb_error) = int_value;
209 } else if (kwdid == 1) {
210 VG_(gdb_printf) ("debuglog value changed from %d to %d\n",
211 VG_(debugLog_getLevel)(), int_value);
212 VG_(debugLog_startup) (int_value, "gdbsrv");
213 } else {
214 vg_assert (0);
215 }
216 break;
217 case 2: /* gdb_output */
218 (*sink_wanted_at_return).fd = -2;
219 command_output_to_log = False;
220 VG_(gdb_printf) ("valgrind output will go to gdb\n");
221 break;
222 case 3: /* log_output */
223 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
224 command_output_to_log = True;
225 VG_(gdb_printf) ("valgrind output will go to log\n");
226 break;
227 case 4: /* mixed output */
228 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
229 command_output_to_log = False;
230 VG_(gdb_printf)
231 ("valgrind output will go to log, interactive output will go to gdb\n");
232 break;
233 default:
234 vg_assert (0);
235 }
236 break;
sewardj30b3eca2011-06-28 08:20:39 +0000237 case 2: /* v.info */ {
sewardj3b290482011-05-06 21:02:55 +0000238 ret = 1;
239 wcmd = strtok_r (NULL, " ", &ssaveptr);
240 switch (kwdid = VG_(keyword_id)
sewardjd6e13d82011-10-22 20:23:30 +0000241 ("all_errors n_errs_found last_error gdbserver_status memory"
242 " scheduler",
sewardj3b290482011-05-06 21:02:55 +0000243 wcmd, kwd_report_all)) {
244 case -2:
245 case -1:
246 break;
247 case 0: // all_errors
248 // A verbosity of minimum 2 is needed to show the errors.
249 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False);
250 break;
251 case 1: // n_errs_found
philippebaf69642012-02-15 22:29:30 +0000252 VG_(gdb_printf) ("n_errs_found %d n_errs_shown %d (vgdb-error %d)\n",
sewardj3b290482011-05-06 21:02:55 +0000253 VG_(get_n_errs_found) (),
philippebaf69642012-02-15 22:29:30 +0000254 VG_(get_n_errs_shown) (),
sewardj3b290482011-05-06 21:02:55 +0000255 VG_(dyn_vgdb_error));
256 break;
257 case 2: // last_error
258 VG_(show_last_error)();
259 break;
260 case 3: // gdbserver_status
261 VG_(gdbserver_status_output)();
262 break;
263 case 4: /* memory */
264 VG_(print_all_arena_stats) ();
265 if (VG_(clo_profile_heap))
266 VG_(print_arena_cc_analysis) ();
philippe93a6a8d2012-04-27 22:59:43 +0000267 wcmd = strtok_r (NULL, " ", &ssaveptr);
268 if (wcmd != NULL) {
269 switch (VG_(keyword_id) ("aspacemgr", wcmd, kwd_report_all)) {
270 case -2:
271 case -1: break;
272 case 0:
273 VG_(am_show_nsegments) (0, "gdbserver v.info memory aspacemgr");
274 break;
275 default: tl_assert (0);
276 }
277 }
278
sewardj3b290482011-05-06 21:02:55 +0000279 ret = 1;
280 break;
sewardjd6e13d82011-10-22 20:23:30 +0000281 case 5: /* scheduler */
282 VG_(show_sched_status) ();
283 ret = 1;
284 break;
sewardj3b290482011-05-06 21:02:55 +0000285 default:
286 vg_assert(0);
287 }
288 break;
289 }
sewardj30b3eca2011-06-28 08:20:39 +0000290 case 3: /* v.wait */
sewardj3b290482011-05-06 21:02:55 +0000291 wcmd = strtok_r (NULL, " ", &ssaveptr);
292 if (wcmd != NULL) {
293 int_value = strtol (wcmd, &endptr, 10);
294 VG_(gdb_printf) ("gdbserver: continuing in %d ms ...\n", int_value);
295 VG_(poll)(NULL, 0, int_value);
296 }
297 VG_(gdb_printf) ("gdbserver: continuing after wait ...\n");
298 ret = 1;
299 break;
sewardj30b3eca2011-06-28 08:20:39 +0000300 case 4: /* v.kill */
sewardj3b290482011-05-06 21:02:55 +0000301 kill_request ("monitor command request to kill this process\n");
302 break;
sewardj30b3eca2011-06-28 08:20:39 +0000303 case 5: { /* v.translate */
sewardj3b290482011-05-06 21:02:55 +0000304 Addr address;
305 SizeT verbosity = 0x20;
306
307 ret = 1;
308
309 VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr);
310 if (address != (Addr) 0 || verbosity != 0) {
311 /* we need to force the output to log for the translation trace,
312 as low level VEX tracing cannot be redirected to gdb. */
313 int saved_command_output_to_log = command_output_to_log;
314 int saved_fd = VG_(log_output_sink).fd;
315 Bool single_stepping_on_entry = valgrind_single_stepping();
316 int vex_verbosity = verbosity & 0xff;
317 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
318 if ((verbosity & 0x100) && !single_stepping_on_entry) {
319 valgrind_set_single_stepping(True);
320 // to force gdbserver instrumentation.
321 }
sewardj99d61342011-05-17 16:35:11 +0000322# if defined(VGA_arm)
323 // on arm, we need to (potentially) convert this address
324 // to the thumb form.
325 address = thumb_pc (address);
326# endif
327
sewardj3b290482011-05-06 21:02:55 +0000328 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
329 address,
330 /*debugging*/True,
331 (Int) vex_verbosity,
332 /*bbs_done*/0,
333 /*allow redir?*/True);
334 if ((verbosity & 0x100) && !single_stepping_on_entry) {
335 valgrind_set_single_stepping(False);
336 // reset single stepping.
337 }
338 command_output_to_log = saved_command_output_to_log;
339 VG_(log_output_sink).fd = saved_fd;
340 }
341 break;
342 }
343
344 default:
345 vg_assert (0);
346 }
347 return ret;
348}
349
350/* handle_gdb_monitor_command handles the provided mon string command,
351 which can be either a "standard" valgrind monitor command
352 or a tool specific monitor command.
353 If command recognised, return 1 else return 0.
354 Note that in case of ambiguous command, 1 is returned.
355*/
356static
357int handle_gdb_monitor_command (char* mon)
358{
359 UWord ret = 0;
360 UWord tool_ret = 0;
361 // initially, we assume that when returning, the desired sink is the
362 // one we have when entering. It can however be changed by the standard
363 // valgrind command handling.
364 OutputSink sink_wanted_at_return = VG_(log_output_sink);
365
366 if (!initial_valgrind_sink_saved) {
367 /* first time we enter here, we save the valgrind default log sink */
368 initial_valgrind_sink = sink_wanted_at_return;
369 initial_valgrind_sink_saved = True;
370 }
371
372 if (!command_output_to_log)
373 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */
374
375 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return);
376
377 /* Even if command was recognised by valgrind core, we call the
378 tool command handler : this is needed to handle help command
379 and/or to let the tool do some additional processing of a
380 valgrind standard command. Note however that if valgrind
381 recognised the command, we will always return success. */
382 if (VG_(needs).client_requests) {
383 /* If the tool reports an error when handling a monitor command,
384 we need to avoid calling gdbserver during this command
385 handling. So, we temporarily set VG_(dyn_vgdb_error) to
386 a huge value to ensure m_errormgr.c does not call gdbserver. */
387 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error);
388 UWord arg[2];
389 VG_(dyn_vgdb_error) = 999999999;
390 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND;
391 arg[1] = (UWord) mon;
392 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg,
393 &tool_ret);
394 VG_(dyn_vgdb_error) = save_dyn_vgdb_error;
395 }
396
397 /* restore or set the desired output */
398 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
399 if (ret | tool_ret)
400 return 1;
401 else
402 return 0;
403}
404
405
406/* Handle all of the extended 'Q' packets. */
407static
408void handle_set (char *arg_own_buf, int *new_packet_len_p)
409{
410 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) {
411 noack_mode = True;
412 write_ok (arg_own_buf);
413 return;
414 }
415
416 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
417 int i;
418 char *from, *to;
419 char *end = arg_own_buf + strlen(arg_own_buf);
420 CORE_ADDR sig;
421 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
422 pass_signals[i] = 0;
423
424 from = arg_own_buf + 13;
425 while (from < end) {
426 to = strchr(from, ';');
427 if (to == NULL) to = end;
428 decode_address (&sig, from, to - from);
429 pass_signals[(int)sig] = 1;
philippe886fde32012-03-29 21:56:47 +0000430 dlog(1, "pass_signal gdb_nr %d %s\n",
431 (int)sig, target_signal_to_name(sig));
sewardj3b290482011-05-06 21:02:55 +0000432 from = to;
433 if (*from == ';') from++;
434 }
435 write_ok (arg_own_buf);
436 return;
437 }
438 /* Otherwise we didn't know what packet it was. Say we didn't
439 understand it. */
440 arg_own_buf[0] = 0;
441}
442
443/* Handle all of the extended 'q' packets. */
444static
445void handle_query (char *arg_own_buf, int *new_packet_len_p)
446{
447 static struct inferior_list_entry *thread_ptr;
448
449 /* qRcmd, monitor command handling. */
450 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
451 char *p = arg_own_buf + 6;
452 int cmdlen = strlen(p)/2;
453 char cmd[cmdlen+1];
454
455 if (unhexify (cmd, p, cmdlen) != cmdlen) {
456 write_enn (arg_own_buf);
457 return;
458 }
459 cmd[cmdlen] = '\0';
460
461 if (handle_gdb_monitor_command (cmd)) {
462 /* In case the command is from a standalone vgdb,
463 connection will be closed soon => flush the output. */
464 VG_(message_flush) ();
465 write_ok (arg_own_buf);
466 return;
467 } else {
468 /* cmd not recognised */
469 VG_(gdb_printf)
470 ("command '%s' not recognised\n"
471 "In gdb, try 'monitor help'\n"
472 "In a shell, try 'vgdb help'\n",
473 cmd);
474 write_ok (arg_own_buf);
475 return;
476 }
477 }
478
479 /* provide some valgrind specific info in return to qThreadExtraInfo. */
480 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
481 unsigned long gdb_id;
482 struct thread_info *ti;
483 ThreadState *tst;
484 char status[100];
485
486 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
487 ti = gdb_id_to_thread (gdb_id);
488 if (ti != NULL) {
489 tst = (ThreadState *) inferior_target_data (ti);
490 /* Additional info is the tid and the thread status. */
491 VG_(snprintf) (status, sizeof(status), "tid %d %s",
492 tst->tid,
493 VG_(name_of_ThreadStatus)(tst->status));
494 hexify (arg_own_buf, status, strlen(status));
495 return;
496 } else {
497 write_enn (arg_own_buf);
498 return;
499 }
500 }
501
502 if (strcmp ("qAttached", arg_own_buf) == 0) {
503 /* tell gdb to always detach, never kill the process */
504 arg_own_buf[0] = '1';
505 arg_own_buf[1] = 0;
506 return;
507 }
508
509 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
510 /* We have no symbol to read. */
511 write_ok (arg_own_buf);
512 return;
513 }
514
515 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
516 thread_ptr = all_threads.head;
517 VG_(sprintf) (arg_own_buf, "m%x",
518 thread_to_gdb_id ((struct thread_info *)thread_ptr));
519 thread_ptr = thread_ptr->next;
520 return;
521 }
522
523 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
524 if (thread_ptr != NULL) {
525 VG_(sprintf) (arg_own_buf, "m%x",
526 thread_to_gdb_id ((struct thread_info *)thread_ptr));
527 thread_ptr = thread_ptr->next;
528 return;
529 } else {
530 VG_(sprintf) (arg_own_buf, "l");
531 return;
532 }
533 }
534
philippe419d5f22012-05-24 21:33:17 +0000535 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL
sewardj3b290482011-05-06 21:02:55 +0000536 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
537 CORE_ADDR ofs;
538 unsigned int len, doc_len;
539 char *annex = NULL;
540 // First, the annex is extracted from the packet received.
541 // Then, it is replaced by the corresponding file name.
542 int fd;
543
544 /* Grab the annex, offset, and length. */
545 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
546 strcpy (arg_own_buf, "E00");
547 return;
548 }
549
550 if (strcmp (annex, "target.xml") == 0) {
philippe419d5f22012-05-24 21:33:17 +0000551 annex = valgrind_target_xml(VG_(clo_vgdb_shadow_registers));
552 if (annex != NULL && VG_(clo_vgdb_shadow_registers)) {
553 /* Ensure the shadow registers are initialized. */
554 initialize_shadow_low(True);
sewardj3b290482011-05-06 21:02:55 +0000555 }
sewardj3b290482011-05-06 21:02:55 +0000556 if (annex == NULL) {
557 strcpy (arg_own_buf, "E00");
558 return;
559 }
560 }
561
562 {
philippe75a5f782012-02-24 11:25:58 +0000563 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1];
sewardj3b290482011-05-06 21:02:55 +0000564 struct vg_stat stat_doc;
565 char toread[len];
566 int len_read;
567
568 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
569 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
570 if (fd == -1) {
571 strcpy (arg_own_buf, "E00");
572 return;
573 }
574 if (VG_(fstat) (fd, &stat_doc) != 0) {
575 VG_(close) (fd);
576 strcpy (arg_own_buf, "E00");
577 return;
578 }
579 doc_len = stat_doc.size;
580
581 if (len > PBUFSIZ - POVERHSIZ)
582 len = PBUFSIZ - POVERHSIZ;
583
584 if (ofs > doc_len) {
585 write_enn (arg_own_buf);
586 VG_(close) (fd);
587 return;
588 }
589 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
590 len_read = VG_(read) (fd, toread, len);
591 *new_packet_len_p = write_qxfer_response (arg_own_buf, toread,
592 len_read, ofs + len_read < doc_len);
593 VG_(close) (fd);
594 return;
595 }
596 }
597
sewardj2a312392011-06-26 09:26:48 +0000598 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) {
599 unsigned char *data;
600 int n;
601 CORE_ADDR ofs;
602 unsigned int len;
603 char *annex;
604
605 /* Reject any annex; grab the offset and length. */
606 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0
607 || annex[0] != '\0') {
608 strcpy (arg_own_buf, "E00");
609 return;
610 }
611
612 if (len > PBUFSIZ - 2)
613 len = PBUFSIZ - 2;
614 data = malloc (len);
615
616 {
617 UWord *client_auxv = VG_(client_auxv);
618 unsigned int client_auxv_len = 0;
619 while (*client_auxv != 0) {
620 dlog(4, "auxv %lld %llx\n",
621 (ULong)*client_auxv,
622 (ULong)*(client_auxv+1));
623 client_auxv++;
624 client_auxv++;
625 client_auxv_len += 2 * sizeof(UWord);
626 }
627 client_auxv_len += 2 * sizeof(UWord);
628 dlog(4, "auxv len %d\n", client_auxv_len);
629
630 if (ofs >= client_auxv_len)
631 n = -1;
632 else {
633 n = client_auxv_len - ofs;
634 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n);
635 }
636 }
637
638 if (n < 0)
639 write_enn (arg_own_buf);
640 else if (n > len)
641 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
642 else
643 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
644
645 free (data);
646
647 return;
648 }
649
650
sewardj3b290482011-05-06 21:02:55 +0000651 /* Protocol features query. */
652 if (strncmp ("qSupported", arg_own_buf, 10) == 0
653 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
654 VG_(sprintf) (arg_own_buf, "PacketSize=%x", PBUFSIZ - 1);
655 /* Note: max packet size including frame and checksum, but without
656 trailing null byte, which is not sent/received. */
657
658 strcat (arg_own_buf, ";QStartNoAckMode+");
659 strcat (arg_own_buf, ";QPassSignals+");
sewardj2a312392011-06-26 09:26:48 +0000660 if (VG_(client_auxv))
661 strcat (arg_own_buf, ";qXfer:auxv:read+");
sewardj3b290482011-05-06 21:02:55 +0000662
philippe419d5f22012-05-24 21:33:17 +0000663 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) {
sewardj3b290482011-05-06 21:02:55 +0000664 strcat (arg_own_buf, ";qXfer:features:read+");
665 /* if a new gdb connects to us, we have to reset the register
666 set to the normal register sets to allow this new gdb to
667 decide to use or not the shadow registers.
668
669 Note that the reset is only done for gdb that are sending
670 qSupported packets. If a user first connected with a recent
671 gdb using shadow registers and then with a very old gdb
672 that does not use qSupported packet, then the old gdb will
673 not properly connect. */
674 initialize_shadow_low(False);
675 }
676 return;
677 }
678
679 /* Otherwise we didn't know what packet it was. Say we didn't
680 understand it. */
681 arg_own_buf[0] = 0;
682}
683
684/* Handle all of the extended 'v' packets. */
685static
sewardj0bb3c672011-07-26 23:29:25 +0000686void handle_v_requests (char *arg_own_buf, char *status, int *zignal)
sewardj3b290482011-05-06 21:02:55 +0000687{
688 /* vcont packet code from gdb 6.6 removed */
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 return;
694}
695
696static
697void myresume (int step, int sig)
698{
699 struct thread_resume resume_info[2];
700 int n = 0;
701
philippe349a3912012-05-23 21:50:36 +0000702 if (step || sig) {
sewardj3b290482011-05-06 21:02:55 +0000703 resume_info[0].step = step;
704 resume_info[0].sig = sig;
sewardj3b290482011-05-06 21:02:55 +0000705 n++;
706 }
sewardj3b290482011-05-06 21:02:55 +0000707 resume_info[n].step = 0;
708 resume_info[n].sig = 0;
sewardj3b290482011-05-06 21:02:55 +0000709
philippe349a3912012-05-23 21:50:36 +0000710 resume_reply_packet_needed = True;
711 valgrind_resume (resume_info);
sewardj3b290482011-05-06 21:02:55 +0000712}
713
714/* server_main global variables */
715static char *own_buf;
716static unsigned char *mem_buf;
717
718void gdbserver_init (void)
719{
720 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
721 noack_mode = False;
philippe349a3912012-05-23 21:50:36 +0000722 valgrind_initialize_target ();
philippe0e1cac92012-02-28 22:37:44 +0000723 // After a fork, gdbserver_init can be called again.
724 // We do not have to re-malloc the buffers in such a case.
725 if (own_buf == NULL)
726 own_buf = malloc (PBUFSIZ);
727 if (mem_buf == NULL)
728 mem_buf = malloc (PBUFSIZ);
sewardj3b290482011-05-06 21:02:55 +0000729}
730
731void gdbserver_terminate (void)
732{
733 /* last call to gdbserver is cleanup call */
734 if (VG_MINIMAL_SETJMP(toplevel)) {
735 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
736 return;
737 }
738 remote_close();
739}
740
741void server_main (void)
742{
743 static char status;
sewardj0bb3c672011-07-26 23:29:25 +0000744 static int zignal;
sewardj3b290482011-05-06 21:02:55 +0000745
746 char ch;
747 int i = 0;
748 unsigned int len;
749 CORE_ADDR mem_addr;
750
philippe349a3912012-05-23 21:50:36 +0000751 zignal = valgrind_wait (&status);
sewardj3b290482011-05-06 21:02:55 +0000752 if (VG_MINIMAL_SETJMP(toplevel)) {
753 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
754 }
755 while (1) {
756 unsigned char sig;
757 int packet_len;
758 int new_packet_len = -1;
759
philippe349a3912012-05-23 21:50:36 +0000760 if (resume_reply_packet_needed) {
761 /* Send the resume reply to reply to last GDB resume
762 request. */
763 resume_reply_packet_needed = False;
sewardj0bb3c672011-07-26 23:29:25 +0000764 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000765 putpkt (own_buf);
766 }
767
philippe0447bbd2012-10-17 21:32:03 +0000768 /* If we our status is terminal (exit or fatal signal) get out
769 as quickly as we can. We won't be able to handle any request
770 anymore. */
771 if (status == 'W' || status == 'X') {
772 return;
773 }
774
sewardj3b290482011-05-06 21:02:55 +0000775 packet_len = getpkt (own_buf);
776 if (packet_len <= 0)
777 break;
778
779 i = 0;
780 ch = own_buf[i++];
781 switch (ch) {
782 case 'Q':
783 handle_set (own_buf, &new_packet_len);
784 break;
785 case 'q':
786 handle_query (own_buf, &new_packet_len);
787 break;
788 case 'd':
789 /* set/unset debugging is done through valgrind debug level. */
790 own_buf[0] = '\0';
791 break;
792 case 'D':
793 reset_valgrind_sink("gdb detaching from process");
794
795 /* When detaching or kill the process, gdb expects to get
796 an packet OK back. Any other output will make gdb
797 believes detach did not work. */
798 write_ok (own_buf);
799 putpkt (own_buf);
800 remote_finish (reset_after_error);
801 remote_open (VG_(clo_vgdb_prefix));
802 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +0000803 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +0000804 return;
805 case '!':
806 /* We can not use the extended protocol with valgrind,
807 because we can not restart the running
808 program. So return unrecognized. */
809 own_buf[0] = '\0';
810 break;
811 case '?':
sewardj0bb3c672011-07-26 23:29:25 +0000812 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000813 break;
814 case 'H':
815 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
816 unsigned long gdb_id, thread_id;
817
818 gdb_id = strtoul (&own_buf[2], NULL, 16);
819 thread_id = gdb_id_to_thread_id (gdb_id);
820 if (thread_id == 0) {
821 write_enn (own_buf);
822 break;
823 }
824
825 if (own_buf[1] == 'g') {
826 general_thread = thread_id;
827 set_desired_inferior (1);
828 } else if (own_buf[1] == 'c') {
829 cont_thread = thread_id;
830 } else if (own_buf[1] == 's') {
831 step_thread = thread_id;
832 }
833
834 write_ok (own_buf);
835 } else {
836 /* Silently ignore it so that gdb can extend the protocol
837 without compatibility headaches. */
838 own_buf[0] = '\0';
839 }
840 break;
841 case 'g':
842 set_desired_inferior (1);
843 registers_to_string (own_buf);
844 break;
845 case 'G':
846 set_desired_inferior (1);
847 registers_from_string (&own_buf[1]);
848 write_ok (own_buf);
849 break;
850 case 'P': {
851 int regno;
852 char *regbytes;
853 Bool mod;
854 ThreadState *tst;
855 regno = strtol(&own_buf[1], NULL, 16);
856 regbytes = strchr(&own_buf[0], '=') + 1;
857 set_desired_inferior (1);
858 tst = (ThreadState *) inferior_target_data (current_inferior);
859 /* Only accept changing registers in "runnable state3.
860 In fact, it would be ok to change most of the registers
861 except a few "sensitive" registers such as the PC, SP, BP.
862 We assume we do not need to very specific here, and that we
863 can just refuse all of these. */
864 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
865 supply_register_from_string (regno, regbytes, &mod);
866 write_ok (own_buf);
867 } else {
868 /* at least from gdb 6.6 onwards, an E. error
869 reply is shown to the user. So, we do an error
870 msg which both is accepted by gdb as an error msg
871 and is readable by the user. */
872 VG_(sprintf)
873 (own_buf,
874"E.\n"
875"ERROR changing register %s regno %d\n"
876"gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
877"set pc, calling from gdb a function in the debugged process, ...)\n"
878"can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
879"Thread status is %s\n",
880 find_register_by_number (regno)->name, regno,
881 VG_(name_of_ThreadStatus)(tst->status));
882 if (VG_(clo_verbosity) > 1)
883 VG_(umsg) ("%s\n", own_buf);
884 }
885 break;
886 }
887 case 'm':
888 decode_m_packet (&own_buf[1], &mem_addr, &len);
philippe349a3912012-05-23 21:50:36 +0000889 if (valgrind_read_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +0000890 convert_int_to_ascii (mem_buf, own_buf, len);
891 else
892 write_enn (own_buf);
893 break;
894 case 'M':
895 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
philippe349a3912012-05-23 21:50:36 +0000896 if (valgrind_write_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +0000897 write_ok (own_buf);
898 else
899 write_enn (own_buf);
900 break;
901 case 'X':
902 if (decode_X_packet (&own_buf[1], packet_len - 1,
903 &mem_addr, &len, mem_buf) < 0
philippe349a3912012-05-23 21:50:36 +0000904 || valgrind_write_memory (mem_addr, mem_buf, len) != 0)
sewardj3b290482011-05-06 21:02:55 +0000905 write_enn (own_buf);
906 else
907 write_ok (own_buf);
908 break;
909 case 'C':
910 convert_ascii_to_int (own_buf + 1, &sig, 1);
911 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +0000912 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +0000913 else
sewardj0bb3c672011-07-26 23:29:25 +0000914 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +0000915 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +0000916 myresume (0, zignal);
sewardj3b290482011-05-06 21:02:55 +0000917 return; // return control to valgrind
918 case 'S':
919 convert_ascii_to_int (own_buf + 1, &sig, 1);
920 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +0000921 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +0000922 else
sewardj0bb3c672011-07-26 23:29:25 +0000923 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +0000924 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +0000925 myresume (1, zignal);
sewardj3b290482011-05-06 21:02:55 +0000926 return; // return control to valgrind
927 case 'c':
928 set_desired_inferior (0);
929 myresume (0, 0);
930 return; // return control to valgrind
931 case 's':
932 set_desired_inferior (0);
933 myresume (1, 0);
934 return; // return control to valgrind
935 case 'Z': {
936 char *lenptr;
937 char *dataptr;
938 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
939 int zlen = strtol (lenptr + 1, &dataptr, 16);
940 char type = own_buf[1];
941
philippe349a3912012-05-23 21:50:36 +0000942 if (type < '0' || type > '4') {
943 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +0000944 own_buf[0] = '\0';
945 } else {
946 int res;
947
philippe349a3912012-05-23 21:50:36 +0000948 res = valgrind_insert_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +0000949 if (res == 0)
950 write_ok (own_buf);
951 else if (res == 1)
952 /* Unsupported. */
953 own_buf[0] = '\0';
954 else
955 write_enn (own_buf);
956 }
957 break;
958 }
959 case 'z': {
960 char *lenptr;
961 char *dataptr;
962 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
963 int zlen = strtol (lenptr + 1, &dataptr, 16);
964 char type = own_buf[1];
965
philippe349a3912012-05-23 21:50:36 +0000966 if (type < '0' || type > '4') {
967 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +0000968 own_buf[0] = '\0';
969 } else {
970 int res;
971
philippe349a3912012-05-23 21:50:36 +0000972 res = valgrind_remove_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +0000973 if (res == 0)
974 write_ok (own_buf);
975 else if (res == 1)
976 /* Unsupported. */
977 own_buf[0] = '\0';
978 else
979 write_enn (own_buf);
980 }
981 break;
982 }
983 case 'k':
984 kill_request("Gdb request to kill this process\n");
985 break;
986 case 'T': {
987 unsigned long gdb_id, thread_id;
988
989 gdb_id = strtoul (&own_buf[1], NULL, 16);
990 thread_id = gdb_id_to_thread_id (gdb_id);
991 if (thread_id == 0) {
992 write_enn (own_buf);
993 break;
994 }
995
philippe349a3912012-05-23 21:50:36 +0000996 if (valgrind_thread_alive (thread_id))
sewardj3b290482011-05-06 21:02:55 +0000997 write_ok (own_buf);
998 else
999 write_enn (own_buf);
1000 break;
1001 }
1002 case 'R':
1003 /* Restarting the inferior is only supported in the
1004 extended protocol.
1005 => It is a request we don't understand. Respond with an
1006 empty packet so that gdb knows that we don't support this
1007 request. */
1008 own_buf[0] = '\0';
1009 break;
1010 case 'v':
1011 /* Extended (long) request. */
sewardj0bb3c672011-07-26 23:29:25 +00001012 handle_v_requests (own_buf, &status, &zignal);
sewardj3b290482011-05-06 21:02:55 +00001013 break;
1014 default:
1015 /* It is a request we don't understand. Respond with an
1016 empty packet so that gdb knows that we don't support this
1017 request. */
1018 own_buf[0] = '\0';
1019 break;
1020 }
1021
1022 if (new_packet_len != -1)
1023 putpkt_binary (own_buf, new_packet_len);
1024 else
1025 putpkt (own_buf);
1026
1027 if (status == 'W')
sewardj0bb3c672011-07-26 23:29:25 +00001028 VG_(umsg) ("\nChild exited with status %d\n", zignal);
sewardj3b290482011-05-06 21:02:55 +00001029 if (status == 'X')
sewardj47ffedb2011-10-24 07:36:57 +00001030 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
sewardj0bb3c672011-07-26 23:29:25 +00001031 target_signal_to_host (zignal),
1032 target_signal_to_name (zignal));
sewardj3b290482011-05-06 21:02:55 +00001033 if (status == 'W' || status == 'X') {
1034 VG_(umsg) ("Process exiting\n");
1035 VG_(exit) (0);
1036 }
1037 }
1038
1039 /* We come here when getpkt fails => close the connection,
1040 and re-open. Then return control to valgrind.
1041 We return the control to valgrind as we assume that
1042 the connection was closed due to vgdb having finished
1043 to execute a command. */
1044 if (VG_(clo_verbosity) > 1)
1045 VG_(umsg) ("Remote side has terminated connection. "
1046 "GDBserver will reopen the connection.\n");
1047 remote_finish (reset_after_error);
1048 remote_open (VG_(clo_vgdb_prefix));
1049 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +00001050 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001051 return;
1052}