blob: d8e6094d6dcb96c9618094dac3a91df587124fd4 [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
bart2dafc542011-05-18 16:08:28 +000037int pass_signals[TARGET_SIGNAL_LAST];
38
sewardj3b290482011-05-06 21:02:55 +000039/* for a gdbserver integrated in valgrind, resuming the process consists
40 in returning the control to valgrind.
41 Then at the next error or break or ..., valgrind calls gdbserver again.
42 A resume packet must then be built.
43 resume_packet_needed records the fact that the next call to gdbserver
44 must send a resume packet to gdb. */
45static Bool resume_packet_needed = False;
46
47VG_MINIMAL_JMP_BUF(toplevel);
48
49/* Decode a qXfer read request. Return 0 if everything looks OK,
50 or -1 otherwise. */
51
52static
53int decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
54{
55 /* Extract and NUL-terminate the annex. */
56 *annex = buf;
57 while (*buf && *buf != ':')
58 buf++;
59 if (*buf == '\0')
60 return -1;
61 *buf++ = 0;
62
63 /* After the read/write marker and annex, qXfer looks like a
64 traditional 'm' packet. */
65 decode_m_packet (buf, ofs, len);
66
67 return 0;
68}
69
70/* Write the response to a successful qXfer read. Returns the
71 length of the (binary) data stored in BUF, corresponding
72 to as much of DATA/LEN as we could fit. IS_MORE controls
73 the first character of the response. */
74static
75int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
76{
77 int out_len;
78
79 if (is_more)
80 buf[0] = 'm';
81 else
82 buf[0] = 'l';
83
84 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
85 PBUFSIZ - POVERHSIZ - 1) + 1;
86}
87
88static Bool initial_valgrind_sink_saved = False;
89/* True <=> valgrind log sink saved in initial_valgrind_sink */
90static OutputSink initial_valgrind_sink;
91
92static Bool command_output_to_log = False;
93/* True <=> command output goes to log instead of gdb */
94
95void reset_valgrind_sink(char *info)
96{
97 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd
98 && initial_valgrind_sink_saved) {
99 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
100 VG_(umsg) ("Reset valgrind output to log (%s)\n",
101 (info = NULL ? "" : info));
102 }
103}
104
105static
106void kill_request (char *msg)
107{
108 VG_(umsg) ("%s", msg);
109 remote_close();
110 VG_(exit) (0);
111}
112
113/* handle_gdb_valgrind_command handles the provided mon string command.
114 If command is recognised, return 1 else return 0.
115 Note that in case of ambiguous command, 1 is returned.
116
117 *sink_wanted_at_return is modified if one of the commands
sewardj30b3eca2011-06-28 08:20:39 +0000118 'v.set *_output' is handled.
sewardj3b290482011-05-06 21:02:55 +0000119*/
120static
121int handle_gdb_valgrind_command (char* mon, OutputSink* sink_wanted_at_return)
122{
123 UWord ret = 0;
124 char s[strlen(mon)+1]; /* copy for strtok_r */
125 char* wcmd;
126 Char* ssaveptr;
127 char* endptr;
128 int kwdid;
129 int int_value;
130
131 vg_assert (initial_valgrind_sink_saved);
132
133 strcpy (s, mon);
134 wcmd = strtok_r (s, " ", &ssaveptr);
135 /* NB: if possible, avoid introducing a new command below which
sewardj30b3eca2011-06-28 08:20:39 +0000136 starts with the same 3 first letters as an already existing
sewardj3b290482011-05-06 21:02:55 +0000137 command. This ensures a shorter abbreviation for the user. */
sewardj30b3eca2011-06-28 08:20:39 +0000138 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate",
sewardj3b290482011-05-06 21:02:55 +0000139 wcmd, kwd_report_duplicated_matches)) {
140 case -2:
141 ret = 1;
142 break;
143 case -1:
144 break;
145 case 0: /* help */
146 ret = 1;
147 wcmd = strtok_r (NULL, " ", &ssaveptr);
148 if (wcmd == NULL) {
149 int_value = 0;
150 } else {
151 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) {
152 case -2: int_value = 0; break;
153 case -1: int_value = 0; break;
154 case 0: int_value = 1; break;
155 default: tl_assert (0);
156 }
157 }
158
159 VG_(gdb_printf) (
160"general valgrind monitor commands:\n"
161" help [debug] : monitor command help. With debug: + debugging commands\n"
sewardj30b3eca2011-06-28 08:20:39 +0000162" v.wait [<ms>] : sleep <ms> (default 0) then continue\n"
163" v.info all_errors : show all errors found so far\n"
164" v.info last_error : show last error found\n"
165" v.info n_errs_found : show the nr of errors found so far\n"
166" v.kill : kill the Valgrind process\n"
167" v.set gdb_output : set valgrind output to gdb\n"
168" v.set log_output : set valgrind output to log\n"
169" v.set mixed_output : set valgrind output to log, interactive output to gdb\n"
170" v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
sewardj3b290482011-05-06 21:02:55 +0000171 if (int_value) { VG_(gdb_printf) (
172"debugging valgrind internals monitor commands:\n"
sewardj30b3eca2011-06-28 08:20:39 +0000173" v.info gdbserver_status : show gdbserver status\n"
174" v.info memory : show valgrind heap memory stats\n"
sewardjd6e13d82011-10-22 20:23:30 +0000175" v.info scheduler : show valgrind thread state and stacktrace\n"
sewardj30b3eca2011-06-28 08:20:39 +0000176" v.set debuglog <level> : set valgrind debug log level to <level>\n"
177" v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
sewardj3b290482011-05-06 21:02:55 +0000178" (default traceflags 0b00100000 : show after instrumentation)\n"
179" An additional flag 0b100000000 allows to show gdbserver instrumentation\n");
180 }
181 break;
sewardj30b3eca2011-06-28 08:20:39 +0000182 case 1: /* v.set */
sewardj3b290482011-05-06 21:02:55 +0000183 ret = 1;
184 wcmd = strtok_r (NULL, " ", &ssaveptr);
185 switch (kwdid = VG_(keyword_id)
186 ("vgdb-error debuglog gdb_output log_output mixed_output",
187 wcmd, kwd_report_all)) {
188 case -2:
189 case -1:
190 break;
191 case 0: /* vgdb-error */
192 case 1: /* debuglog */
193 wcmd = strtok_r (NULL, " ", &ssaveptr);
194 if (wcmd == NULL) {
195 int_value = 0;
196 endptr = "empty"; /* to report an error below */
197 } else {
198 int_value = strtol (wcmd, &endptr, 10);
199 }
200 if (*endptr != '\0') {
201 VG_(gdb_printf) ("missing or malformed integer value\n");
202 } else if (kwdid == 0) {
203 VG_(gdb_printf) ("vgdb-error value changed from %d to %d\n",
204 VG_(dyn_vgdb_error), int_value);
205 VG_(dyn_vgdb_error) = int_value;
206 } else if (kwdid == 1) {
207 VG_(gdb_printf) ("debuglog value changed from %d to %d\n",
208 VG_(debugLog_getLevel)(), int_value);
209 VG_(debugLog_startup) (int_value, "gdbsrv");
210 } else {
211 vg_assert (0);
212 }
213 break;
214 case 2: /* gdb_output */
215 (*sink_wanted_at_return).fd = -2;
216 command_output_to_log = False;
217 VG_(gdb_printf) ("valgrind output will go to gdb\n");
218 break;
219 case 3: /* log_output */
220 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
221 command_output_to_log = True;
222 VG_(gdb_printf) ("valgrind output will go to log\n");
223 break;
224 case 4: /* mixed output */
225 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
226 command_output_to_log = False;
227 VG_(gdb_printf)
228 ("valgrind output will go to log, interactive output will go to gdb\n");
229 break;
230 default:
231 vg_assert (0);
232 }
233 break;
sewardj30b3eca2011-06-28 08:20:39 +0000234 case 2: /* v.info */ {
sewardj3b290482011-05-06 21:02:55 +0000235 ret = 1;
236 wcmd = strtok_r (NULL, " ", &ssaveptr);
237 switch (kwdid = VG_(keyword_id)
sewardjd6e13d82011-10-22 20:23:30 +0000238 ("all_errors n_errs_found last_error gdbserver_status memory"
239 " scheduler",
sewardj3b290482011-05-06 21:02:55 +0000240 wcmd, kwd_report_all)) {
241 case -2:
242 case -1:
243 break;
244 case 0: // all_errors
245 // A verbosity of minimum 2 is needed to show the errors.
246 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False);
247 break;
248 case 1: // n_errs_found
philippebaf69642012-02-15 22:29:30 +0000249 VG_(gdb_printf) ("n_errs_found %d n_errs_shown %d (vgdb-error %d)\n",
sewardj3b290482011-05-06 21:02:55 +0000250 VG_(get_n_errs_found) (),
philippebaf69642012-02-15 22:29:30 +0000251 VG_(get_n_errs_shown) (),
sewardj3b290482011-05-06 21:02:55 +0000252 VG_(dyn_vgdb_error));
253 break;
254 case 2: // last_error
255 VG_(show_last_error)();
256 break;
257 case 3: // gdbserver_status
258 VG_(gdbserver_status_output)();
259 break;
260 case 4: /* memory */
261 VG_(print_all_arena_stats) ();
262 if (VG_(clo_profile_heap))
263 VG_(print_arena_cc_analysis) ();
264 ret = 1;
265 break;
sewardjd6e13d82011-10-22 20:23:30 +0000266 case 5: /* scheduler */
267 VG_(show_sched_status) ();
268 ret = 1;
269 break;
sewardj3b290482011-05-06 21:02:55 +0000270 default:
271 vg_assert(0);
272 }
273 break;
274 }
sewardj30b3eca2011-06-28 08:20:39 +0000275 case 3: /* v.wait */
sewardj3b290482011-05-06 21:02:55 +0000276 wcmd = strtok_r (NULL, " ", &ssaveptr);
277 if (wcmd != NULL) {
278 int_value = strtol (wcmd, &endptr, 10);
279 VG_(gdb_printf) ("gdbserver: continuing in %d ms ...\n", int_value);
280 VG_(poll)(NULL, 0, int_value);
281 }
282 VG_(gdb_printf) ("gdbserver: continuing after wait ...\n");
283 ret = 1;
284 break;
sewardj30b3eca2011-06-28 08:20:39 +0000285 case 4: /* v.kill */
sewardj3b290482011-05-06 21:02:55 +0000286 kill_request ("monitor command request to kill this process\n");
287 break;
sewardj30b3eca2011-06-28 08:20:39 +0000288 case 5: { /* v.translate */
sewardj3b290482011-05-06 21:02:55 +0000289 Addr address;
290 SizeT verbosity = 0x20;
291
292 ret = 1;
293
294 VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr);
295 if (address != (Addr) 0 || verbosity != 0) {
296 /* we need to force the output to log for the translation trace,
297 as low level VEX tracing cannot be redirected to gdb. */
298 int saved_command_output_to_log = command_output_to_log;
299 int saved_fd = VG_(log_output_sink).fd;
300 Bool single_stepping_on_entry = valgrind_single_stepping();
301 int vex_verbosity = verbosity & 0xff;
302 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
303 if ((verbosity & 0x100) && !single_stepping_on_entry) {
304 valgrind_set_single_stepping(True);
305 // to force gdbserver instrumentation.
306 }
sewardj99d61342011-05-17 16:35:11 +0000307# if defined(VGA_arm)
308 // on arm, we need to (potentially) convert this address
309 // to the thumb form.
310 address = thumb_pc (address);
311# endif
312
sewardj3b290482011-05-06 21:02:55 +0000313 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
314 address,
315 /*debugging*/True,
316 (Int) vex_verbosity,
317 /*bbs_done*/0,
318 /*allow redir?*/True);
319 if ((verbosity & 0x100) && !single_stepping_on_entry) {
320 valgrind_set_single_stepping(False);
321 // reset single stepping.
322 }
323 command_output_to_log = saved_command_output_to_log;
324 VG_(log_output_sink).fd = saved_fd;
325 }
326 break;
327 }
328
329 default:
330 vg_assert (0);
331 }
332 return ret;
333}
334
335/* handle_gdb_monitor_command handles the provided mon string command,
336 which can be either a "standard" valgrind monitor command
337 or a tool specific monitor command.
338 If command recognised, return 1 else return 0.
339 Note that in case of ambiguous command, 1 is returned.
340*/
341static
342int handle_gdb_monitor_command (char* mon)
343{
344 UWord ret = 0;
345 UWord tool_ret = 0;
346 // initially, we assume that when returning, the desired sink is the
347 // one we have when entering. It can however be changed by the standard
348 // valgrind command handling.
349 OutputSink sink_wanted_at_return = VG_(log_output_sink);
350
351 if (!initial_valgrind_sink_saved) {
352 /* first time we enter here, we save the valgrind default log sink */
353 initial_valgrind_sink = sink_wanted_at_return;
354 initial_valgrind_sink_saved = True;
355 }
356
357 if (!command_output_to_log)
358 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */
359
360 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return);
361
362 /* Even if command was recognised by valgrind core, we call the
363 tool command handler : this is needed to handle help command
364 and/or to let the tool do some additional processing of a
365 valgrind standard command. Note however that if valgrind
366 recognised the command, we will always return success. */
367 if (VG_(needs).client_requests) {
368 /* If the tool reports an error when handling a monitor command,
369 we need to avoid calling gdbserver during this command
370 handling. So, we temporarily set VG_(dyn_vgdb_error) to
371 a huge value to ensure m_errormgr.c does not call gdbserver. */
372 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error);
373 UWord arg[2];
374 VG_(dyn_vgdb_error) = 999999999;
375 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND;
376 arg[1] = (UWord) mon;
377 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg,
378 &tool_ret);
379 VG_(dyn_vgdb_error) = save_dyn_vgdb_error;
380 }
381
382 /* restore or set the desired output */
383 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
384 if (ret | tool_ret)
385 return 1;
386 else
387 return 0;
388}
389
390
391/* Handle all of the extended 'Q' packets. */
392static
393void handle_set (char *arg_own_buf, int *new_packet_len_p)
394{
395 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) {
396 noack_mode = True;
397 write_ok (arg_own_buf);
398 return;
399 }
400
401 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
402 int i;
403 char *from, *to;
404 char *end = arg_own_buf + strlen(arg_own_buf);
405 CORE_ADDR sig;
406 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
407 pass_signals[i] = 0;
408
409 from = arg_own_buf + 13;
410 while (from < end) {
411 to = strchr(from, ';');
412 if (to == NULL) to = end;
413 decode_address (&sig, from, to - from);
414 pass_signals[(int)sig] = 1;
415 dlog(1, "pass_signal %d\n", (int)sig);
416 from = to;
417 if (*from == ';') from++;
418 }
419 write_ok (arg_own_buf);
420 return;
421 }
422 /* Otherwise we didn't know what packet it was. Say we didn't
423 understand it. */
424 arg_own_buf[0] = 0;
425}
426
427/* Handle all of the extended 'q' packets. */
428static
429void handle_query (char *arg_own_buf, int *new_packet_len_p)
430{
431 static struct inferior_list_entry *thread_ptr;
432
433 /* qRcmd, monitor command handling. */
434 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
435 char *p = arg_own_buf + 6;
436 int cmdlen = strlen(p)/2;
437 char cmd[cmdlen+1];
438
439 if (unhexify (cmd, p, cmdlen) != cmdlen) {
440 write_enn (arg_own_buf);
441 return;
442 }
443 cmd[cmdlen] = '\0';
444
445 if (handle_gdb_monitor_command (cmd)) {
446 /* In case the command is from a standalone vgdb,
447 connection will be closed soon => flush the output. */
448 VG_(message_flush) ();
449 write_ok (arg_own_buf);
450 return;
451 } else {
452 /* cmd not recognised */
453 VG_(gdb_printf)
454 ("command '%s' not recognised\n"
455 "In gdb, try 'monitor help'\n"
456 "In a shell, try 'vgdb help'\n",
457 cmd);
458 write_ok (arg_own_buf);
459 return;
460 }
461 }
462
463 /* provide some valgrind specific info in return to qThreadExtraInfo. */
464 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
465 unsigned long gdb_id;
466 struct thread_info *ti;
467 ThreadState *tst;
468 char status[100];
469
470 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
471 ti = gdb_id_to_thread (gdb_id);
472 if (ti != NULL) {
473 tst = (ThreadState *) inferior_target_data (ti);
474 /* Additional info is the tid and the thread status. */
475 VG_(snprintf) (status, sizeof(status), "tid %d %s",
476 tst->tid,
477 VG_(name_of_ThreadStatus)(tst->status));
478 hexify (arg_own_buf, status, strlen(status));
479 return;
480 } else {
481 write_enn (arg_own_buf);
482 return;
483 }
484 }
485
486 if (strcmp ("qAttached", arg_own_buf) == 0) {
487 /* tell gdb to always detach, never kill the process */
488 arg_own_buf[0] = '1';
489 arg_own_buf[1] = 0;
490 return;
491 }
492
493 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
494 /* We have no symbol to read. */
495 write_ok (arg_own_buf);
496 return;
497 }
498
499 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
500 thread_ptr = all_threads.head;
501 VG_(sprintf) (arg_own_buf, "m%x",
502 thread_to_gdb_id ((struct thread_info *)thread_ptr));
503 thread_ptr = thread_ptr->next;
504 return;
505 }
506
507 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
508 if (thread_ptr != NULL) {
509 VG_(sprintf) (arg_own_buf, "m%x",
510 thread_to_gdb_id ((struct thread_info *)thread_ptr));
511 thread_ptr = thread_ptr->next;
512 return;
513 } else {
514 VG_(sprintf) (arg_own_buf, "l");
515 return;
516 }
517 }
518
519 if ( ((*the_target->target_xml)() != NULL
520 || (*the_target->shadow_target_xml)() != NULL)
521 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
522 CORE_ADDR ofs;
523 unsigned int len, doc_len;
524 char *annex = NULL;
525 // First, the annex is extracted from the packet received.
526 // Then, it is replaced by the corresponding file name.
527 int fd;
528
529 /* Grab the annex, offset, and length. */
530 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
531 strcpy (arg_own_buf, "E00");
532 return;
533 }
534
535 if (strcmp (annex, "target.xml") == 0) {
536 annex = NULL; // to replace it by the corresponding filename.
537
538 /* If VG_(clo_vgdb_shadow_registers), try to use
539 shadow_target_xml. Fallback to target_xml
540 if not defined. */
541 if (VG_(clo_vgdb_shadow_registers)) {
542 annex = (*the_target->shadow_target_xml)();
543 if (annex != NULL)
544 /* Ensure the shadow registers are initialized. */
545 initialize_shadow_low(True);
546 }
547 if (annex == NULL)
548 annex = (*the_target->target_xml)();
549 if (annex == NULL) {
550 strcpy (arg_own_buf, "E00");
551 return;
552 }
553 }
554
555 {
556 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex)];
557 struct vg_stat stat_doc;
558 char toread[len];
559 int len_read;
560
561 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
562 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
563 if (fd == -1) {
564 strcpy (arg_own_buf, "E00");
565 return;
566 }
567 if (VG_(fstat) (fd, &stat_doc) != 0) {
568 VG_(close) (fd);
569 strcpy (arg_own_buf, "E00");
570 return;
571 }
572 doc_len = stat_doc.size;
573
574 if (len > PBUFSIZ - POVERHSIZ)
575 len = PBUFSIZ - POVERHSIZ;
576
577 if (ofs > doc_len) {
578 write_enn (arg_own_buf);
579 VG_(close) (fd);
580 return;
581 }
582 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
583 len_read = VG_(read) (fd, toread, len);
584 *new_packet_len_p = write_qxfer_response (arg_own_buf, toread,
585 len_read, ofs + len_read < doc_len);
586 VG_(close) (fd);
587 return;
588 }
589 }
590
sewardj2a312392011-06-26 09:26:48 +0000591 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) {
592 unsigned char *data;
593 int n;
594 CORE_ADDR ofs;
595 unsigned int len;
596 char *annex;
597
598 /* Reject any annex; grab the offset and length. */
599 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0
600 || annex[0] != '\0') {
601 strcpy (arg_own_buf, "E00");
602 return;
603 }
604
605 if (len > PBUFSIZ - 2)
606 len = PBUFSIZ - 2;
607 data = malloc (len);
608
609 {
610 UWord *client_auxv = VG_(client_auxv);
611 unsigned int client_auxv_len = 0;
612 while (*client_auxv != 0) {
613 dlog(4, "auxv %lld %llx\n",
614 (ULong)*client_auxv,
615 (ULong)*(client_auxv+1));
616 client_auxv++;
617 client_auxv++;
618 client_auxv_len += 2 * sizeof(UWord);
619 }
620 client_auxv_len += 2 * sizeof(UWord);
621 dlog(4, "auxv len %d\n", client_auxv_len);
622
623 if (ofs >= client_auxv_len)
624 n = -1;
625 else {
626 n = client_auxv_len - ofs;
627 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n);
628 }
629 }
630
631 if (n < 0)
632 write_enn (arg_own_buf);
633 else if (n > len)
634 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
635 else
636 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
637
638 free (data);
639
640 return;
641 }
642
643
sewardj3b290482011-05-06 21:02:55 +0000644 /* Protocol features query. */
645 if (strncmp ("qSupported", arg_own_buf, 10) == 0
646 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
647 VG_(sprintf) (arg_own_buf, "PacketSize=%x", PBUFSIZ - 1);
648 /* Note: max packet size including frame and checksum, but without
649 trailing null byte, which is not sent/received. */
650
651 strcat (arg_own_buf, ";QStartNoAckMode+");
652 strcat (arg_own_buf, ";QPassSignals+");
sewardj2a312392011-06-26 09:26:48 +0000653 if (VG_(client_auxv))
654 strcat (arg_own_buf, ";qXfer:auxv:read+");
sewardj3b290482011-05-06 21:02:55 +0000655
656 if ((*the_target->target_xml)() != NULL
657 || (*the_target->shadow_target_xml)() != NULL) {
658 strcat (arg_own_buf, ";qXfer:features:read+");
659 /* if a new gdb connects to us, we have to reset the register
660 set to the normal register sets to allow this new gdb to
661 decide to use or not the shadow registers.
662
663 Note that the reset is only done for gdb that are sending
664 qSupported packets. If a user first connected with a recent
665 gdb using shadow registers and then with a very old gdb
666 that does not use qSupported packet, then the old gdb will
667 not properly connect. */
668 initialize_shadow_low(False);
669 }
670 return;
671 }
672
673 /* Otherwise we didn't know what packet it was. Say we didn't
674 understand it. */
675 arg_own_buf[0] = 0;
676}
677
678/* Handle all of the extended 'v' packets. */
679static
sewardj0bb3c672011-07-26 23:29:25 +0000680void handle_v_requests (char *arg_own_buf, char *status, int *zignal)
sewardj3b290482011-05-06 21:02:55 +0000681{
682 /* vcont packet code from gdb 6.6 removed */
683
684 /* Otherwise we didn't know what packet it was. Say we didn't
685 understand it. */
686 arg_own_buf[0] = 0;
687 return;
688}
689
690static
691void myresume (int step, int sig)
692{
693 struct thread_resume resume_info[2];
694 int n = 0;
695
696 if (step || sig || (cont_thread != 0 && cont_thread != -1)) {
697 resume_info[0].thread
698 = ((struct inferior_list_entry *) current_inferior)->id;
699 resume_info[0].step = step;
700 resume_info[0].sig = sig;
701 resume_info[0].leave_stopped = 0;
702 n++;
703 }
704 resume_info[n].thread = -1;
705 resume_info[n].step = 0;
706 resume_info[n].sig = 0;
707 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
708
709 resume_packet_needed = True;
710 (*the_target->resume) (resume_info);
711}
712
713/* server_main global variables */
714static char *own_buf;
715static unsigned char *mem_buf;
716
717void gdbserver_init (void)
718{
719 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
720 noack_mode = False;
721 initialize_low ();
722 own_buf = malloc (PBUFSIZ);
723 mem_buf = malloc (PBUFSIZ);
724}
725
726void gdbserver_terminate (void)
727{
728 /* last call to gdbserver is cleanup call */
729 if (VG_MINIMAL_SETJMP(toplevel)) {
730 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
731 return;
732 }
733 remote_close();
734}
735
736void server_main (void)
737{
738 static char status;
sewardj0bb3c672011-07-26 23:29:25 +0000739 static int zignal;
sewardj3b290482011-05-06 21:02:55 +0000740
741 char ch;
742 int i = 0;
743 unsigned int len;
744 CORE_ADDR mem_addr;
745
sewardj0bb3c672011-07-26 23:29:25 +0000746 zignal = mywait (&status);
sewardj3b290482011-05-06 21:02:55 +0000747 if (VG_MINIMAL_SETJMP(toplevel)) {
748 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
749 }
750 while (1) {
751 unsigned char sig;
752 int packet_len;
753 int new_packet_len = -1;
754
755 if (resume_packet_needed) {
756 resume_packet_needed = False;
sewardj0bb3c672011-07-26 23:29:25 +0000757 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000758 putpkt (own_buf);
759 }
760
761 packet_len = getpkt (own_buf);
762 if (packet_len <= 0)
763 break;
764
765 i = 0;
766 ch = own_buf[i++];
767 switch (ch) {
768 case 'Q':
769 handle_set (own_buf, &new_packet_len);
770 break;
771 case 'q':
772 handle_query (own_buf, &new_packet_len);
773 break;
774 case 'd':
775 /* set/unset debugging is done through valgrind debug level. */
776 own_buf[0] = '\0';
777 break;
778 case 'D':
779 reset_valgrind_sink("gdb detaching from process");
780
781 /* When detaching or kill the process, gdb expects to get
782 an packet OK back. Any other output will make gdb
783 believes detach did not work. */
784 write_ok (own_buf);
785 putpkt (own_buf);
786 remote_finish (reset_after_error);
787 remote_open (VG_(clo_vgdb_prefix));
788 myresume (0, 0);
789 resume_packet_needed = False;
790 return;
791 case '!':
792 /* We can not use the extended protocol with valgrind,
793 because we can not restart the running
794 program. So return unrecognized. */
795 own_buf[0] = '\0';
796 break;
797 case '?':
sewardj0bb3c672011-07-26 23:29:25 +0000798 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000799 break;
800 case 'H':
801 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
802 unsigned long gdb_id, thread_id;
803
804 gdb_id = strtoul (&own_buf[2], NULL, 16);
805 thread_id = gdb_id_to_thread_id (gdb_id);
806 if (thread_id == 0) {
807 write_enn (own_buf);
808 break;
809 }
810
811 if (own_buf[1] == 'g') {
812 general_thread = thread_id;
813 set_desired_inferior (1);
814 } else if (own_buf[1] == 'c') {
815 cont_thread = thread_id;
816 } else if (own_buf[1] == 's') {
817 step_thread = thread_id;
818 }
819
820 write_ok (own_buf);
821 } else {
822 /* Silently ignore it so that gdb can extend the protocol
823 without compatibility headaches. */
824 own_buf[0] = '\0';
825 }
826 break;
827 case 'g':
828 set_desired_inferior (1);
829 registers_to_string (own_buf);
830 break;
831 case 'G':
832 set_desired_inferior (1);
833 registers_from_string (&own_buf[1]);
834 write_ok (own_buf);
835 break;
836 case 'P': {
837 int regno;
838 char *regbytes;
839 Bool mod;
840 ThreadState *tst;
841 regno = strtol(&own_buf[1], NULL, 16);
842 regbytes = strchr(&own_buf[0], '=') + 1;
843 set_desired_inferior (1);
844 tst = (ThreadState *) inferior_target_data (current_inferior);
845 /* Only accept changing registers in "runnable state3.
846 In fact, it would be ok to change most of the registers
847 except a few "sensitive" registers such as the PC, SP, BP.
848 We assume we do not need to very specific here, and that we
849 can just refuse all of these. */
850 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
851 supply_register_from_string (regno, regbytes, &mod);
852 write_ok (own_buf);
853 } else {
854 /* at least from gdb 6.6 onwards, an E. error
855 reply is shown to the user. So, we do an error
856 msg which both is accepted by gdb as an error msg
857 and is readable by the user. */
858 VG_(sprintf)
859 (own_buf,
860"E.\n"
861"ERROR changing register %s regno %d\n"
862"gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
863"set pc, calling from gdb a function in the debugged process, ...)\n"
864"can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
865"Thread status is %s\n",
866 find_register_by_number (regno)->name, regno,
867 VG_(name_of_ThreadStatus)(tst->status));
868 if (VG_(clo_verbosity) > 1)
869 VG_(umsg) ("%s\n", own_buf);
870 }
871 break;
872 }
873 case 'm':
874 decode_m_packet (&own_buf[1], &mem_addr, &len);
875 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
876 convert_int_to_ascii (mem_buf, own_buf, len);
877 else
878 write_enn (own_buf);
879 break;
880 case 'M':
881 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
882 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
883 write_ok (own_buf);
884 else
885 write_enn (own_buf);
886 break;
887 case 'X':
888 if (decode_X_packet (&own_buf[1], packet_len - 1,
889 &mem_addr, &len, mem_buf) < 0
890 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
891 write_enn (own_buf);
892 else
893 write_ok (own_buf);
894 break;
895 case 'C':
896 convert_ascii_to_int (own_buf + 1, &sig, 1);
897 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +0000898 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +0000899 else
sewardj0bb3c672011-07-26 23:29:25 +0000900 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +0000901 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +0000902 myresume (0, zignal);
sewardj3b290482011-05-06 21:02:55 +0000903 return; // return control to valgrind
904 case 'S':
905 convert_ascii_to_int (own_buf + 1, &sig, 1);
906 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +0000907 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +0000908 else
sewardj0bb3c672011-07-26 23:29:25 +0000909 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +0000910 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +0000911 myresume (1, zignal);
sewardj3b290482011-05-06 21:02:55 +0000912 return; // return control to valgrind
913 case 'c':
914 set_desired_inferior (0);
915 myresume (0, 0);
916 return; // return control to valgrind
917 case 's':
918 set_desired_inferior (0);
919 myresume (1, 0);
920 return; // return control to valgrind
921 case 'Z': {
922 char *lenptr;
923 char *dataptr;
924 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
925 int zlen = strtol (lenptr + 1, &dataptr, 16);
926 char type = own_buf[1];
927
928 if (the_target->insert_watchpoint == NULL
929 || (type < '0' || type > '4')) {
930 /* No watchpoint support or not a watchpoint command;
931 unrecognized either way. */
932 own_buf[0] = '\0';
933 } else {
934 int res;
935
936 res = (*the_target->insert_watchpoint) (type, addr, zlen);
937 if (res == 0)
938 write_ok (own_buf);
939 else if (res == 1)
940 /* Unsupported. */
941 own_buf[0] = '\0';
942 else
943 write_enn (own_buf);
944 }
945 break;
946 }
947 case 'z': {
948 char *lenptr;
949 char *dataptr;
950 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
951 int zlen = strtol (lenptr + 1, &dataptr, 16);
952 char type = own_buf[1];
953
954 if (the_target->remove_watchpoint == NULL
955 || (type < '0' || type > '4')) {
956 /* No watchpoint support or not a watchpoint command;
957 unrecognized either way. */
958 own_buf[0] = '\0';
959 } else {
960 int res;
961
962 res = (*the_target->remove_watchpoint) (type, addr, zlen);
963 if (res == 0)
964 write_ok (own_buf);
965 else if (res == 1)
966 /* Unsupported. */
967 own_buf[0] = '\0';
968 else
969 write_enn (own_buf);
970 }
971 break;
972 }
973 case 'k':
974 kill_request("Gdb request to kill this process\n");
975 break;
976 case 'T': {
977 unsigned long gdb_id, thread_id;
978
979 gdb_id = strtoul (&own_buf[1], NULL, 16);
980 thread_id = gdb_id_to_thread_id (gdb_id);
981 if (thread_id == 0) {
982 write_enn (own_buf);
983 break;
984 }
985
986 if (mythread_alive (thread_id))
987 write_ok (own_buf);
988 else
989 write_enn (own_buf);
990 break;
991 }
992 case 'R':
993 /* Restarting the inferior is only supported in the
994 extended protocol.
995 => It is a request we don't understand. Respond with an
996 empty packet so that gdb knows that we don't support this
997 request. */
998 own_buf[0] = '\0';
999 break;
1000 case 'v':
1001 /* Extended (long) request. */
sewardj0bb3c672011-07-26 23:29:25 +00001002 handle_v_requests (own_buf, &status, &zignal);
sewardj3b290482011-05-06 21:02:55 +00001003 break;
1004 default:
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 }
1011
1012 if (new_packet_len != -1)
1013 putpkt_binary (own_buf, new_packet_len);
1014 else
1015 putpkt (own_buf);
1016
1017 if (status == 'W')
sewardj0bb3c672011-07-26 23:29:25 +00001018 VG_(umsg) ("\nChild exited with status %d\n", zignal);
sewardj3b290482011-05-06 21:02:55 +00001019 if (status == 'X')
sewardj47ffedb2011-10-24 07:36:57 +00001020 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
sewardj0bb3c672011-07-26 23:29:25 +00001021 target_signal_to_host (zignal),
1022 target_signal_to_name (zignal));
sewardj3b290482011-05-06 21:02:55 +00001023 if (status == 'W' || status == 'X') {
1024 VG_(umsg) ("Process exiting\n");
1025 VG_(exit) (0);
1026 }
1027 }
1028
1029 /* We come here when getpkt fails => close the connection,
1030 and re-open. Then return control to valgrind.
1031 We return the control to valgrind as we assume that
1032 the connection was closed due to vgdb having finished
1033 to execute a command. */
1034 if (VG_(clo_verbosity) > 1)
1035 VG_(umsg) ("Remote side has terminated connection. "
1036 "GDBserver will reopen the connection.\n");
1037 remote_finish (reset_after_error);
1038 remote_open (VG_(clo_vgdb_prefix));
1039 myresume (0, 0);
1040 resume_packet_needed = False;
1041 return;
1042}