sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1 | /* 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" |
sewardj | 2a31239 | 2011-06-26 09:26:48 +0000 | [diff] [blame] | 29 | #include "pub_core_initimg.h" |
philippe | f3a6e93 | 2013-01-10 20:42:51 +0000 | [diff] [blame] | 30 | #include "pub_core_execontext.h" |
florian | dbb3584 | 2012-10-27 18:39:11 +0000 | [diff] [blame] | 31 | #include "pub_core_syswrap.h" // VG_(show_open_fds) |
philippe | 66e4c73 | 2013-01-26 16:45:01 +0000 | [diff] [blame] | 32 | #include "pub_core_scheduler.h" |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 33 | #include "pub_core_transtab.h" |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 34 | #include "pub_core_debuginfo.h" |
| 35 | #include "pub_core_addrinfo.h" |
florian | 7463e49 | 2015-02-26 17:48:07 +0000 | [diff] [blame] | 36 | #include "pub_core_aspacemgr.h" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 37 | |
| 38 | unsigned long cont_thread; |
| 39 | unsigned long general_thread; |
| 40 | unsigned long step_thread; |
| 41 | unsigned long thread_from_wait; |
| 42 | unsigned long old_thread_from_wait; |
| 43 | |
philippe | 886fde3 | 2012-03-29 21:56:47 +0000 | [diff] [blame] | 44 | int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */ |
bart | 2dafc54 | 2011-05-18 16:08:28 +0000 | [diff] [blame] | 45 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 46 | /* for a gdbserver integrated in valgrind, resuming the process consists |
| 47 | in returning the control to valgrind. |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 48 | The guess process resumes its execution. |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 49 | Then at the next error or break or ..., valgrind calls gdbserver again. |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 50 | A resume reply packet must then be built to inform GDB that the |
| 51 | resume request is finished. |
| 52 | resume_reply_packet_needed records the fact that the next call to gdbserver |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 53 | must send a resume packet to gdb. */ |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 54 | static Bool resume_reply_packet_needed = False; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 55 | |
| 56 | VG_MINIMAL_JMP_BUF(toplevel); |
| 57 | |
| 58 | /* Decode a qXfer read request. Return 0 if everything looks OK, |
| 59 | or -1 otherwise. */ |
| 60 | |
| 61 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 62 | int decode_xfer_read (char *buf, const char **annex, CORE_ADDR *ofs, unsigned int *len) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 63 | { |
| 64 | /* Extract and NUL-terminate the annex. */ |
| 65 | *annex = buf; |
| 66 | while (*buf && *buf != ':') |
| 67 | buf++; |
| 68 | if (*buf == '\0') |
| 69 | return -1; |
| 70 | *buf++ = 0; |
| 71 | |
| 72 | /* After the read/write marker and annex, qXfer looks like a |
| 73 | traditional 'm' packet. */ |
| 74 | decode_m_packet (buf, ofs, len); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /* Write the response to a successful qXfer read. Returns the |
| 80 | length of the (binary) data stored in BUF, corresponding |
| 81 | to as much of DATA/LEN as we could fit. IS_MORE controls |
| 82 | the first character of the response. */ |
| 83 | static |
| 84 | int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more) |
| 85 | { |
| 86 | int out_len; |
| 87 | |
| 88 | if (is_more) |
| 89 | buf[0] = 'm'; |
| 90 | else |
| 91 | buf[0] = 'l'; |
| 92 | |
| 93 | return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len, |
| 94 | PBUFSIZ - POVERHSIZ - 1) + 1; |
| 95 | } |
| 96 | |
| 97 | static Bool initial_valgrind_sink_saved = False; |
| 98 | /* True <=> valgrind log sink saved in initial_valgrind_sink */ |
| 99 | static OutputSink initial_valgrind_sink; |
| 100 | |
| 101 | static Bool command_output_to_log = False; |
| 102 | /* True <=> command output goes to log instead of gdb */ |
| 103 | |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 104 | void reset_valgrind_sink(const char *info) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 105 | { |
| 106 | if (VG_(log_output_sink).fd != initial_valgrind_sink.fd |
| 107 | && initial_valgrind_sink_saved) { |
| 108 | VG_(log_output_sink).fd = initial_valgrind_sink.fd; |
| 109 | VG_(umsg) ("Reset valgrind output to log (%s)\n", |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 110 | (info == NULL ? "" : info)); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 114 | void print_to_initial_valgrind_sink (const char *msg) |
| 115 | { |
| 116 | vg_assert (initial_valgrind_sink_saved); |
| 117 | VG_(write) (initial_valgrind_sink.fd, msg, strlen(msg)); |
| 118 | } |
| 119 | |
| 120 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 121 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 122 | void kill_request (const char *msg) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 123 | { |
| 124 | VG_(umsg) ("%s", msg); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 125 | VG_(exit) (0); |
| 126 | } |
| 127 | |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 128 | // s is a NULL terminated string made of O or more words (separated by spaces). |
| 129 | // Returns a pointer to the Nth word in s. |
| 130 | // If Nth word does not exist, return a pointer to the last (0) byte of s. |
| 131 | static |
| 132 | const char *wordn (const char *s, int n) |
| 133 | { |
| 134 | int word_seen = 0; |
| 135 | Bool searching_word = True; |
| 136 | |
| 137 | while (*s) { |
| 138 | if (*s == ' ') |
| 139 | searching_word = True; |
| 140 | else { |
| 141 | if (searching_word) { |
| 142 | searching_word = False; |
| 143 | word_seen++; |
| 144 | if (word_seen == n) |
| 145 | return s; |
| 146 | } |
| 147 | } |
| 148 | s++; |
| 149 | } |
| 150 | return s; |
| 151 | } |
| 152 | |
philippe | 4f6f336 | 2014-04-19 00:25:54 +0000 | [diff] [blame] | 153 | void VG_(print_all_stats) (Bool memory_stats, Bool tool_stats) |
| 154 | { |
| 155 | if (memory_stats) { |
| 156 | VG_(message)(Vg_DebugMsg, "\n"); |
| 157 | VG_(message)(Vg_DebugMsg, |
| 158 | "------ Valgrind's internal memory use stats follow ------\n" ); |
| 159 | VG_(sanity_check_malloc_all)(); |
philippe | 9252713 | 2015-05-10 22:40:38 +0000 | [diff] [blame] | 160 | VG_(message) |
| 161 | (Vg_DebugMsg, |
| 162 | "------ %'13llu bytes have already been mmap-ed ANONYMOUS.\n", |
| 163 | VG_(am_get_anonsize_total)()); |
philippe | 4f6f336 | 2014-04-19 00:25:54 +0000 | [diff] [blame] | 164 | VG_(print_all_arena_stats)(); |
| 165 | if (VG_(clo_profile_heap)) |
| 166 | VG_(print_arena_cc_analysis) (); |
| 167 | VG_(message)(Vg_DebugMsg, "\n"); |
| 168 | } |
| 169 | |
| 170 | VG_(print_translation_stats)(); |
| 171 | VG_(print_tt_tc_stats)(); |
| 172 | VG_(print_scheduler_stats)(); |
| 173 | VG_(print_ExeContext_stats)( False /* with_stacktraces */ ); |
| 174 | VG_(print_errormgr_stats)(); |
| 175 | if (tool_stats && VG_(needs).print_stats) { |
| 176 | VG_TDICT_CALL(tool_print_stats); |
| 177 | } |
| 178 | } |
| 179 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 180 | /* handle_gdb_valgrind_command handles the provided mon string command. |
| 181 | If command is recognised, return 1 else return 0. |
| 182 | Note that in case of ambiguous command, 1 is returned. |
| 183 | |
| 184 | *sink_wanted_at_return is modified if one of the commands |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 185 | 'v.set *_output' is handled. |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 186 | */ |
| 187 | static |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 188 | int handle_gdb_valgrind_command (char *mon, OutputSink *sink_wanted_at_return) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 189 | { |
| 190 | UWord ret = 0; |
| 191 | char s[strlen(mon)+1]; /* copy for strtok_r */ |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 192 | char *wcmd; |
| 193 | HChar *ssaveptr; |
| 194 | const char *endptr; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 195 | int kwdid; |
| 196 | int int_value; |
| 197 | |
| 198 | vg_assert (initial_valgrind_sink_saved); |
| 199 | |
| 200 | strcpy (s, mon); |
| 201 | wcmd = strtok_r (s, " ", &ssaveptr); |
| 202 | /* NB: if possible, avoid introducing a new command below which |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 203 | starts with the same 3 first letters as an already existing |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 204 | command. This ensures a shorter abbreviation for the user. */ |
philippe | 6ec8d63 | 2013-01-23 22:10:28 +0000 | [diff] [blame] | 205 | switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate" |
| 206 | " v.do", |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 207 | wcmd, kwd_report_duplicated_matches)) { |
| 208 | case -2: |
| 209 | ret = 1; |
| 210 | break; |
| 211 | case -1: |
| 212 | break; |
| 213 | case 0: /* help */ |
| 214 | ret = 1; |
| 215 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 216 | if (wcmd == NULL) { |
| 217 | int_value = 0; |
| 218 | } else { |
| 219 | switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) { |
| 220 | case -2: int_value = 0; break; |
| 221 | case -1: int_value = 0; break; |
| 222 | case 0: int_value = 1; break; |
florian | e2800c9 | 2014-09-15 20:57:45 +0000 | [diff] [blame] | 223 | default: vg_assert (0); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | VG_(gdb_printf) ( |
| 228 | "general valgrind monitor commands:\n" |
philippe | c336038 | 2012-10-21 14:37:14 +0000 | [diff] [blame] | 229 | " help [debug] : monitor command help. With debug: + debugging commands\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 230 | " v.wait [<ms>] : sleep <ms> (default 0) then continue\n" |
| 231 | " v.info all_errors : show all errors found so far\n" |
| 232 | " v.info last_error : show last error found\n" |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 233 | " v.info location <addr> : show information about location <addr>\n" |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 234 | " v.info n_errs_found [msg] : show the nr of errors found so far and the given msg\n" |
philippe | c336038 | 2012-10-21 14:37:14 +0000 | [diff] [blame] | 235 | " v.info open_fds : show open file descriptors (only if --track-fds=yes)\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 236 | " v.kill : kill the Valgrind process\n" |
| 237 | " v.set gdb_output : set valgrind output to gdb\n" |
| 238 | " v.set log_output : set valgrind output to log\n" |
| 239 | " v.set mixed_output : set valgrind output to log, interactive output to gdb\n" |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 240 | " v.set merge-recursive-frames <num> : merge recursive calls in max <num> frames\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 241 | " v.set vgdb-error <errornr> : debug me at error >= <errornr> \n"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 242 | if (int_value) { VG_(gdb_printf) ( |
| 243 | "debugging valgrind internals monitor commands:\n" |
philippe | 6ec8d63 | 2013-01-23 22:10:28 +0000 | [diff] [blame] | 244 | " v.do expensive_sanity_check_general : do an expensive sanity check now\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 245 | " v.info gdbserver_status : show gdbserver status\n" |
philippe | 93a6a8d | 2012-04-27 22:59:43 +0000 | [diff] [blame] | 246 | " v.info memory [aspacemgr] : show valgrind heap memory stats\n" |
florian | ad4e979 | 2015-07-05 21:53:33 +0000 | [diff] [blame] | 247 | " (with aspacemgr arg, also shows valgrind segments on log output)\n" |
philippe | f3a6e93 | 2013-01-10 20:42:51 +0000 | [diff] [blame] | 248 | " v.info exectxt : show stacktraces and stats of all execontexts\n" |
sewardj | d6e13d8 | 2011-10-22 20:23:30 +0000 | [diff] [blame] | 249 | " v.info scheduler : show valgrind thread state and stacktrace\n" |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 250 | " v.info stats : show various valgrind and tool stats\n" |
philippe | 97bfa19 | 2015-06-02 22:09:42 +0000 | [diff] [blame] | 251 | " v.info unwind <addr> [<len>] : show unwind debug info for <addr> .. <addr+len>\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 252 | " v.set debuglog <level> : set valgrind debug log level to <level>\n" |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame] | 253 | " v.set hostvisibility [yes*|no] : (en/dis)ables access by gdb/gdbserver to\n" |
| 254 | " Valgrind internal host status/memory\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 255 | " v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 256 | " (default traceflags 0b00100000 : show after instrumentation)\n" |
| 257 | " An additional flag 0b100000000 allows to show gdbserver instrumentation\n"); |
| 258 | } |
| 259 | break; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 260 | case 1: /* v.set */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 261 | ret = 1; |
| 262 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 263 | switch (kwdid = VG_(keyword_id) |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 264 | ("vgdb-error debuglog merge-recursive-frames" |
philippe | 64ba574 | 2014-06-19 20:33:27 +0000 | [diff] [blame] | 265 | " gdb_output log_output mixed_output hostvisibility", |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 266 | wcmd, kwd_report_all)) { |
| 267 | case -2: |
| 268 | case -1: |
| 269 | break; |
| 270 | case 0: /* vgdb-error */ |
| 271 | case 1: /* debuglog */ |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 272 | case 2: /* merge-recursive-frames */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 273 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 274 | if (wcmd == NULL) { |
| 275 | int_value = 0; |
| 276 | endptr = "empty"; /* to report an error below */ |
| 277 | } else { |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 278 | HChar *the_end; |
| 279 | int_value = strtol (wcmd, &the_end, 10); |
| 280 | endptr = the_end; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 281 | } |
| 282 | if (*endptr != '\0') { |
| 283 | VG_(gdb_printf) ("missing or malformed integer value\n"); |
| 284 | } else if (kwdid == 0) { |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 285 | VG_(printf) ("vgdb-error value changed from %d to %d\n", |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 286 | VG_(dyn_vgdb_error), int_value); |
| 287 | VG_(dyn_vgdb_error) = int_value; |
| 288 | } else if (kwdid == 1) { |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 289 | VG_(printf) ("debuglog value changed from %d to %d\n", |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 290 | VG_(debugLog_getLevel)(), int_value); |
| 291 | VG_(debugLog_startup) (int_value, "gdbsrv"); |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 292 | } else if (kwdid == 2) { |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 293 | VG_(printf) |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 294 | ("merge-recursive-frames value changed from %d to %d\n", |
| 295 | VG_(clo_merge_recursive_frames), int_value); |
| 296 | VG_(clo_merge_recursive_frames) = int_value; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 297 | } else { |
| 298 | vg_assert (0); |
| 299 | } |
| 300 | break; |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 301 | case 3: /* gdb_output */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 302 | (*sink_wanted_at_return).fd = -2; |
| 303 | command_output_to_log = False; |
| 304 | VG_(gdb_printf) ("valgrind output will go to gdb\n"); |
| 305 | break; |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 306 | case 4: /* log_output */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 307 | (*sink_wanted_at_return).fd = initial_valgrind_sink.fd; |
| 308 | command_output_to_log = True; |
| 309 | VG_(gdb_printf) ("valgrind output will go to log\n"); |
| 310 | break; |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 311 | case 5: /* mixed output */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 312 | (*sink_wanted_at_return).fd = initial_valgrind_sink.fd; |
| 313 | command_output_to_log = False; |
| 314 | VG_(gdb_printf) |
philippe | d4c3744 | 2015-03-21 16:13:08 +0000 | [diff] [blame] | 315 | ("valgrind output will go to log, " |
| 316 | "interactive output will go to gdb\n"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 317 | break; |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame] | 318 | case 6: /* hostvisibility */ |
| 319 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 320 | if (wcmd != NULL) { |
| 321 | switch (VG_(keyword_id) ("yes no", wcmd, kwd_report_all)) { |
| 322 | case -2: |
| 323 | case -1: break; |
| 324 | case 0: |
| 325 | hostvisibility = True; |
| 326 | break; |
| 327 | case 1: |
| 328 | hostvisibility = False; |
| 329 | break; |
florian | e2800c9 | 2014-09-15 20:57:45 +0000 | [diff] [blame] | 330 | default: vg_assert (0); |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame] | 331 | } |
| 332 | } else { |
| 333 | hostvisibility = True; |
| 334 | } |
philippe | 3a73b39 | 2014-06-09 15:47:46 +0000 | [diff] [blame] | 335 | if (hostvisibility) { |
philippe | 72a10e5 | 2014-07-31 21:15:42 +0000 | [diff] [blame] | 336 | const DebugInfo *tooldi |
| 337 | = VG_(find_DebugInfo) ((Addr)handle_gdb_valgrind_command); |
philippe | 45da632 | 2015-03-28 12:23:07 +0000 | [diff] [blame] | 338 | /* Normally, we should always find the tooldi. In case we |
| 339 | do not, suggest a 'likely somewhat working' address: */ |
| 340 | const Addr tool_text_start |
| 341 | = tooldi ? |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 342 | VG_(DebugInfo_get_text_avma) (tooldi) : 0x58000000; |
philippe | 45da632 | 2015-03-28 12:23:07 +0000 | [diff] [blame] | 343 | const NSegment *toolseg |
philippe | 72a10e5 | 2014-07-31 21:15:42 +0000 | [diff] [blame] | 344 | = tooldi ? |
| 345 | VG_(am_find_nsegment) (VG_(DebugInfo_get_text_avma) (tooldi)) |
| 346 | : NULL; |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame] | 347 | VG_(gdb_printf) |
| 348 | ("Enabled access to Valgrind memory/status by GDB\n" |
philippe | 3a73b39 | 2014-06-09 15:47:46 +0000 | [diff] [blame] | 349 | "If not yet done, tell GDB which valgrind file(s) to use, " |
| 350 | "typically:\n" |
philippe | 72a10e5 | 2014-07-31 21:15:42 +0000 | [diff] [blame] | 351 | "add-symbol-file %s %p\n", |
| 352 | toolseg ? VG_(am_get_filename)(toolseg) |
| 353 | : "<toolfile> <address> e.g.", |
philippe | 45da632 | 2015-03-28 12:23:07 +0000 | [diff] [blame] | 354 | (void*)tool_text_start); |
philippe | 3a73b39 | 2014-06-09 15:47:46 +0000 | [diff] [blame] | 355 | } else |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame] | 356 | VG_(gdb_printf) |
| 357 | ("Disabled access to Valgrind memory/status by GDB\n"); |
| 358 | break; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 359 | default: |
| 360 | vg_assert (0); |
| 361 | } |
| 362 | break; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 363 | case 2: /* v.info */ { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 364 | ret = 1; |
| 365 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 366 | switch (kwdid = VG_(keyword_id) |
sewardj | d6e13d8 | 2011-10-22 20:23:30 +0000 | [diff] [blame] | 367 | ("all_errors n_errs_found last_error gdbserver_status memory" |
philippe | 97bfa19 | 2015-06-02 22:09:42 +0000 | [diff] [blame] | 368 | " scheduler stats open_fds exectxt location unwind", |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 369 | wcmd, kwd_report_all)) { |
| 370 | case -2: |
| 371 | case -1: |
| 372 | break; |
| 373 | case 0: // all_errors |
| 374 | // A verbosity of minimum 2 is needed to show the errors. |
| 375 | VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False); |
| 376 | break; |
| 377 | case 1: // n_errs_found |
florian | 654ea86 | 2015-08-06 12:11:33 +0000 | [diff] [blame] | 378 | VG_(printf) ("n_errs_found %u n_errs_shown %u (vgdb-error %d) %s\n", |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 379 | VG_(get_n_errs_found) (), |
| 380 | VG_(get_n_errs_shown) (), |
| 381 | VG_(dyn_vgdb_error), |
| 382 | wordn (mon, 3)); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 383 | break; |
| 384 | case 2: // last_error |
| 385 | VG_(show_last_error)(); |
| 386 | break; |
| 387 | case 3: // gdbserver_status |
| 388 | VG_(gdbserver_status_output)(); |
| 389 | break; |
| 390 | case 4: /* memory */ |
philippe | 9252713 | 2015-05-10 22:40:38 +0000 | [diff] [blame] | 391 | VG_(printf) ("%'13llu bytes have already been mmap-ed ANONYMOUS.\n", |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 392 | VG_(am_get_anonsize_total)()); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 393 | VG_(print_all_arena_stats) (); |
| 394 | if (VG_(clo_profile_heap)) |
| 395 | VG_(print_arena_cc_analysis) (); |
philippe | 93a6a8d | 2012-04-27 22:59:43 +0000 | [diff] [blame] | 396 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 397 | if (wcmd != NULL) { |
| 398 | switch (VG_(keyword_id) ("aspacemgr", wcmd, kwd_report_all)) { |
| 399 | case -2: |
| 400 | case -1: break; |
| 401 | case 0: |
| 402 | VG_(am_show_nsegments) (0, "gdbserver v.info memory aspacemgr"); |
| 403 | break; |
florian | e2800c9 | 2014-09-15 20:57:45 +0000 | [diff] [blame] | 404 | default: vg_assert (0); |
philippe | 93a6a8d | 2012-04-27 22:59:43 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 408 | ret = 1; |
| 409 | break; |
sewardj | d6e13d8 | 2011-10-22 20:23:30 +0000 | [diff] [blame] | 410 | case 5: /* scheduler */ |
philippe | 4f6f336 | 2014-04-19 00:25:54 +0000 | [diff] [blame] | 411 | VG_(show_sched_status) (True, // host_stacktrace |
philippe | 38a74d2 | 2014-08-29 22:53:19 +0000 | [diff] [blame] | 412 | True, // stack_usage |
philippe | 4f6f336 | 2014-04-19 00:25:54 +0000 | [diff] [blame] | 413 | True); // exited_threads |
sewardj | d6e13d8 | 2011-10-22 20:23:30 +0000 | [diff] [blame] | 414 | ret = 1; |
| 415 | break; |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 416 | case 6: /* stats */ |
philippe | 4f6f336 | 2014-04-19 00:25:54 +0000 | [diff] [blame] | 417 | VG_(print_all_stats)(False, /* Memory stats */ |
| 418 | True /* Tool stats */); |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 419 | ret = 1; |
| 420 | break; |
| 421 | case 7: /* open_fds */ |
philippe | c336038 | 2012-10-21 14:37:14 +0000 | [diff] [blame] | 422 | if (VG_(clo_track_fds)) |
| 423 | VG_(show_open_fds) (""); |
| 424 | else |
| 425 | VG_(gdb_printf) |
| 426 | ("Valgrind must be started with --track-fds=yes" |
| 427 | " to show open fds\n"); |
| 428 | ret = 1; |
| 429 | break; |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 430 | case 8: /* exectxt */ |
philippe | f3a6e93 | 2013-01-10 20:42:51 +0000 | [diff] [blame] | 431 | VG_(print_ExeContext_stats) (True /* with_stacktraces */); |
| 432 | ret = 1; |
| 433 | break; |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 434 | case 9: { /* location */ |
| 435 | /* Note: we prefer 'v.info location' and not 'v.info address' as |
| 436 | v.info address is inconsistent with the GDB (native) |
| 437 | command 'info address' which gives the address for a symbol. |
| 438 | GDB equivalent command of 'v.info location' is 'info symbol'. */ |
| 439 | Addr address; |
| 440 | SizeT dummy_sz = 0x1234; |
philippe | d4c3744 | 2015-03-21 16:13:08 +0000 | [diff] [blame] | 441 | if (VG_(strtok_get_address_and_size) (&address, |
| 442 | &dummy_sz, &ssaveptr)) { |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 443 | // If tool provides location information, use that. |
| 444 | if (VG_(needs).info_location) { |
| 445 | VG_TDICT_CALL(tool_info_location, address); |
| 446 | } |
philippe | d4c3744 | 2015-03-21 16:13:08 +0000 | [diff] [blame] | 447 | // If tool does not provide location info, use the common one. |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 448 | // Also use the common to compare with tool when debug log is set. |
| 449 | if (!VG_(needs).info_location || VG_(debugLog_getLevel)() > 0 ) { |
| 450 | AddrInfo ai; |
| 451 | ai.tag = Addr_Undescribed; |
| 452 | VG_(describe_addr) (address, &ai); |
| 453 | VG_(pp_addrinfo) (address, &ai); |
| 454 | VG_(clear_addrinfo) (&ai); |
| 455 | } |
| 456 | } |
| 457 | ret = 1; |
| 458 | break; |
| 459 | } |
philippe | 97bfa19 | 2015-06-02 22:09:42 +0000 | [diff] [blame] | 460 | case 10: { /* unwind */ |
| 461 | Addr address; |
philippe | c403a55 | 2015-06-02 22:17:51 +0000 | [diff] [blame] | 462 | SizeT sz = 1; |
philippe | 97bfa19 | 2015-06-02 22:09:42 +0000 | [diff] [blame] | 463 | if (VG_(strtok_get_address_and_size) (&address, |
| 464 | &sz, &ssaveptr)) { |
| 465 | VG_(ppUnwindInfo) (address, address + sz - 1); |
| 466 | } |
| 467 | ret = 1; |
| 468 | break; |
| 469 | } |
| 470 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 471 | default: |
| 472 | vg_assert(0); |
| 473 | } |
| 474 | break; |
| 475 | } |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 476 | case 3: /* v.wait */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 477 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 478 | if (wcmd != NULL) { |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 479 | int_value = strtol (wcmd, NULL, 10); |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 480 | VG_(printf) ("gdbserver: continuing in %d ms ...\n", int_value); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 481 | VG_(poll)(NULL, 0, int_value); |
| 482 | } |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 483 | VG_(printf) ("gdbserver: continuing after wait ...\n"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 484 | ret = 1; |
| 485 | break; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 486 | case 4: /* v.kill */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 487 | kill_request ("monitor command request to kill this process\n"); |
| 488 | break; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 489 | case 5: { /* v.translate */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 490 | Addr address; |
| 491 | SizeT verbosity = 0x20; |
| 492 | |
| 493 | ret = 1; |
| 494 | |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 495 | if (VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr)) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 496 | /* we need to force the output to log for the translation trace, |
| 497 | as low level VEX tracing cannot be redirected to gdb. */ |
| 498 | int saved_command_output_to_log = command_output_to_log; |
| 499 | int saved_fd = VG_(log_output_sink).fd; |
| 500 | Bool single_stepping_on_entry = valgrind_single_stepping(); |
| 501 | int vex_verbosity = verbosity & 0xff; |
| 502 | VG_(log_output_sink).fd = initial_valgrind_sink.fd; |
| 503 | if ((verbosity & 0x100) && !single_stepping_on_entry) { |
| 504 | valgrind_set_single_stepping(True); |
| 505 | // to force gdbserver instrumentation. |
| 506 | } |
sewardj | 99d6134 | 2011-05-17 16:35:11 +0000 | [diff] [blame] | 507 | # if defined(VGA_arm) |
| 508 | // on arm, we need to (potentially) convert this address |
| 509 | // to the thumb form. |
| 510 | address = thumb_pc (address); |
| 511 | # endif |
| 512 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 513 | VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/, |
| 514 | address, |
| 515 | /*debugging*/True, |
| 516 | (Int) vex_verbosity, |
| 517 | /*bbs_done*/0, |
| 518 | /*allow redir?*/True); |
| 519 | if ((verbosity & 0x100) && !single_stepping_on_entry) { |
| 520 | valgrind_set_single_stepping(False); |
| 521 | // reset single stepping. |
| 522 | } |
| 523 | command_output_to_log = saved_command_output_to_log; |
| 524 | VG_(log_output_sink).fd = saved_fd; |
| 525 | } |
| 526 | break; |
| 527 | } |
| 528 | |
philippe | 6ec8d63 | 2013-01-23 22:10:28 +0000 | [diff] [blame] | 529 | case 6: /* v.do */ |
| 530 | ret = 1; |
| 531 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 532 | switch (VG_(keyword_id) ("expensive_sanity_check_general", |
| 533 | wcmd, kwd_report_all)) { |
| 534 | case -2: |
| 535 | case -1: break; |
| 536 | case 0: { /* expensive_sanity_check_general */ |
| 537 | // Temporarily bump up sanity level to check e.g. the malloc arenas. |
| 538 | const Int save_clo_sanity_level = VG_(clo_sanity_level); |
| 539 | if (VG_(clo_sanity_level) < 4) VG_(clo_sanity_level) = 4; |
| 540 | VG_(sanity_check_general) (/* force_expensive */ True); |
| 541 | VG_(clo_sanity_level) = save_clo_sanity_level; |
| 542 | break; |
| 543 | } |
florian | e2800c9 | 2014-09-15 20:57:45 +0000 | [diff] [blame] | 544 | default: vg_assert (0); |
philippe | 6ec8d63 | 2013-01-23 22:10:28 +0000 | [diff] [blame] | 545 | } |
| 546 | break; |
| 547 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 548 | default: |
| 549 | vg_assert (0); |
| 550 | } |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | /* handle_gdb_monitor_command handles the provided mon string command, |
| 555 | which can be either a "standard" valgrind monitor command |
| 556 | or a tool specific monitor command. |
| 557 | If command recognised, return 1 else return 0. |
| 558 | Note that in case of ambiguous command, 1 is returned. |
| 559 | */ |
| 560 | static |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 561 | int handle_gdb_monitor_command (char *mon) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 562 | { |
| 563 | UWord ret = 0; |
| 564 | UWord tool_ret = 0; |
| 565 | // initially, we assume that when returning, the desired sink is the |
| 566 | // one we have when entering. It can however be changed by the standard |
| 567 | // valgrind command handling. |
| 568 | OutputSink sink_wanted_at_return = VG_(log_output_sink); |
philippe | d4c3744 | 2015-03-21 16:13:08 +0000 | [diff] [blame] | 569 | // When using gdbserver, we temporarily disable xml output. |
| 570 | Bool save_clo_xml = VG_(clo_xml); |
| 571 | VG_(clo_xml) = False; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 572 | |
| 573 | if (!initial_valgrind_sink_saved) { |
| 574 | /* first time we enter here, we save the valgrind default log sink */ |
| 575 | initial_valgrind_sink = sink_wanted_at_return; |
| 576 | initial_valgrind_sink_saved = True; |
| 577 | } |
| 578 | |
| 579 | if (!command_output_to_log) |
| 580 | VG_(log_output_sink).fd = -2; /* redirect to monitor_output */ |
| 581 | |
| 582 | ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return); |
| 583 | |
| 584 | /* Even if command was recognised by valgrind core, we call the |
| 585 | tool command handler : this is needed to handle help command |
| 586 | and/or to let the tool do some additional processing of a |
| 587 | valgrind standard command. Note however that if valgrind |
| 588 | recognised the command, we will always return success. */ |
| 589 | if (VG_(needs).client_requests) { |
| 590 | /* If the tool reports an error when handling a monitor command, |
| 591 | we need to avoid calling gdbserver during this command |
| 592 | handling. So, we temporarily set VG_(dyn_vgdb_error) to |
| 593 | a huge value to ensure m_errormgr.c does not call gdbserver. */ |
| 594 | Int save_dyn_vgdb_error = VG_(dyn_vgdb_error); |
| 595 | UWord arg[2]; |
| 596 | VG_(dyn_vgdb_error) = 999999999; |
| 597 | arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND; |
| 598 | arg[1] = (UWord) mon; |
| 599 | VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg, |
| 600 | &tool_ret); |
| 601 | VG_(dyn_vgdb_error) = save_dyn_vgdb_error; |
| 602 | } |
| 603 | |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 604 | VG_(message_flush) (); |
| 605 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 606 | /* restore or set the desired output */ |
| 607 | VG_(log_output_sink).fd = sink_wanted_at_return.fd; |
philippe | d4c3744 | 2015-03-21 16:13:08 +0000 | [diff] [blame] | 608 | VG_(clo_xml) = save_clo_xml; |
| 609 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 610 | if (ret | tool_ret) |
| 611 | return 1; |
| 612 | else |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | |
| 617 | /* Handle all of the extended 'Q' packets. */ |
| 618 | static |
| 619 | void handle_set (char *arg_own_buf, int *new_packet_len_p) |
| 620 | { |
| 621 | if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) { |
| 622 | noack_mode = True; |
| 623 | write_ok (arg_own_buf); |
| 624 | return; |
| 625 | } |
| 626 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 627 | if (strcmp ("QCatchSyscalls:0", arg_own_buf) == 0) { |
| 628 | dlog (3, "catch syscall all off\n"); |
| 629 | catching_syscalls = False; |
| 630 | write_ok (arg_own_buf); |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | const char *q1 = "QCatchSyscalls:1"; |
| 635 | if (strncmp (q1, arg_own_buf, strlen(q1)) == 0) { |
| 636 | Int i; |
| 637 | const char *p; |
| 638 | |
| 639 | if (syscalls_to_catch != NULL) { |
| 640 | free (syscalls_to_catch); |
| 641 | syscalls_to_catch = NULL; |
| 642 | } |
| 643 | syscalls_to_catch_size = 0; |
| 644 | p = arg_own_buf + strlen(q1); |
| 645 | while (*p) { |
| 646 | if (*p++ == ';') |
| 647 | syscalls_to_catch_size++; |
| 648 | } |
| 649 | if (syscalls_to_catch_size > 0) { |
| 650 | CORE_ADDR sysno; |
| 651 | char *from, *to; |
| 652 | |
| 653 | syscalls_to_catch = malloc (syscalls_to_catch_size * sizeof (int)); |
| 654 | |
| 655 | from = strchr (arg_own_buf, ';') + 1; |
| 656 | for (i = 0; i < syscalls_to_catch_size; i++) { |
| 657 | to = strchr (from, ';'); |
| 658 | if (to == NULL) |
| 659 | to = arg_own_buf + strlen (arg_own_buf); |
| 660 | decode_address (&sysno, from, to - from); |
| 661 | syscalls_to_catch[i] = (Int)sysno; |
| 662 | dlog(4, "catch syscall sysno %d\n", (int)sysno); |
| 663 | from = to; |
| 664 | if (*from == ';') from++; |
| 665 | } |
| 666 | } else |
| 667 | dlog (4, "catch syscall all sysno\n"); |
| 668 | catching_syscalls = True; |
| 669 | write_ok (arg_own_buf); |
| 670 | return; |
| 671 | } |
| 672 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 673 | if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) { |
| 674 | int i; |
| 675 | char *from, *to; |
| 676 | char *end = arg_own_buf + strlen(arg_own_buf); |
| 677 | CORE_ADDR sig; |
| 678 | for (i = 0; i < TARGET_SIGNAL_LAST; i++) |
| 679 | pass_signals[i] = 0; |
| 680 | |
| 681 | from = arg_own_buf + 13; |
| 682 | while (from < end) { |
| 683 | to = strchr(from, ';'); |
| 684 | if (to == NULL) to = end; |
| 685 | decode_address (&sig, from, to - from); |
| 686 | pass_signals[(int)sig] = 1; |
philippe | 75d8a4e | 2015-03-26 21:32:03 +0000 | [diff] [blame] | 687 | dlog(3, "pass_signal gdb_nr %d %s\n", |
philippe | 886fde3 | 2012-03-29 21:56:47 +0000 | [diff] [blame] | 688 | (int)sig, target_signal_to_name(sig)); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 689 | from = to; |
| 690 | if (*from == ';') from++; |
| 691 | } |
| 692 | write_ok (arg_own_buf); |
| 693 | return; |
| 694 | } |
| 695 | /* Otherwise we didn't know what packet it was. Say we didn't |
| 696 | understand it. */ |
| 697 | arg_own_buf[0] = 0; |
| 698 | } |
| 699 | |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 700 | Bool VG_(client_monitor_command) (HChar *cmd) |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 701 | { |
| 702 | const Bool connected = remote_connected(); |
| 703 | const int saved_command_output_to_log = command_output_to_log; |
| 704 | Bool handled; |
| 705 | |
| 706 | if (!connected) |
| 707 | command_output_to_log = True; |
| 708 | handled = handle_gdb_monitor_command (cmd); |
| 709 | if (!connected) { |
| 710 | // reset the log output unless cmd changed it. |
| 711 | if (command_output_to_log) |
| 712 | command_output_to_log = saved_command_output_to_log; |
| 713 | } |
| 714 | if (handled) |
| 715 | return False; // recognised |
| 716 | else |
| 717 | return True; // not recognised |
| 718 | } |
| 719 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 720 | /* Handle all of the extended 'q' packets. */ |
| 721 | static |
| 722 | void handle_query (char *arg_own_buf, int *new_packet_len_p) |
| 723 | { |
| 724 | static struct inferior_list_entry *thread_ptr; |
| 725 | |
philippe | 1670b05 | 2014-08-15 10:27:52 +0000 | [diff] [blame] | 726 | /* thread local storage query */ |
| 727 | if (strncmp ("qGetTLSAddr:", arg_own_buf, 12) == 0) { |
| 728 | char *from, *to; |
| 729 | char *end = arg_own_buf + strlen(arg_own_buf); |
| 730 | unsigned long gdb_id; |
| 731 | CORE_ADDR lm; |
| 732 | CORE_ADDR offset; |
| 733 | struct thread_info *ti; |
| 734 | |
| 735 | from = arg_own_buf + 12; |
| 736 | to = strchr(from, ','); |
| 737 | *to = 0; |
| 738 | gdb_id = strtoul (from, NULL, 16); |
| 739 | from = to + 1; |
| 740 | to = strchr(from, ','); |
| 741 | decode_address (&offset, from, to - from); |
| 742 | from = to + 1; |
| 743 | to = end; |
| 744 | decode_address (&lm, from, to - from); |
| 745 | dlog(2, "qGetTLSAddr thread %lu offset %p lm %p\n", |
| 746 | gdb_id, (void*)offset, (void*)lm); |
| 747 | |
| 748 | ti = gdb_id_to_thread (gdb_id); |
| 749 | if (ti != NULL) { |
| 750 | ThreadState *tst; |
| 751 | Addr tls_addr; |
| 752 | |
| 753 | tst = (ThreadState *) inferior_target_data (ti); |
| 754 | if (valgrind_get_tls_addr(tst, offset, lm, &tls_addr)) { |
| 755 | VG_(sprintf) (arg_own_buf, "%lx", tls_addr); |
| 756 | return; |
| 757 | } |
| 758 | // else we will report we do not support qGetTLSAddr |
| 759 | } else { |
| 760 | write_enn (arg_own_buf); |
| 761 | return; |
| 762 | } |
| 763 | } |
| 764 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 765 | /* qRcmd, monitor command handling. */ |
| 766 | if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) { |
| 767 | char *p = arg_own_buf + 6; |
| 768 | int cmdlen = strlen(p)/2; |
| 769 | char cmd[cmdlen+1]; |
| 770 | |
| 771 | if (unhexify (cmd, p, cmdlen) != cmdlen) { |
| 772 | write_enn (arg_own_buf); |
| 773 | return; |
| 774 | } |
| 775 | cmd[cmdlen] = '\0'; |
| 776 | |
| 777 | if (handle_gdb_monitor_command (cmd)) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 778 | write_ok (arg_own_buf); |
| 779 | return; |
| 780 | } else { |
| 781 | /* cmd not recognised */ |
| 782 | VG_(gdb_printf) |
| 783 | ("command '%s' not recognised\n" |
| 784 | "In gdb, try 'monitor help'\n" |
| 785 | "In a shell, try 'vgdb help'\n", |
| 786 | cmd); |
| 787 | write_ok (arg_own_buf); |
| 788 | return; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | /* provide some valgrind specific info in return to qThreadExtraInfo. */ |
| 793 | if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) { |
| 794 | unsigned long gdb_id; |
| 795 | struct thread_info *ti; |
| 796 | ThreadState *tst; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 797 | |
| 798 | gdb_id = strtoul (&arg_own_buf[17], NULL, 16); |
| 799 | ti = gdb_id_to_thread (gdb_id); |
| 800 | if (ti != NULL) { |
| 801 | tst = (ThreadState *) inferior_target_data (ti); |
florian | 4978951 | 2013-09-16 17:08:50 +0000 | [diff] [blame] | 802 | /* Additional info is the tid, the thread status and the thread's |
| 803 | name, if any. */ |
florian | 7b7d594 | 2014-12-19 20:29:22 +0000 | [diff] [blame] | 804 | SizeT len = strlen(VG_(name_of_ThreadStatus)(tst->status)) + 20; |
| 805 | if (tst->thread_name) len += strlen(tst->thread_name); |
| 806 | /* As the string will be hexified and copied into own_buf we need |
| 807 | to limit the length to avoid buffer overflow. */ |
| 808 | if (len * 2 > (PBUFSIZ + POVERHSIZ)) |
| 809 | len = (PBUFSIZ + POVERHSIZ) / 2; |
| 810 | char status[len]; |
florian | 4978951 | 2013-09-16 17:08:50 +0000 | [diff] [blame] | 811 | if (tst->thread_name) { |
florian | 654ea86 | 2015-08-06 12:11:33 +0000 | [diff] [blame] | 812 | VG_(snprintf) (status, sizeof(status), "tid %u %s %s", |
florian | 4978951 | 2013-09-16 17:08:50 +0000 | [diff] [blame] | 813 | tst->tid, |
| 814 | VG_(name_of_ThreadStatus)(tst->status), |
| 815 | tst->thread_name); |
| 816 | } else { |
florian | 654ea86 | 2015-08-06 12:11:33 +0000 | [diff] [blame] | 817 | VG_(snprintf) (status, sizeof(status), "tid %u %s", |
florian | 4978951 | 2013-09-16 17:08:50 +0000 | [diff] [blame] | 818 | tst->tid, |
| 819 | VG_(name_of_ThreadStatus)(tst->status)); |
| 820 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 821 | hexify (arg_own_buf, status, strlen(status)); |
| 822 | return; |
| 823 | } else { |
| 824 | write_enn (arg_own_buf); |
| 825 | return; |
| 826 | } |
| 827 | } |
philippe | 1670b05 | 2014-08-15 10:27:52 +0000 | [diff] [blame] | 828 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 829 | if (strcmp ("qAttached", arg_own_buf) == 0) { |
| 830 | /* tell gdb to always detach, never kill the process */ |
| 831 | arg_own_buf[0] = '1'; |
| 832 | arg_own_buf[1] = 0; |
| 833 | return; |
| 834 | } |
| 835 | |
| 836 | if (strcmp ("qSymbol::", arg_own_buf) == 0) { |
| 837 | /* We have no symbol to read. */ |
| 838 | write_ok (arg_own_buf); |
| 839 | return; |
| 840 | } |
| 841 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 842 | if (strcmp ("qC", arg_own_buf) == 0) { |
| 843 | VG_(sprintf) (arg_own_buf, "QC%x", |
| 844 | thread_to_gdb_id (current_inferior)); |
| 845 | return; |
| 846 | } |
| 847 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 848 | if (strcmp ("qfThreadInfo", arg_own_buf) == 0) { |
| 849 | thread_ptr = all_threads.head; |
| 850 | VG_(sprintf) (arg_own_buf, "m%x", |
| 851 | thread_to_gdb_id ((struct thread_info *)thread_ptr)); |
| 852 | thread_ptr = thread_ptr->next; |
| 853 | return; |
| 854 | } |
| 855 | |
| 856 | if (strcmp ("qsThreadInfo", arg_own_buf) == 0) { |
| 857 | if (thread_ptr != NULL) { |
| 858 | VG_(sprintf) (arg_own_buf, "m%x", |
| 859 | thread_to_gdb_id ((struct thread_info *)thread_ptr)); |
| 860 | thread_ptr = thread_ptr->next; |
| 861 | return; |
| 862 | } else { |
| 863 | VG_(sprintf) (arg_own_buf, "l"); |
| 864 | return; |
| 865 | } |
| 866 | } |
| 867 | |
philippe | 419d5f2 | 2012-05-24 21:33:17 +0000 | [diff] [blame] | 868 | if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 869 | && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) { |
| 870 | CORE_ADDR ofs; |
| 871 | unsigned int len, doc_len; |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 872 | const char *annex = NULL; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 873 | // First, the annex is extracted from the packet received. |
| 874 | // Then, it is replaced by the corresponding file name. |
| 875 | int fd; |
| 876 | |
| 877 | /* Grab the annex, offset, and length. */ |
| 878 | if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) { |
| 879 | strcpy (arg_own_buf, "E00"); |
| 880 | return; |
| 881 | } |
| 882 | |
| 883 | if (strcmp (annex, "target.xml") == 0) { |
philippe | 419d5f2 | 2012-05-24 21:33:17 +0000 | [diff] [blame] | 884 | annex = valgrind_target_xml(VG_(clo_vgdb_shadow_registers)); |
| 885 | if (annex != NULL && VG_(clo_vgdb_shadow_registers)) { |
| 886 | /* Ensure the shadow registers are initialized. */ |
| 887 | initialize_shadow_low(True); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 888 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 889 | if (annex == NULL) { |
| 890 | strcpy (arg_own_buf, "E00"); |
| 891 | return; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | { |
philippe | 75a5f78 | 2012-02-24 11:25:58 +0000 | [diff] [blame] | 896 | char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1]; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 897 | struct vg_stat stat_doc; |
| 898 | char toread[len]; |
| 899 | int len_read; |
| 900 | |
| 901 | VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex); |
| 902 | fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0); |
| 903 | if (fd == -1) { |
| 904 | strcpy (arg_own_buf, "E00"); |
| 905 | return; |
| 906 | } |
| 907 | if (VG_(fstat) (fd, &stat_doc) != 0) { |
| 908 | VG_(close) (fd); |
| 909 | strcpy (arg_own_buf, "E00"); |
| 910 | return; |
| 911 | } |
| 912 | doc_len = stat_doc.size; |
| 913 | |
| 914 | if (len > PBUFSIZ - POVERHSIZ) |
| 915 | len = PBUFSIZ - POVERHSIZ; |
| 916 | |
| 917 | if (ofs > doc_len) { |
| 918 | write_enn (arg_own_buf); |
| 919 | VG_(close) (fd); |
| 920 | return; |
| 921 | } |
| 922 | VG_(lseek) (fd, ofs, VKI_SEEK_SET); |
| 923 | len_read = VG_(read) (fd, toread, len); |
philippe | b301469 | 2015-05-17 13:38:25 +0000 | [diff] [blame] | 924 | *new_packet_len_p = write_qxfer_response (arg_own_buf, |
| 925 | (unsigned char *)toread, |
| 926 | len_read, |
| 927 | ofs + len_read < doc_len); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 928 | VG_(close) (fd); |
| 929 | return; |
| 930 | } |
| 931 | } |
| 932 | |
sewardj | 2a31239 | 2011-06-26 09:26:48 +0000 | [diff] [blame] | 933 | if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) { |
| 934 | unsigned char *data; |
| 935 | int n; |
| 936 | CORE_ADDR ofs; |
| 937 | unsigned int len; |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 938 | const char *annex; |
sewardj | 2a31239 | 2011-06-26 09:26:48 +0000 | [diff] [blame] | 939 | |
| 940 | /* Reject any annex; grab the offset and length. */ |
| 941 | if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0 |
| 942 | || annex[0] != '\0') { |
| 943 | strcpy (arg_own_buf, "E00"); |
| 944 | return; |
| 945 | } |
| 946 | |
philippe | b301469 | 2015-05-17 13:38:25 +0000 | [diff] [blame] | 947 | if (len > PBUFSIZ - POVERHSIZ) |
| 948 | len = PBUFSIZ - POVERHSIZ; |
sewardj | 2a31239 | 2011-06-26 09:26:48 +0000 | [diff] [blame] | 949 | data = malloc (len); |
| 950 | |
| 951 | { |
| 952 | UWord *client_auxv = VG_(client_auxv); |
| 953 | unsigned int client_auxv_len = 0; |
| 954 | while (*client_auxv != 0) { |
florian | 654ea86 | 2015-08-06 12:11:33 +0000 | [diff] [blame] | 955 | dlog(4, "auxv %llu %llx\n", |
sewardj | 2a31239 | 2011-06-26 09:26:48 +0000 | [diff] [blame] | 956 | (ULong)*client_auxv, |
| 957 | (ULong)*(client_auxv+1)); |
| 958 | client_auxv++; |
| 959 | client_auxv++; |
| 960 | client_auxv_len += 2 * sizeof(UWord); |
| 961 | } |
| 962 | client_auxv_len += 2 * sizeof(UWord); |
florian | 654ea86 | 2015-08-06 12:11:33 +0000 | [diff] [blame] | 963 | dlog(4, "auxv len %u\n", client_auxv_len); |
sewardj | 2a31239 | 2011-06-26 09:26:48 +0000 | [diff] [blame] | 964 | |
| 965 | if (ofs >= client_auxv_len) |
| 966 | n = -1; |
| 967 | else { |
| 968 | n = client_auxv_len - ofs; |
| 969 | VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | if (n < 0) |
| 974 | write_enn (arg_own_buf); |
| 975 | else if (n > len) |
| 976 | *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1); |
| 977 | else |
| 978 | *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0); |
| 979 | |
| 980 | free (data); |
| 981 | |
| 982 | return; |
| 983 | } |
| 984 | |
philippe | 5e94f05 | 2015-05-14 19:56:47 +0000 | [diff] [blame] | 985 | if (strncmp ("qXfer:exec-file:read:", arg_own_buf, 21) == 0) { |
| 986 | unsigned char *data; |
| 987 | int n; |
| 988 | CORE_ADDR ofs; |
| 989 | unsigned int len; |
| 990 | const char *annex; |
| 991 | unsigned long pid; |
| 992 | const HChar *name; |
| 993 | |
philippe | b301469 | 2015-05-17 13:38:25 +0000 | [diff] [blame] | 994 | /* grab the annex, offset and length. */ |
philippe | 5e94f05 | 2015-05-14 19:56:47 +0000 | [diff] [blame] | 995 | if (decode_xfer_read (arg_own_buf + 21, &annex, &ofs, &len) < 0) { |
| 996 | strcpy (arg_own_buf, "E00"); |
| 997 | return; |
| 998 | } |
| 999 | |
philippe | b301469 | 2015-05-17 13:38:25 +0000 | [diff] [blame] | 1000 | /* Reject any annex with invalid/unexpected pid */ |
philippe | 5e94f05 | 2015-05-14 19:56:47 +0000 | [diff] [blame] | 1001 | if (strlen(annex) > 0) |
| 1002 | pid = strtoul (annex, NULL, 16); |
| 1003 | else |
| 1004 | pid = 0; |
| 1005 | if ((int)pid != VG_(getpid)() && pid != 0) { |
| 1006 | VG_(sprintf) (arg_own_buf, |
| 1007 | "E.Valgrind gdbserver pid is %d." |
| 1008 | " Cannot give info for pid %d", |
| 1009 | VG_(getpid)(), (int) pid); |
| 1010 | return; |
| 1011 | } |
| 1012 | |
| 1013 | if (len > PBUFSIZ - 2) |
| 1014 | len = PBUFSIZ - 2; |
| 1015 | data = malloc (len); |
| 1016 | |
| 1017 | if (!VG_(resolve_filename)(VG_(cl_exec_fd), &name)) { |
| 1018 | VG_(sprintf) (arg_own_buf, |
| 1019 | "E.Valgrind gdbserver could not" |
| 1020 | " resolve pid %d exec filename.", |
| 1021 | VG_(getpid)()); |
| 1022 | return; |
| 1023 | } |
| 1024 | |
| 1025 | if (ofs >= strlen(name)) |
| 1026 | n = -1; |
| 1027 | else { |
| 1028 | n = strlen(name) - ofs; |
| 1029 | VG_(memcpy) (data, name, n); |
| 1030 | } |
| 1031 | |
| 1032 | if (n < 0) |
| 1033 | write_enn (arg_own_buf); |
| 1034 | else if (n > len) |
| 1035 | *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1); |
| 1036 | else |
| 1037 | *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0); |
| 1038 | |
| 1039 | free (data); |
| 1040 | |
| 1041 | return; |
| 1042 | } |
sewardj | 2a31239 | 2011-06-26 09:26:48 +0000 | [diff] [blame] | 1043 | |
philippe | b301469 | 2015-05-17 13:38:25 +0000 | [diff] [blame] | 1044 | if (strncmp ("qXfer:siginfo:read:", arg_own_buf, 19) == 0) { |
| 1045 | vki_siginfo_t info; |
| 1046 | int n; |
| 1047 | CORE_ADDR ofs; |
| 1048 | unsigned int len; |
| 1049 | const char *annex; |
| 1050 | |
| 1051 | /* Reject any annex; grab the offset and length. */ |
| 1052 | if (decode_xfer_read (arg_own_buf + 19, &annex, &ofs, &len) < 0 |
| 1053 | || annex[0] != '\0') { |
| 1054 | strcpy (arg_own_buf, "E00"); |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | if (len > PBUFSIZ - POVERHSIZ) |
| 1059 | len = PBUFSIZ - POVERHSIZ; |
| 1060 | |
| 1061 | gdbserver_pending_signal_to_report(&info); |
| 1062 | |
| 1063 | if (ofs >= sizeof(info)) |
| 1064 | n = -1; |
| 1065 | else |
| 1066 | n = sizeof(info) - ofs; |
| 1067 | |
| 1068 | if (n < 0) |
| 1069 | write_enn (arg_own_buf); |
| 1070 | else if (n > len) |
| 1071 | *new_packet_len_p = write_qxfer_response (arg_own_buf, |
| 1072 | (unsigned char *)&info, |
| 1073 | len, 1); |
| 1074 | else |
| 1075 | *new_packet_len_p = write_qxfer_response (arg_own_buf, |
| 1076 | (unsigned char *)&info, |
| 1077 | n, 0); |
| 1078 | |
| 1079 | return; |
| 1080 | } |
| 1081 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1082 | /* Protocol features query. */ |
| 1083 | if (strncmp ("qSupported", arg_own_buf, 10) == 0 |
| 1084 | && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) { |
florian | 654ea86 | 2015-08-06 12:11:33 +0000 | [diff] [blame] | 1085 | VG_(sprintf) (arg_own_buf, "PacketSize=%x", (UInt)PBUFSIZ - 1); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1086 | /* Note: max packet size including frame and checksum, but without |
| 1087 | trailing null byte, which is not sent/received. */ |
philippe | 5e94f05 | 2015-05-14 19:56:47 +0000 | [diff] [blame] | 1088 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1089 | strcat (arg_own_buf, ";QStartNoAckMode+"); |
| 1090 | strcat (arg_own_buf, ";QPassSignals+"); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 1091 | strcat (arg_own_buf, ";QCatchSyscalls+"); |
sewardj | 2a31239 | 2011-06-26 09:26:48 +0000 | [diff] [blame] | 1092 | if (VG_(client_auxv)) |
| 1093 | strcat (arg_own_buf, ";qXfer:auxv:read+"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1094 | |
philippe | 419d5f2 | 2012-05-24 21:33:17 +0000 | [diff] [blame] | 1095 | if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1096 | strcat (arg_own_buf, ";qXfer:features:read+"); |
| 1097 | /* if a new gdb connects to us, we have to reset the register |
| 1098 | set to the normal register sets to allow this new gdb to |
| 1099 | decide to use or not the shadow registers. |
| 1100 | |
| 1101 | Note that the reset is only done for gdb that are sending |
| 1102 | qSupported packets. If a user first connected with a recent |
| 1103 | gdb using shadow registers and then with a very old gdb |
| 1104 | that does not use qSupported packet, then the old gdb will |
| 1105 | not properly connect. */ |
| 1106 | initialize_shadow_low(False); |
| 1107 | } |
philippe | 5e94f05 | 2015-05-14 19:56:47 +0000 | [diff] [blame] | 1108 | strcat (arg_own_buf, ";qXfer:exec-file:read+"); |
philippe | b301469 | 2015-05-17 13:38:25 +0000 | [diff] [blame] | 1109 | strcat (arg_own_buf, ";qXfer:siginfo:read+"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | /* Otherwise we didn't know what packet it was. Say we didn't |
| 1114 | understand it. */ |
| 1115 | arg_own_buf[0] = 0; |
| 1116 | } |
| 1117 | |
| 1118 | /* Handle all of the extended 'v' packets. */ |
| 1119 | static |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1120 | void handle_v_requests (char *arg_own_buf, char *status, int *zignal) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1121 | { |
| 1122 | /* vcont packet code from gdb 6.6 removed */ |
| 1123 | |
| 1124 | /* Otherwise we didn't know what packet it was. Say we didn't |
| 1125 | understand it. */ |
| 1126 | arg_own_buf[0] = 0; |
| 1127 | return; |
| 1128 | } |
| 1129 | |
| 1130 | static |
| 1131 | void myresume (int step, int sig) |
| 1132 | { |
| 1133 | struct thread_resume resume_info[2]; |
| 1134 | int n = 0; |
| 1135 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1136 | if (step || sig) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1137 | resume_info[0].step = step; |
| 1138 | resume_info[0].sig = sig; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1139 | n++; |
| 1140 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1141 | resume_info[n].step = 0; |
| 1142 | resume_info[n].sig = 0; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1143 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1144 | resume_reply_packet_needed = True; |
| 1145 | valgrind_resume (resume_info); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | /* server_main global variables */ |
| 1149 | static char *own_buf; |
| 1150 | static unsigned char *mem_buf; |
| 1151 | |
| 1152 | void gdbserver_init (void) |
| 1153 | { |
| 1154 | dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version); |
| 1155 | noack_mode = False; |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1156 | valgrind_initialize_target (); |
philippe | 0e1cac9 | 2012-02-28 22:37:44 +0000 | [diff] [blame] | 1157 | // After a fork, gdbserver_init can be called again. |
| 1158 | // We do not have to re-malloc the buffers in such a case. |
| 1159 | if (own_buf == NULL) |
philippe | 03ffc6e | 2013-07-25 22:37:02 +0000 | [diff] [blame] | 1160 | own_buf = malloc (PBUFSIZ+POVERHSIZ); |
philippe | 0e1cac9 | 2012-02-28 22:37:44 +0000 | [diff] [blame] | 1161 | if (mem_buf == NULL) |
philippe | 03ffc6e | 2013-07-25 22:37:02 +0000 | [diff] [blame] | 1162 | mem_buf = malloc (PBUFSIZ+POVERHSIZ); |
| 1163 | // Note: normally, we should only malloc PBUFSIZ. However, |
| 1164 | // GDB has a bug, and in some cases, sends e.g. 'm' packets |
| 1165 | // asking for slightly more than the PacketSize given at |
| 1166 | // connection initialisation. So, we bypass the GDB bug |
| 1167 | // by allocating slightly more. |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | void gdbserver_terminate (void) |
| 1171 | { |
| 1172 | /* last call to gdbserver is cleanup call */ |
| 1173 | if (VG_MINIMAL_SETJMP(toplevel)) { |
| 1174 | dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n"); |
| 1175 | return; |
| 1176 | } |
| 1177 | remote_close(); |
| 1178 | } |
| 1179 | |
| 1180 | void server_main (void) |
| 1181 | { |
| 1182 | static char status; |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1183 | static int zignal; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1184 | |
| 1185 | char ch; |
| 1186 | int i = 0; |
| 1187 | unsigned int len; |
| 1188 | CORE_ADDR mem_addr; |
| 1189 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1190 | zignal = valgrind_wait (&status); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1191 | if (VG_MINIMAL_SETJMP(toplevel)) { |
| 1192 | dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n"); |
| 1193 | } |
| 1194 | while (1) { |
| 1195 | unsigned char sig; |
| 1196 | int packet_len; |
| 1197 | int new_packet_len = -1; |
| 1198 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1199 | if (resume_reply_packet_needed) { |
| 1200 | /* Send the resume reply to reply to last GDB resume |
| 1201 | request. */ |
| 1202 | resume_reply_packet_needed = False; |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1203 | prepare_resume_reply (own_buf, status, zignal); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1204 | putpkt (own_buf); |
| 1205 | } |
| 1206 | |
philippe | b301469 | 2015-05-17 13:38:25 +0000 | [diff] [blame] | 1207 | /* If our status is terminal (exit or fatal signal) get out |
philippe | 0447bbd | 2012-10-17 21:32:03 +0000 | [diff] [blame] | 1208 | as quickly as we can. We won't be able to handle any request |
| 1209 | anymore. */ |
| 1210 | if (status == 'W' || status == 'X') { |
| 1211 | return; |
| 1212 | } |
| 1213 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1214 | packet_len = getpkt (own_buf); |
| 1215 | if (packet_len <= 0) |
| 1216 | break; |
| 1217 | |
| 1218 | i = 0; |
| 1219 | ch = own_buf[i++]; |
| 1220 | switch (ch) { |
| 1221 | case 'Q': |
| 1222 | handle_set (own_buf, &new_packet_len); |
| 1223 | break; |
| 1224 | case 'q': |
| 1225 | handle_query (own_buf, &new_packet_len); |
| 1226 | break; |
| 1227 | case 'd': |
| 1228 | /* set/unset debugging is done through valgrind debug level. */ |
| 1229 | own_buf[0] = '\0'; |
| 1230 | break; |
| 1231 | case 'D': |
| 1232 | reset_valgrind_sink("gdb detaching from process"); |
| 1233 | |
| 1234 | /* When detaching or kill the process, gdb expects to get |
| 1235 | an packet OK back. Any other output will make gdb |
| 1236 | believes detach did not work. */ |
| 1237 | write_ok (own_buf); |
| 1238 | putpkt (own_buf); |
| 1239 | remote_finish (reset_after_error); |
| 1240 | remote_open (VG_(clo_vgdb_prefix)); |
| 1241 | myresume (0, 0); |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1242 | resume_reply_packet_needed = False; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1243 | return; |
| 1244 | case '!': |
| 1245 | /* We can not use the extended protocol with valgrind, |
| 1246 | because we can not restart the running |
| 1247 | program. So return unrecognized. */ |
| 1248 | own_buf[0] = '\0'; |
| 1249 | break; |
| 1250 | case '?': |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1251 | prepare_resume_reply (own_buf, status, zignal); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1252 | break; |
| 1253 | case 'H': |
| 1254 | if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') { |
| 1255 | unsigned long gdb_id, thread_id; |
| 1256 | |
| 1257 | gdb_id = strtoul (&own_buf[2], NULL, 16); |
| 1258 | thread_id = gdb_id_to_thread_id (gdb_id); |
| 1259 | if (thread_id == 0) { |
| 1260 | write_enn (own_buf); |
| 1261 | break; |
| 1262 | } |
| 1263 | |
| 1264 | if (own_buf[1] == 'g') { |
| 1265 | general_thread = thread_id; |
| 1266 | set_desired_inferior (1); |
| 1267 | } else if (own_buf[1] == 'c') { |
| 1268 | cont_thread = thread_id; |
| 1269 | } else if (own_buf[1] == 's') { |
| 1270 | step_thread = thread_id; |
| 1271 | } |
| 1272 | |
| 1273 | write_ok (own_buf); |
| 1274 | } else { |
| 1275 | /* Silently ignore it so that gdb can extend the protocol |
| 1276 | without compatibility headaches. */ |
| 1277 | own_buf[0] = '\0'; |
| 1278 | } |
| 1279 | break; |
| 1280 | case 'g': |
| 1281 | set_desired_inferior (1); |
| 1282 | registers_to_string (own_buf); |
| 1283 | break; |
| 1284 | case 'G': |
| 1285 | set_desired_inferior (1); |
| 1286 | registers_from_string (&own_buf[1]); |
| 1287 | write_ok (own_buf); |
| 1288 | break; |
| 1289 | case 'P': { |
| 1290 | int regno; |
| 1291 | char *regbytes; |
| 1292 | Bool mod; |
| 1293 | ThreadState *tst; |
| 1294 | regno = strtol(&own_buf[1], NULL, 16); |
| 1295 | regbytes = strchr(&own_buf[0], '=') + 1; |
| 1296 | set_desired_inferior (1); |
| 1297 | tst = (ThreadState *) inferior_target_data (current_inferior); |
| 1298 | /* Only accept changing registers in "runnable state3. |
| 1299 | In fact, it would be ok to change most of the registers |
| 1300 | except a few "sensitive" registers such as the PC, SP, BP. |
| 1301 | We assume we do not need to very specific here, and that we |
| 1302 | can just refuse all of these. */ |
| 1303 | if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) { |
| 1304 | supply_register_from_string (regno, regbytes, &mod); |
| 1305 | write_ok (own_buf); |
| 1306 | } else { |
| 1307 | /* at least from gdb 6.6 onwards, an E. error |
| 1308 | reply is shown to the user. So, we do an error |
| 1309 | msg which both is accepted by gdb as an error msg |
| 1310 | and is readable by the user. */ |
| 1311 | VG_(sprintf) |
| 1312 | (own_buf, |
| 1313 | "E.\n" |
| 1314 | "ERROR changing register %s regno %d\n" |
| 1315 | "gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n" |
| 1316 | "set pc, calling from gdb a function in the debugged process, ...)\n" |
| 1317 | "can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n" |
| 1318 | "Thread status is %s\n", |
| 1319 | find_register_by_number (regno)->name, regno, |
| 1320 | VG_(name_of_ThreadStatus)(tst->status)); |
| 1321 | if (VG_(clo_verbosity) > 1) |
| 1322 | VG_(umsg) ("%s\n", own_buf); |
| 1323 | } |
| 1324 | break; |
| 1325 | } |
| 1326 | case 'm': |
| 1327 | decode_m_packet (&own_buf[1], &mem_addr, &len); |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1328 | if (valgrind_read_memory (mem_addr, mem_buf, len) == 0) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1329 | convert_int_to_ascii (mem_buf, own_buf, len); |
| 1330 | else |
| 1331 | write_enn (own_buf); |
| 1332 | break; |
| 1333 | case 'M': |
| 1334 | decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1335 | if (valgrind_write_memory (mem_addr, mem_buf, len) == 0) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1336 | write_ok (own_buf); |
| 1337 | else |
| 1338 | write_enn (own_buf); |
| 1339 | break; |
| 1340 | case 'X': |
| 1341 | if (decode_X_packet (&own_buf[1], packet_len - 1, |
| 1342 | &mem_addr, &len, mem_buf) < 0 |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1343 | || valgrind_write_memory (mem_addr, mem_buf, len) != 0) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1344 | write_enn (own_buf); |
| 1345 | else |
| 1346 | write_ok (own_buf); |
| 1347 | break; |
| 1348 | case 'C': |
| 1349 | convert_ascii_to_int (own_buf + 1, &sig, 1); |
| 1350 | if (target_signal_to_host_p (sig)) |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1351 | zignal = target_signal_to_host (sig); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1352 | else |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1353 | zignal = 0; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1354 | set_desired_inferior (0); |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1355 | myresume (0, zignal); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1356 | return; // return control to valgrind |
| 1357 | case 'S': |
| 1358 | convert_ascii_to_int (own_buf + 1, &sig, 1); |
| 1359 | if (target_signal_to_host_p (sig)) |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1360 | zignal = target_signal_to_host (sig); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1361 | else |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1362 | zignal = 0; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1363 | set_desired_inferior (0); |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1364 | myresume (1, zignal); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1365 | return; // return control to valgrind |
| 1366 | case 'c': |
| 1367 | set_desired_inferior (0); |
| 1368 | myresume (0, 0); |
| 1369 | return; // return control to valgrind |
| 1370 | case 's': |
| 1371 | set_desired_inferior (0); |
| 1372 | myresume (1, 0); |
| 1373 | return; // return control to valgrind |
| 1374 | case 'Z': { |
| 1375 | char *lenptr; |
| 1376 | char *dataptr; |
| 1377 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); |
| 1378 | int zlen = strtol (lenptr + 1, &dataptr, 16); |
| 1379 | char type = own_buf[1]; |
| 1380 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1381 | if (type < '0' || type > '4') { |
| 1382 | /* Watchpoint command type unrecognized. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1383 | own_buf[0] = '\0'; |
| 1384 | } else { |
| 1385 | int res; |
| 1386 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1387 | res = valgrind_insert_watchpoint (type, addr, zlen); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1388 | if (res == 0) |
| 1389 | write_ok (own_buf); |
| 1390 | else if (res == 1) |
| 1391 | /* Unsupported. */ |
| 1392 | own_buf[0] = '\0'; |
| 1393 | else |
| 1394 | write_enn (own_buf); |
| 1395 | } |
| 1396 | break; |
| 1397 | } |
| 1398 | case 'z': { |
| 1399 | char *lenptr; |
| 1400 | char *dataptr; |
| 1401 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); |
| 1402 | int zlen = strtol (lenptr + 1, &dataptr, 16); |
| 1403 | char type = own_buf[1]; |
| 1404 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1405 | if (type < '0' || type > '4') { |
| 1406 | /* Watchpoint command type unrecognized. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1407 | own_buf[0] = '\0'; |
| 1408 | } else { |
| 1409 | int res; |
| 1410 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1411 | res = valgrind_remove_watchpoint (type, addr, zlen); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1412 | if (res == 0) |
| 1413 | write_ok (own_buf); |
| 1414 | else if (res == 1) |
| 1415 | /* Unsupported. */ |
| 1416 | own_buf[0] = '\0'; |
| 1417 | else |
| 1418 | write_enn (own_buf); |
| 1419 | } |
| 1420 | break; |
| 1421 | } |
| 1422 | case 'k': |
| 1423 | kill_request("Gdb request to kill this process\n"); |
| 1424 | break; |
| 1425 | case 'T': { |
| 1426 | unsigned long gdb_id, thread_id; |
| 1427 | |
| 1428 | gdb_id = strtoul (&own_buf[1], NULL, 16); |
| 1429 | thread_id = gdb_id_to_thread_id (gdb_id); |
| 1430 | if (thread_id == 0) { |
| 1431 | write_enn (own_buf); |
| 1432 | break; |
| 1433 | } |
| 1434 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1435 | if (valgrind_thread_alive (thread_id)) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1436 | write_ok (own_buf); |
| 1437 | else |
| 1438 | write_enn (own_buf); |
| 1439 | break; |
| 1440 | } |
| 1441 | case 'R': |
| 1442 | /* Restarting the inferior is only supported in the |
| 1443 | extended protocol. |
| 1444 | => It is a request we don't understand. Respond with an |
| 1445 | empty packet so that gdb knows that we don't support this |
| 1446 | request. */ |
| 1447 | own_buf[0] = '\0'; |
| 1448 | break; |
| 1449 | case 'v': |
| 1450 | /* Extended (long) request. */ |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1451 | handle_v_requests (own_buf, &status, &zignal); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1452 | break; |
| 1453 | default: |
| 1454 | /* It is a request we don't understand. Respond with an |
| 1455 | empty packet so that gdb knows that we don't support this |
| 1456 | request. */ |
| 1457 | own_buf[0] = '\0'; |
| 1458 | break; |
| 1459 | } |
| 1460 | |
| 1461 | if (new_packet_len != -1) |
| 1462 | putpkt_binary (own_buf, new_packet_len); |
| 1463 | else |
| 1464 | putpkt (own_buf); |
| 1465 | |
| 1466 | if (status == 'W') |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1467 | VG_(umsg) ("\nChild exited with status %d\n", zignal); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1468 | if (status == 'X') |
sewardj | 47ffedb | 2011-10-24 07:36:57 +0000 | [diff] [blame] | 1469 | VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n", |
florian | 654ea86 | 2015-08-06 12:11:33 +0000 | [diff] [blame] | 1470 | (UInt)target_signal_to_host (zignal), |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1471 | target_signal_to_name (zignal)); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1472 | if (status == 'W' || status == 'X') { |
| 1473 | VG_(umsg) ("Process exiting\n"); |
| 1474 | VG_(exit) (0); |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | /* We come here when getpkt fails => close the connection, |
| 1479 | and re-open. Then return control to valgrind. |
| 1480 | We return the control to valgrind as we assume that |
| 1481 | the connection was closed due to vgdb having finished |
| 1482 | to execute a command. */ |
| 1483 | if (VG_(clo_verbosity) > 1) |
| 1484 | VG_(umsg) ("Remote side has terminated connection. " |
| 1485 | "GDBserver will reopen the connection.\n"); |
| 1486 | remote_finish (reset_after_error); |
| 1487 | remote_open (VG_(clo_vgdb_prefix)); |
| 1488 | myresume (0, 0); |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1489 | resume_reply_packet_needed = False; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1490 | return; |
| 1491 | } |