| 1 | /* Agent expression code for remote server. |
| 2 | Copyright (C) 2009-2013 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include "server.h" |
| 20 | #include "ax.h" |
| 21 | #include "format.h" |
| 22 | |
| 23 | static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2); |
| 24 | |
| 25 | #ifdef IN_PROCESS_AGENT |
| 26 | int debug_agent = 0; |
| 27 | #endif |
| 28 | |
| 29 | static void |
| 30 | ax_vdebug (const char *fmt, ...) |
| 31 | { |
| 32 | char buf[1024]; |
| 33 | va_list ap; |
| 34 | |
| 35 | va_start (ap, fmt); |
| 36 | vsprintf (buf, fmt, ap); |
| 37 | fprintf (stderr, PROG "/ax: %s\n", buf); |
| 38 | va_end (ap); |
| 39 | } |
| 40 | |
| 41 | #define ax_debug_1(level, fmt, args...) \ |
| 42 | do { \ |
| 43 | if (level <= debug_threads) \ |
| 44 | ax_vdebug ((fmt), ##args); \ |
| 45 | } while (0) |
| 46 | |
| 47 | #define ax_debug(FMT, args...) \ |
| 48 | ax_debug_1 (1, FMT, ##args) |
| 49 | |
| 50 | /* This enum must exactly match what is documented in |
| 51 | gdb/doc/agentexpr.texi, including all the numerical values. */ |
| 52 | |
| 53 | enum gdb_agent_op |
| 54 | { |
| 55 | #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \ |
| 56 | gdb_agent_op_ ## NAME = VALUE, |
| 57 | #include "ax.def" |
| 58 | #undef DEFOP |
| 59 | gdb_agent_op_last |
| 60 | }; |
| 61 | |
| 62 | static const char *gdb_agent_op_names [gdb_agent_op_last] = |
| 63 | { |
| 64 | "?undef?" |
| 65 | #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME |
| 66 | #include "ax.def" |
| 67 | #undef DEFOP |
| 68 | }; |
| 69 | |
| 70 | static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] = |
| 71 | { |
| 72 | 0 |
| 73 | #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE |
| 74 | #include "ax.def" |
| 75 | #undef DEFOP |
| 76 | }; |
| 77 | |
| 78 | /* A wrapper for gdb_agent_op_names that does some bounds-checking. */ |
| 79 | |
| 80 | static const char * |
| 81 | gdb_agent_op_name (int op) |
| 82 | { |
| 83 | if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL) |
| 84 | return "?undef?"; |
| 85 | return gdb_agent_op_names[op]; |
| 86 | } |
| 87 | |
| 88 | #ifndef IN_PROCESS_AGENT |
| 89 | |
| 90 | /* The packet form of an agent expression consists of an 'X', number |
| 91 | of bytes in expression, a comma, and then the bytes. */ |
| 92 | |
| 93 | struct agent_expr * |
| 94 | gdb_parse_agent_expr (char **actparm) |
| 95 | { |
| 96 | char *act = *actparm; |
| 97 | ULONGEST xlen; |
| 98 | struct agent_expr *aexpr; |
| 99 | |
| 100 | ++act; /* skip the X */ |
| 101 | act = unpack_varlen_hex (act, &xlen); |
| 102 | ++act; /* skip a comma */ |
| 103 | aexpr = xmalloc (sizeof (struct agent_expr)); |
| 104 | aexpr->length = xlen; |
| 105 | aexpr->bytes = xmalloc (xlen); |
| 106 | convert_ascii_to_int (act, aexpr->bytes, xlen); |
| 107 | *actparm = act + (xlen * 2); |
| 108 | return aexpr; |
| 109 | } |
| 110 | |
| 111 | /* Convert the bytes of an agent expression back into hex digits, so |
| 112 | they can be printed or uploaded. This allocates the buffer, |
| 113 | callers should free when they are done with it. */ |
| 114 | |
| 115 | char * |
| 116 | gdb_unparse_agent_expr (struct agent_expr *aexpr) |
| 117 | { |
| 118 | char *rslt; |
| 119 | |
| 120 | rslt = xmalloc (2 * aexpr->length + 1); |
| 121 | convert_int_to_ascii (aexpr->bytes, rslt, aexpr->length); |
| 122 | return rslt; |
| 123 | } |
| 124 | |
| 125 | /* Bytecode compilation. */ |
| 126 | |
| 127 | CORE_ADDR current_insn_ptr; |
| 128 | |
| 129 | int emit_error; |
| 130 | |
| 131 | struct bytecode_address |
| 132 | { |
| 133 | int pc; |
| 134 | CORE_ADDR address; |
| 135 | int goto_pc; |
| 136 | /* Offset and size of field to be modified in the goto block. */ |
| 137 | int from_offset, from_size; |
| 138 | struct bytecode_address *next; |
| 139 | } *bytecode_address_table; |
| 140 | |
| 141 | void |
| 142 | emit_prologue (void) |
| 143 | { |
| 144 | target_emit_ops ()->emit_prologue (); |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | emit_epilogue (void) |
| 149 | { |
| 150 | target_emit_ops ()->emit_epilogue (); |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | emit_add (void) |
| 155 | { |
| 156 | target_emit_ops ()->emit_add (); |
| 157 | } |
| 158 | |
| 159 | static void |
| 160 | emit_sub (void) |
| 161 | { |
| 162 | target_emit_ops ()->emit_sub (); |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | emit_mul (void) |
| 167 | { |
| 168 | target_emit_ops ()->emit_mul (); |
| 169 | } |
| 170 | |
| 171 | static void |
| 172 | emit_lsh (void) |
| 173 | { |
| 174 | target_emit_ops ()->emit_lsh (); |
| 175 | } |
| 176 | |
| 177 | static void |
| 178 | emit_rsh_signed (void) |
| 179 | { |
| 180 | target_emit_ops ()->emit_rsh_signed (); |
| 181 | } |
| 182 | |
| 183 | static void |
| 184 | emit_rsh_unsigned (void) |
| 185 | { |
| 186 | target_emit_ops ()->emit_rsh_unsigned (); |
| 187 | } |
| 188 | |
| 189 | static void |
| 190 | emit_ext (int arg) |
| 191 | { |
| 192 | target_emit_ops ()->emit_ext (arg); |
| 193 | } |
| 194 | |
| 195 | static void |
| 196 | emit_log_not (void) |
| 197 | { |
| 198 | target_emit_ops ()->emit_log_not (); |
| 199 | } |
| 200 | |
| 201 | static void |
| 202 | emit_bit_and (void) |
| 203 | { |
| 204 | target_emit_ops ()->emit_bit_and (); |
| 205 | } |
| 206 | |
| 207 | static void |
| 208 | emit_bit_or (void) |
| 209 | { |
| 210 | target_emit_ops ()->emit_bit_or (); |
| 211 | } |
| 212 | |
| 213 | static void |
| 214 | emit_bit_xor (void) |
| 215 | { |
| 216 | target_emit_ops ()->emit_bit_xor (); |
| 217 | } |
| 218 | |
| 219 | static void |
| 220 | emit_bit_not (void) |
| 221 | { |
| 222 | target_emit_ops ()->emit_bit_not (); |
| 223 | } |
| 224 | |
| 225 | static void |
| 226 | emit_equal (void) |
| 227 | { |
| 228 | target_emit_ops ()->emit_equal (); |
| 229 | } |
| 230 | |
| 231 | static void |
| 232 | emit_less_signed (void) |
| 233 | { |
| 234 | target_emit_ops ()->emit_less_signed (); |
| 235 | } |
| 236 | |
| 237 | static void |
| 238 | emit_less_unsigned (void) |
| 239 | { |
| 240 | target_emit_ops ()->emit_less_unsigned (); |
| 241 | } |
| 242 | |
| 243 | static void |
| 244 | emit_ref (int size) |
| 245 | { |
| 246 | target_emit_ops ()->emit_ref (size); |
| 247 | } |
| 248 | |
| 249 | static void |
| 250 | emit_if_goto (int *offset_p, int *size_p) |
| 251 | { |
| 252 | target_emit_ops ()->emit_if_goto (offset_p, size_p); |
| 253 | } |
| 254 | |
| 255 | static void |
| 256 | emit_goto (int *offset_p, int *size_p) |
| 257 | { |
| 258 | target_emit_ops ()->emit_goto (offset_p, size_p); |
| 259 | } |
| 260 | |
| 261 | static void |
| 262 | write_goto_address (CORE_ADDR from, CORE_ADDR to, int size) |
| 263 | { |
| 264 | target_emit_ops ()->write_goto_address (from, to, size); |
| 265 | } |
| 266 | |
| 267 | static void |
| 268 | emit_const (LONGEST num) |
| 269 | { |
| 270 | target_emit_ops ()->emit_const (num); |
| 271 | } |
| 272 | |
| 273 | static void |
| 274 | emit_reg (int reg) |
| 275 | { |
| 276 | target_emit_ops ()->emit_reg (reg); |
| 277 | } |
| 278 | |
| 279 | static void |
| 280 | emit_pop (void) |
| 281 | { |
| 282 | target_emit_ops ()->emit_pop (); |
| 283 | } |
| 284 | |
| 285 | static void |
| 286 | emit_stack_flush (void) |
| 287 | { |
| 288 | target_emit_ops ()->emit_stack_flush (); |
| 289 | } |
| 290 | |
| 291 | static void |
| 292 | emit_zero_ext (int arg) |
| 293 | { |
| 294 | target_emit_ops ()->emit_zero_ext (arg); |
| 295 | } |
| 296 | |
| 297 | static void |
| 298 | emit_swap (void) |
| 299 | { |
| 300 | target_emit_ops ()->emit_swap (); |
| 301 | } |
| 302 | |
| 303 | static void |
| 304 | emit_stack_adjust (int n) |
| 305 | { |
| 306 | target_emit_ops ()->emit_stack_adjust (n); |
| 307 | } |
| 308 | |
| 309 | /* FN's prototype is `LONGEST(*fn)(int)'. */ |
| 310 | |
| 311 | static void |
| 312 | emit_int_call_1 (CORE_ADDR fn, int arg1) |
| 313 | { |
| 314 | target_emit_ops ()->emit_int_call_1 (fn, arg1); |
| 315 | } |
| 316 | |
| 317 | /* FN's prototype is `void(*fn)(int,LONGEST)'. */ |
| 318 | |
| 319 | static void |
| 320 | emit_void_call_2 (CORE_ADDR fn, int arg1) |
| 321 | { |
| 322 | target_emit_ops ()->emit_void_call_2 (fn, arg1); |
| 323 | } |
| 324 | |
| 325 | static void |
| 326 | emit_eq_goto (int *offset_p, int *size_p) |
| 327 | { |
| 328 | target_emit_ops ()->emit_eq_goto (offset_p, size_p); |
| 329 | } |
| 330 | |
| 331 | static void |
| 332 | emit_ne_goto (int *offset_p, int *size_p) |
| 333 | { |
| 334 | target_emit_ops ()->emit_ne_goto (offset_p, size_p); |
| 335 | } |
| 336 | |
| 337 | static void |
| 338 | emit_lt_goto (int *offset_p, int *size_p) |
| 339 | { |
| 340 | target_emit_ops ()->emit_lt_goto (offset_p, size_p); |
| 341 | } |
| 342 | |
| 343 | static void |
| 344 | emit_ge_goto (int *offset_p, int *size_p) |
| 345 | { |
| 346 | target_emit_ops ()->emit_ge_goto (offset_p, size_p); |
| 347 | } |
| 348 | |
| 349 | static void |
| 350 | emit_gt_goto (int *offset_p, int *size_p) |
| 351 | { |
| 352 | target_emit_ops ()->emit_gt_goto (offset_p, size_p); |
| 353 | } |
| 354 | |
| 355 | static void |
| 356 | emit_le_goto (int *offset_p, int *size_p) |
| 357 | { |
| 358 | target_emit_ops ()->emit_le_goto (offset_p, size_p); |
| 359 | } |
| 360 | |
| 361 | /* Scan an agent expression for any evidence that the given PC is the |
| 362 | target of a jump bytecode in the expression. */ |
| 363 | |
| 364 | int |
| 365 | is_goto_target (struct agent_expr *aexpr, int pc) |
| 366 | { |
| 367 | int i; |
| 368 | unsigned char op; |
| 369 | |
| 370 | for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op]) |
| 371 | { |
| 372 | op = aexpr->bytes[i]; |
| 373 | |
| 374 | if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto) |
| 375 | { |
| 376 | int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2]; |
| 377 | if (target == pc) |
| 378 | return 1; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | /* Given an agent expression, turn it into native code. */ |
| 386 | |
| 387 | enum eval_result_type |
| 388 | compile_bytecodes (struct agent_expr *aexpr) |
| 389 | { |
| 390 | int pc = 0; |
| 391 | int done = 0; |
| 392 | unsigned char op, next_op; |
| 393 | int arg; |
| 394 | /* This is only used to build 64-bit value for constants. */ |
| 395 | ULONGEST top; |
| 396 | struct bytecode_address *aentry, *aentry2; |
| 397 | |
| 398 | #define UNHANDLED \ |
| 399 | do \ |
| 400 | { \ |
| 401 | ax_debug ("Cannot compile op 0x%x\n", op); \ |
| 402 | return expr_eval_unhandled_opcode; \ |
| 403 | } while (0) |
| 404 | |
| 405 | if (aexpr->length == 0) |
| 406 | { |
| 407 | ax_debug ("empty agent expression\n"); |
| 408 | return expr_eval_empty_expression; |
| 409 | } |
| 410 | |
| 411 | bytecode_address_table = NULL; |
| 412 | |
| 413 | while (!done) |
| 414 | { |
| 415 | op = aexpr->bytes[pc]; |
| 416 | |
| 417 | ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc); |
| 418 | |
| 419 | /* Record the compiled-code address of the bytecode, for use by |
| 420 | jump instructions. */ |
| 421 | aentry = xmalloc (sizeof (struct bytecode_address)); |
| 422 | aentry->pc = pc; |
| 423 | aentry->address = current_insn_ptr; |
| 424 | aentry->goto_pc = -1; |
| 425 | aentry->from_offset = aentry->from_size = 0; |
| 426 | aentry->next = bytecode_address_table; |
| 427 | bytecode_address_table = aentry; |
| 428 | |
| 429 | ++pc; |
| 430 | |
| 431 | emit_error = 0; |
| 432 | |
| 433 | switch (op) |
| 434 | { |
| 435 | case gdb_agent_op_add: |
| 436 | emit_add (); |
| 437 | break; |
| 438 | |
| 439 | case gdb_agent_op_sub: |
| 440 | emit_sub (); |
| 441 | break; |
| 442 | |
| 443 | case gdb_agent_op_mul: |
| 444 | emit_mul (); |
| 445 | break; |
| 446 | |
| 447 | case gdb_agent_op_div_signed: |
| 448 | UNHANDLED; |
| 449 | break; |
| 450 | |
| 451 | case gdb_agent_op_div_unsigned: |
| 452 | UNHANDLED; |
| 453 | break; |
| 454 | |
| 455 | case gdb_agent_op_rem_signed: |
| 456 | UNHANDLED; |
| 457 | break; |
| 458 | |
| 459 | case gdb_agent_op_rem_unsigned: |
| 460 | UNHANDLED; |
| 461 | break; |
| 462 | |
| 463 | case gdb_agent_op_lsh: |
| 464 | emit_lsh (); |
| 465 | break; |
| 466 | |
| 467 | case gdb_agent_op_rsh_signed: |
| 468 | emit_rsh_signed (); |
| 469 | break; |
| 470 | |
| 471 | case gdb_agent_op_rsh_unsigned: |
| 472 | emit_rsh_unsigned (); |
| 473 | break; |
| 474 | |
| 475 | case gdb_agent_op_trace: |
| 476 | UNHANDLED; |
| 477 | break; |
| 478 | |
| 479 | case gdb_agent_op_trace_quick: |
| 480 | UNHANDLED; |
| 481 | break; |
| 482 | |
| 483 | case gdb_agent_op_log_not: |
| 484 | emit_log_not (); |
| 485 | break; |
| 486 | |
| 487 | case gdb_agent_op_bit_and: |
| 488 | emit_bit_and (); |
| 489 | break; |
| 490 | |
| 491 | case gdb_agent_op_bit_or: |
| 492 | emit_bit_or (); |
| 493 | break; |
| 494 | |
| 495 | case gdb_agent_op_bit_xor: |
| 496 | emit_bit_xor (); |
| 497 | break; |
| 498 | |
| 499 | case gdb_agent_op_bit_not: |
| 500 | emit_bit_not (); |
| 501 | break; |
| 502 | |
| 503 | case gdb_agent_op_equal: |
| 504 | next_op = aexpr->bytes[pc]; |
| 505 | if (next_op == gdb_agent_op_if_goto |
| 506 | && !is_goto_target (aexpr, pc) |
| 507 | && target_emit_ops ()->emit_eq_goto) |
| 508 | { |
| 509 | ax_debug ("Combining equal & if_goto"); |
| 510 | pc += 1; |
| 511 | aentry->pc = pc; |
| 512 | arg = aexpr->bytes[pc++]; |
| 513 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 514 | aentry->goto_pc = arg; |
| 515 | emit_eq_goto (&(aentry->from_offset), &(aentry->from_size)); |
| 516 | } |
| 517 | else if (next_op == gdb_agent_op_log_not |
| 518 | && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto) |
| 519 | && !is_goto_target (aexpr, pc + 1) |
| 520 | && target_emit_ops ()->emit_ne_goto) |
| 521 | { |
| 522 | ax_debug ("Combining equal & log_not & if_goto"); |
| 523 | pc += 2; |
| 524 | aentry->pc = pc; |
| 525 | arg = aexpr->bytes[pc++]; |
| 526 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 527 | aentry->goto_pc = arg; |
| 528 | emit_ne_goto (&(aentry->from_offset), &(aentry->from_size)); |
| 529 | } |
| 530 | else |
| 531 | emit_equal (); |
| 532 | break; |
| 533 | |
| 534 | case gdb_agent_op_less_signed: |
| 535 | next_op = aexpr->bytes[pc]; |
| 536 | if (next_op == gdb_agent_op_if_goto |
| 537 | && !is_goto_target (aexpr, pc)) |
| 538 | { |
| 539 | ax_debug ("Combining less_signed & if_goto"); |
| 540 | pc += 1; |
| 541 | aentry->pc = pc; |
| 542 | arg = aexpr->bytes[pc++]; |
| 543 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 544 | aentry->goto_pc = arg; |
| 545 | emit_lt_goto (&(aentry->from_offset), &(aentry->from_size)); |
| 546 | } |
| 547 | else if (next_op == gdb_agent_op_log_not |
| 548 | && !is_goto_target (aexpr, pc) |
| 549 | && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto) |
| 550 | && !is_goto_target (aexpr, pc + 1)) |
| 551 | { |
| 552 | ax_debug ("Combining less_signed & log_not & if_goto"); |
| 553 | pc += 2; |
| 554 | aentry->pc = pc; |
| 555 | arg = aexpr->bytes[pc++]; |
| 556 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 557 | aentry->goto_pc = arg; |
| 558 | emit_ge_goto (&(aentry->from_offset), &(aentry->from_size)); |
| 559 | } |
| 560 | else |
| 561 | emit_less_signed (); |
| 562 | break; |
| 563 | |
| 564 | case gdb_agent_op_less_unsigned: |
| 565 | emit_less_unsigned (); |
| 566 | break; |
| 567 | |
| 568 | case gdb_agent_op_ext: |
| 569 | arg = aexpr->bytes[pc++]; |
| 570 | if (arg < (sizeof (LONGEST) * 8)) |
| 571 | emit_ext (arg); |
| 572 | break; |
| 573 | |
| 574 | case gdb_agent_op_ref8: |
| 575 | emit_ref (1); |
| 576 | break; |
| 577 | |
| 578 | case gdb_agent_op_ref16: |
| 579 | emit_ref (2); |
| 580 | break; |
| 581 | |
| 582 | case gdb_agent_op_ref32: |
| 583 | emit_ref (4); |
| 584 | break; |
| 585 | |
| 586 | case gdb_agent_op_ref64: |
| 587 | emit_ref (8); |
| 588 | break; |
| 589 | |
| 590 | case gdb_agent_op_if_goto: |
| 591 | arg = aexpr->bytes[pc++]; |
| 592 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 593 | aentry->goto_pc = arg; |
| 594 | emit_if_goto (&(aentry->from_offset), &(aentry->from_size)); |
| 595 | break; |
| 596 | |
| 597 | case gdb_agent_op_goto: |
| 598 | arg = aexpr->bytes[pc++]; |
| 599 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 600 | aentry->goto_pc = arg; |
| 601 | emit_goto (&(aentry->from_offset), &(aentry->from_size)); |
| 602 | break; |
| 603 | |
| 604 | case gdb_agent_op_const8: |
| 605 | emit_stack_flush (); |
| 606 | top = aexpr->bytes[pc++]; |
| 607 | emit_const (top); |
| 608 | break; |
| 609 | |
| 610 | case gdb_agent_op_const16: |
| 611 | emit_stack_flush (); |
| 612 | top = aexpr->bytes[pc++]; |
| 613 | top = (top << 8) + aexpr->bytes[pc++]; |
| 614 | emit_const (top); |
| 615 | break; |
| 616 | |
| 617 | case gdb_agent_op_const32: |
| 618 | emit_stack_flush (); |
| 619 | top = aexpr->bytes[pc++]; |
| 620 | top = (top << 8) + aexpr->bytes[pc++]; |
| 621 | top = (top << 8) + aexpr->bytes[pc++]; |
| 622 | top = (top << 8) + aexpr->bytes[pc++]; |
| 623 | emit_const (top); |
| 624 | break; |
| 625 | |
| 626 | case gdb_agent_op_const64: |
| 627 | emit_stack_flush (); |
| 628 | top = aexpr->bytes[pc++]; |
| 629 | top = (top << 8) + aexpr->bytes[pc++]; |
| 630 | top = (top << 8) + aexpr->bytes[pc++]; |
| 631 | top = (top << 8) + aexpr->bytes[pc++]; |
| 632 | top = (top << 8) + aexpr->bytes[pc++]; |
| 633 | top = (top << 8) + aexpr->bytes[pc++]; |
| 634 | top = (top << 8) + aexpr->bytes[pc++]; |
| 635 | top = (top << 8) + aexpr->bytes[pc++]; |
| 636 | emit_const (top); |
| 637 | break; |
| 638 | |
| 639 | case gdb_agent_op_reg: |
| 640 | emit_stack_flush (); |
| 641 | arg = aexpr->bytes[pc++]; |
| 642 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 643 | emit_reg (arg); |
| 644 | break; |
| 645 | |
| 646 | case gdb_agent_op_end: |
| 647 | ax_debug ("At end of expression\n"); |
| 648 | |
| 649 | /* Assume there is one stack element left, and that it is |
| 650 | cached in "top" where emit_epilogue can get to it. */ |
| 651 | emit_stack_adjust (1); |
| 652 | |
| 653 | done = 1; |
| 654 | break; |
| 655 | |
| 656 | case gdb_agent_op_dup: |
| 657 | /* In our design, dup is equivalent to stack flushing. */ |
| 658 | emit_stack_flush (); |
| 659 | break; |
| 660 | |
| 661 | case gdb_agent_op_pop: |
| 662 | emit_pop (); |
| 663 | break; |
| 664 | |
| 665 | case gdb_agent_op_zero_ext: |
| 666 | arg = aexpr->bytes[pc++]; |
| 667 | if (arg < (sizeof (LONGEST) * 8)) |
| 668 | emit_zero_ext (arg); |
| 669 | break; |
| 670 | |
| 671 | case gdb_agent_op_swap: |
| 672 | next_op = aexpr->bytes[pc]; |
| 673 | /* Detect greater-than comparison sequences. */ |
| 674 | if (next_op == gdb_agent_op_less_signed |
| 675 | && !is_goto_target (aexpr, pc) |
| 676 | && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto) |
| 677 | && !is_goto_target (aexpr, pc + 1)) |
| 678 | { |
| 679 | ax_debug ("Combining swap & less_signed & if_goto"); |
| 680 | pc += 2; |
| 681 | aentry->pc = pc; |
| 682 | arg = aexpr->bytes[pc++]; |
| 683 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 684 | aentry->goto_pc = arg; |
| 685 | emit_gt_goto (&(aentry->from_offset), &(aentry->from_size)); |
| 686 | } |
| 687 | else if (next_op == gdb_agent_op_less_signed |
| 688 | && !is_goto_target (aexpr, pc) |
| 689 | && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not) |
| 690 | && !is_goto_target (aexpr, pc + 1) |
| 691 | && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto) |
| 692 | && !is_goto_target (aexpr, pc + 2)) |
| 693 | { |
| 694 | ax_debug ("Combining swap & less_signed & log_not & if_goto"); |
| 695 | pc += 3; |
| 696 | aentry->pc = pc; |
| 697 | arg = aexpr->bytes[pc++]; |
| 698 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 699 | aentry->goto_pc = arg; |
| 700 | emit_le_goto (&(aentry->from_offset), &(aentry->from_size)); |
| 701 | } |
| 702 | else |
| 703 | emit_swap (); |
| 704 | break; |
| 705 | |
| 706 | case gdb_agent_op_getv: |
| 707 | emit_stack_flush (); |
| 708 | arg = aexpr->bytes[pc++]; |
| 709 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 710 | emit_int_call_1 (get_get_tsv_func_addr (), |
| 711 | arg); |
| 712 | break; |
| 713 | |
| 714 | case gdb_agent_op_setv: |
| 715 | arg = aexpr->bytes[pc++]; |
| 716 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 717 | emit_void_call_2 (get_set_tsv_func_addr (), |
| 718 | arg); |
| 719 | break; |
| 720 | |
| 721 | case gdb_agent_op_tracev: |
| 722 | UNHANDLED; |
| 723 | break; |
| 724 | |
| 725 | /* GDB never (currently) generates any of these ops. */ |
| 726 | case gdb_agent_op_float: |
| 727 | case gdb_agent_op_ref_float: |
| 728 | case gdb_agent_op_ref_double: |
| 729 | case gdb_agent_op_ref_long_double: |
| 730 | case gdb_agent_op_l_to_d: |
| 731 | case gdb_agent_op_d_to_l: |
| 732 | case gdb_agent_op_trace16: |
| 733 | UNHANDLED; |
| 734 | break; |
| 735 | |
| 736 | default: |
| 737 | ax_debug ("Agent expression op 0x%x not recognized\n", op); |
| 738 | /* Don't struggle on, things will just get worse. */ |
| 739 | return expr_eval_unrecognized_opcode; |
| 740 | } |
| 741 | |
| 742 | /* This catches errors that occur in target-specific code |
| 743 | emission. */ |
| 744 | if (emit_error) |
| 745 | { |
| 746 | ax_debug ("Error %d while emitting code for %s\n", |
| 747 | emit_error, gdb_agent_op_name (op)); |
| 748 | return expr_eval_unhandled_opcode; |
| 749 | } |
| 750 | |
| 751 | ax_debug ("Op %s compiled\n", gdb_agent_op_name (op)); |
| 752 | } |
| 753 | |
| 754 | /* Now fill in real addresses as goto destinations. */ |
| 755 | for (aentry = bytecode_address_table; aentry; aentry = aentry->next) |
| 756 | { |
| 757 | int written = 0; |
| 758 | |
| 759 | if (aentry->goto_pc < 0) |
| 760 | continue; |
| 761 | |
| 762 | /* Find the location that we are going to, and call back into |
| 763 | target-specific code to write the actual address or |
| 764 | displacement. */ |
| 765 | for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next) |
| 766 | { |
| 767 | if (aentry2->pc == aentry->goto_pc) |
| 768 | { |
| 769 | ax_debug ("Want to jump from %s to %s\n", |
| 770 | paddress (aentry->address), |
| 771 | paddress (aentry2->address)); |
| 772 | write_goto_address (aentry->address + aentry->from_offset, |
| 773 | aentry2->address, aentry->from_size); |
| 774 | written = 1; |
| 775 | break; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /* Error out if we didn't find a destination. */ |
| 780 | if (!written) |
| 781 | { |
| 782 | ax_debug ("Destination of goto %d not found\n", |
| 783 | aentry->goto_pc); |
| 784 | return expr_eval_invalid_goto; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | return expr_eval_no_error; |
| 789 | } |
| 790 | |
| 791 | #endif |
| 792 | |
| 793 | /* Make printf-type calls using arguments supplied from the host. We |
| 794 | need to parse the format string ourselves, and call the formatting |
| 795 | function with one argument at a time, partly because there is no |
| 796 | safe portable way to construct a varargs call, and partly to serve |
| 797 | as a security barrier against bad format strings that might get |
| 798 | in. */ |
| 799 | |
| 800 | static void |
| 801 | ax_printf (CORE_ADDR fn, CORE_ADDR chan, char *format, |
| 802 | int nargs, ULONGEST *args) |
| 803 | { |
| 804 | char *f = format; |
| 805 | struct format_piece *fpieces; |
| 806 | int i, fp; |
| 807 | char *current_substring; |
| 808 | int nargs_wanted; |
| 809 | |
| 810 | ax_debug ("Printf of \"%s\" with %d args", format, nargs); |
| 811 | |
| 812 | fpieces = parse_format_string (&f); |
| 813 | |
| 814 | nargs_wanted = 0; |
| 815 | for (fp = 0; fpieces[fp].string != NULL; fp++) |
| 816 | if (fpieces[fp].argclass != literal_piece) |
| 817 | ++nargs_wanted; |
| 818 | |
| 819 | if (nargs != nargs_wanted) |
| 820 | error (_("Wrong number of arguments for specified format-string")); |
| 821 | |
| 822 | i = 0; |
| 823 | for (fp = 0; fpieces[fp].string != NULL; fp++) |
| 824 | { |
| 825 | current_substring = fpieces[fp].string; |
| 826 | ax_debug ("current substring is '%s', class is %d", |
| 827 | current_substring, fpieces[fp].argclass); |
| 828 | switch (fpieces[fp].argclass) |
| 829 | { |
| 830 | case string_arg: |
| 831 | { |
| 832 | gdb_byte *str; |
| 833 | CORE_ADDR tem; |
| 834 | int j; |
| 835 | |
| 836 | tem = args[i]; |
| 837 | |
| 838 | /* This is a %s argument. Find the length of the string. */ |
| 839 | for (j = 0;; j++) |
| 840 | { |
| 841 | gdb_byte c; |
| 842 | |
| 843 | read_inferior_memory (tem + j, &c, 1); |
| 844 | if (c == 0) |
| 845 | break; |
| 846 | } |
| 847 | |
| 848 | /* Copy the string contents into a string inside GDB. */ |
| 849 | str = (gdb_byte *) alloca (j + 1); |
| 850 | if (j != 0) |
| 851 | read_inferior_memory (tem, str, j); |
| 852 | str[j] = 0; |
| 853 | |
| 854 | printf (current_substring, (char *) str); |
| 855 | } |
| 856 | break; |
| 857 | |
| 858 | case long_long_arg: |
| 859 | #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG) |
| 860 | { |
| 861 | long long val = args[i]; |
| 862 | |
| 863 | printf (current_substring, val); |
| 864 | break; |
| 865 | } |
| 866 | #else |
| 867 | error (_("long long not supported in agent printf")); |
| 868 | #endif |
| 869 | case int_arg: |
| 870 | { |
| 871 | int val = args[i]; |
| 872 | |
| 873 | printf (current_substring, val); |
| 874 | break; |
| 875 | } |
| 876 | |
| 877 | case long_arg: |
| 878 | { |
| 879 | long val = args[i]; |
| 880 | |
| 881 | printf (current_substring, val); |
| 882 | break; |
| 883 | } |
| 884 | |
| 885 | case literal_piece: |
| 886 | /* Print a portion of the format string that has no |
| 887 | directives. Note that this will not include any |
| 888 | ordinary %-specs, but it might include "%%". That is |
| 889 | why we use printf_filtered and not puts_filtered here. |
| 890 | Also, we pass a dummy argument because some platforms |
| 891 | have modified GCC to include -Wformat-security by |
| 892 | default, which will warn here if there is no |
| 893 | argument. */ |
| 894 | printf (current_substring, 0); |
| 895 | break; |
| 896 | |
| 897 | default: |
| 898 | error (_("Format directive in '%s' not supported in agent printf"), |
| 899 | current_substring); |
| 900 | } |
| 901 | |
| 902 | /* Maybe advance to the next argument. */ |
| 903 | if (fpieces[fp].argclass != literal_piece) |
| 904 | ++i; |
| 905 | } |
| 906 | |
| 907 | free_format_pieces (fpieces); |
| 908 | } |
| 909 | |
| 910 | /* The agent expression evaluator, as specified by the GDB docs. It |
| 911 | returns 0 if everything went OK, and a nonzero error code |
| 912 | otherwise. */ |
| 913 | |
| 914 | enum eval_result_type |
| 915 | gdb_eval_agent_expr (struct eval_agent_expr_context *ctx, |
| 916 | struct agent_expr *aexpr, |
| 917 | ULONGEST *rslt) |
| 918 | { |
| 919 | int pc = 0; |
| 920 | #define STACK_MAX 100 |
| 921 | ULONGEST stack[STACK_MAX], top; |
| 922 | int sp = 0; |
| 923 | unsigned char op; |
| 924 | int arg; |
| 925 | |
| 926 | /* This union is a convenient way to convert representations. For |
| 927 | now, assume a standard architecture where the hardware integer |
| 928 | types have 8, 16, 32, 64 bit types. A more robust solution would |
| 929 | be to import stdint.h from gnulib. */ |
| 930 | union |
| 931 | { |
| 932 | union |
| 933 | { |
| 934 | unsigned char bytes[1]; |
| 935 | unsigned char val; |
| 936 | } u8; |
| 937 | union |
| 938 | { |
| 939 | unsigned char bytes[2]; |
| 940 | unsigned short val; |
| 941 | } u16; |
| 942 | union |
| 943 | { |
| 944 | unsigned char bytes[4]; |
| 945 | unsigned int val; |
| 946 | } u32; |
| 947 | union |
| 948 | { |
| 949 | unsigned char bytes[8]; |
| 950 | ULONGEST val; |
| 951 | } u64; |
| 952 | } cnv; |
| 953 | |
| 954 | if (aexpr->length == 0) |
| 955 | { |
| 956 | ax_debug ("empty agent expression"); |
| 957 | return expr_eval_empty_expression; |
| 958 | } |
| 959 | |
| 960 | /* Cache the stack top in its own variable. Much of the time we can |
| 961 | operate on this variable, rather than dinking with the stack. It |
| 962 | needs to be copied to the stack when sp changes. */ |
| 963 | top = 0; |
| 964 | |
| 965 | while (1) |
| 966 | { |
| 967 | op = aexpr->bytes[pc++]; |
| 968 | |
| 969 | ax_debug ("About to interpret byte 0x%x", op); |
| 970 | |
| 971 | switch (op) |
| 972 | { |
| 973 | case gdb_agent_op_add: |
| 974 | top += stack[--sp]; |
| 975 | break; |
| 976 | |
| 977 | case gdb_agent_op_sub: |
| 978 | top = stack[--sp] - top; |
| 979 | break; |
| 980 | |
| 981 | case gdb_agent_op_mul: |
| 982 | top *= stack[--sp]; |
| 983 | break; |
| 984 | |
| 985 | case gdb_agent_op_div_signed: |
| 986 | if (top == 0) |
| 987 | { |
| 988 | ax_debug ("Attempted to divide by zero"); |
| 989 | return expr_eval_divide_by_zero; |
| 990 | } |
| 991 | top = ((LONGEST) stack[--sp]) / ((LONGEST) top); |
| 992 | break; |
| 993 | |
| 994 | case gdb_agent_op_div_unsigned: |
| 995 | if (top == 0) |
| 996 | { |
| 997 | ax_debug ("Attempted to divide by zero"); |
| 998 | return expr_eval_divide_by_zero; |
| 999 | } |
| 1000 | top = stack[--sp] / top; |
| 1001 | break; |
| 1002 | |
| 1003 | case gdb_agent_op_rem_signed: |
| 1004 | if (top == 0) |
| 1005 | { |
| 1006 | ax_debug ("Attempted to divide by zero"); |
| 1007 | return expr_eval_divide_by_zero; |
| 1008 | } |
| 1009 | top = ((LONGEST) stack[--sp]) % ((LONGEST) top); |
| 1010 | break; |
| 1011 | |
| 1012 | case gdb_agent_op_rem_unsigned: |
| 1013 | if (top == 0) |
| 1014 | { |
| 1015 | ax_debug ("Attempted to divide by zero"); |
| 1016 | return expr_eval_divide_by_zero; |
| 1017 | } |
| 1018 | top = stack[--sp] % top; |
| 1019 | break; |
| 1020 | |
| 1021 | case gdb_agent_op_lsh: |
| 1022 | top = stack[--sp] << top; |
| 1023 | break; |
| 1024 | |
| 1025 | case gdb_agent_op_rsh_signed: |
| 1026 | top = ((LONGEST) stack[--sp]) >> top; |
| 1027 | break; |
| 1028 | |
| 1029 | case gdb_agent_op_rsh_unsigned: |
| 1030 | top = stack[--sp] >> top; |
| 1031 | break; |
| 1032 | |
| 1033 | case gdb_agent_op_trace: |
| 1034 | agent_mem_read (ctx, NULL, (CORE_ADDR) stack[--sp], |
| 1035 | (ULONGEST) top); |
| 1036 | if (--sp >= 0) |
| 1037 | top = stack[sp]; |
| 1038 | break; |
| 1039 | |
| 1040 | case gdb_agent_op_trace_quick: |
| 1041 | arg = aexpr->bytes[pc++]; |
| 1042 | agent_mem_read (ctx, NULL, (CORE_ADDR) top, (ULONGEST) arg); |
| 1043 | break; |
| 1044 | |
| 1045 | case gdb_agent_op_log_not: |
| 1046 | top = !top; |
| 1047 | break; |
| 1048 | |
| 1049 | case gdb_agent_op_bit_and: |
| 1050 | top &= stack[--sp]; |
| 1051 | break; |
| 1052 | |
| 1053 | case gdb_agent_op_bit_or: |
| 1054 | top |= stack[--sp]; |
| 1055 | break; |
| 1056 | |
| 1057 | case gdb_agent_op_bit_xor: |
| 1058 | top ^= stack[--sp]; |
| 1059 | break; |
| 1060 | |
| 1061 | case gdb_agent_op_bit_not: |
| 1062 | top = ~top; |
| 1063 | break; |
| 1064 | |
| 1065 | case gdb_agent_op_equal: |
| 1066 | top = (stack[--sp] == top); |
| 1067 | break; |
| 1068 | |
| 1069 | case gdb_agent_op_less_signed: |
| 1070 | top = (((LONGEST) stack[--sp]) < ((LONGEST) top)); |
| 1071 | break; |
| 1072 | |
| 1073 | case gdb_agent_op_less_unsigned: |
| 1074 | top = (stack[--sp] < top); |
| 1075 | break; |
| 1076 | |
| 1077 | case gdb_agent_op_ext: |
| 1078 | arg = aexpr->bytes[pc++]; |
| 1079 | if (arg < (sizeof (LONGEST) * 8)) |
| 1080 | { |
| 1081 | LONGEST mask = 1 << (arg - 1); |
| 1082 | top &= ((LONGEST) 1 << arg) - 1; |
| 1083 | top = (top ^ mask) - mask; |
| 1084 | } |
| 1085 | break; |
| 1086 | |
| 1087 | case gdb_agent_op_ref8: |
| 1088 | agent_mem_read (ctx, cnv.u8.bytes, (CORE_ADDR) top, 1); |
| 1089 | top = cnv.u8.val; |
| 1090 | break; |
| 1091 | |
| 1092 | case gdb_agent_op_ref16: |
| 1093 | agent_mem_read (ctx, cnv.u16.bytes, (CORE_ADDR) top, 2); |
| 1094 | top = cnv.u16.val; |
| 1095 | break; |
| 1096 | |
| 1097 | case gdb_agent_op_ref32: |
| 1098 | agent_mem_read (ctx, cnv.u32.bytes, (CORE_ADDR) top, 4); |
| 1099 | top = cnv.u32.val; |
| 1100 | break; |
| 1101 | |
| 1102 | case gdb_agent_op_ref64: |
| 1103 | agent_mem_read (ctx, cnv.u64.bytes, (CORE_ADDR) top, 8); |
| 1104 | top = cnv.u64.val; |
| 1105 | break; |
| 1106 | |
| 1107 | case gdb_agent_op_if_goto: |
| 1108 | if (top) |
| 1109 | pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]); |
| 1110 | else |
| 1111 | pc += 2; |
| 1112 | if (--sp >= 0) |
| 1113 | top = stack[sp]; |
| 1114 | break; |
| 1115 | |
| 1116 | case gdb_agent_op_goto: |
| 1117 | pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]); |
| 1118 | break; |
| 1119 | |
| 1120 | case gdb_agent_op_const8: |
| 1121 | /* Flush the cached stack top. */ |
| 1122 | stack[sp++] = top; |
| 1123 | top = aexpr->bytes[pc++]; |
| 1124 | break; |
| 1125 | |
| 1126 | case gdb_agent_op_const16: |
| 1127 | /* Flush the cached stack top. */ |
| 1128 | stack[sp++] = top; |
| 1129 | top = aexpr->bytes[pc++]; |
| 1130 | top = (top << 8) + aexpr->bytes[pc++]; |
| 1131 | break; |
| 1132 | |
| 1133 | case gdb_agent_op_const32: |
| 1134 | /* Flush the cached stack top. */ |
| 1135 | stack[sp++] = top; |
| 1136 | top = aexpr->bytes[pc++]; |
| 1137 | top = (top << 8) + aexpr->bytes[pc++]; |
| 1138 | top = (top << 8) + aexpr->bytes[pc++]; |
| 1139 | top = (top << 8) + aexpr->bytes[pc++]; |
| 1140 | break; |
| 1141 | |
| 1142 | case gdb_agent_op_const64: |
| 1143 | /* Flush the cached stack top. */ |
| 1144 | stack[sp++] = top; |
| 1145 | top = aexpr->bytes[pc++]; |
| 1146 | top = (top << 8) + aexpr->bytes[pc++]; |
| 1147 | top = (top << 8) + aexpr->bytes[pc++]; |
| 1148 | top = (top << 8) + aexpr->bytes[pc++]; |
| 1149 | top = (top << 8) + aexpr->bytes[pc++]; |
| 1150 | top = (top << 8) + aexpr->bytes[pc++]; |
| 1151 | top = (top << 8) + aexpr->bytes[pc++]; |
| 1152 | top = (top << 8) + aexpr->bytes[pc++]; |
| 1153 | break; |
| 1154 | |
| 1155 | case gdb_agent_op_reg: |
| 1156 | /* Flush the cached stack top. */ |
| 1157 | stack[sp++] = top; |
| 1158 | arg = aexpr->bytes[pc++]; |
| 1159 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 1160 | { |
| 1161 | int regnum = arg; |
| 1162 | struct regcache *regcache = ctx->regcache; |
| 1163 | |
| 1164 | switch (register_size (regnum)) |
| 1165 | { |
| 1166 | case 8: |
| 1167 | collect_register (regcache, regnum, cnv.u64.bytes); |
| 1168 | top = cnv.u64.val; |
| 1169 | break; |
| 1170 | case 4: |
| 1171 | collect_register (regcache, regnum, cnv.u32.bytes); |
| 1172 | top = cnv.u32.val; |
| 1173 | break; |
| 1174 | case 2: |
| 1175 | collect_register (regcache, regnum, cnv.u16.bytes); |
| 1176 | top = cnv.u16.val; |
| 1177 | break; |
| 1178 | case 1: |
| 1179 | collect_register (regcache, regnum, cnv.u8.bytes); |
| 1180 | top = cnv.u8.val; |
| 1181 | break; |
| 1182 | default: |
| 1183 | internal_error (__FILE__, __LINE__, |
| 1184 | "unhandled register size"); |
| 1185 | } |
| 1186 | } |
| 1187 | break; |
| 1188 | |
| 1189 | case gdb_agent_op_end: |
| 1190 | ax_debug ("At end of expression, sp=%d, stack top cache=0x%s", |
| 1191 | sp, pulongest (top)); |
| 1192 | if (rslt) |
| 1193 | { |
| 1194 | if (sp <= 0) |
| 1195 | { |
| 1196 | /* This should be an error */ |
| 1197 | ax_debug ("Stack is empty, nothing to return"); |
| 1198 | return expr_eval_empty_stack; |
| 1199 | } |
| 1200 | *rslt = top; |
| 1201 | } |
| 1202 | return expr_eval_no_error; |
| 1203 | |
| 1204 | case gdb_agent_op_dup: |
| 1205 | stack[sp++] = top; |
| 1206 | break; |
| 1207 | |
| 1208 | case gdb_agent_op_pop: |
| 1209 | if (--sp >= 0) |
| 1210 | top = stack[sp]; |
| 1211 | break; |
| 1212 | |
| 1213 | case gdb_agent_op_pick: |
| 1214 | arg = aexpr->bytes[pc++]; |
| 1215 | stack[sp] = top; |
| 1216 | top = stack[sp - arg]; |
| 1217 | ++sp; |
| 1218 | break; |
| 1219 | |
| 1220 | case gdb_agent_op_rot: |
| 1221 | { |
| 1222 | ULONGEST tem = stack[sp - 1]; |
| 1223 | |
| 1224 | stack[sp - 1] = stack[sp - 2]; |
| 1225 | stack[sp - 2] = top; |
| 1226 | top = tem; |
| 1227 | } |
| 1228 | break; |
| 1229 | |
| 1230 | case gdb_agent_op_zero_ext: |
| 1231 | arg = aexpr->bytes[pc++]; |
| 1232 | if (arg < (sizeof (LONGEST) * 8)) |
| 1233 | top &= ((LONGEST) 1 << arg) - 1; |
| 1234 | break; |
| 1235 | |
| 1236 | case gdb_agent_op_swap: |
| 1237 | /* Interchange top two stack elements, making sure top gets |
| 1238 | copied back onto stack. */ |
| 1239 | stack[sp] = top; |
| 1240 | top = stack[sp - 1]; |
| 1241 | stack[sp - 1] = stack[sp]; |
| 1242 | break; |
| 1243 | |
| 1244 | case gdb_agent_op_getv: |
| 1245 | /* Flush the cached stack top. */ |
| 1246 | stack[sp++] = top; |
| 1247 | arg = aexpr->bytes[pc++]; |
| 1248 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 1249 | top = agent_get_trace_state_variable_value (arg); |
| 1250 | break; |
| 1251 | |
| 1252 | case gdb_agent_op_setv: |
| 1253 | arg = aexpr->bytes[pc++]; |
| 1254 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 1255 | agent_set_trace_state_variable_value (arg, top); |
| 1256 | /* Note that we leave the value on the stack, for the |
| 1257 | benefit of later/enclosing expressions. */ |
| 1258 | break; |
| 1259 | |
| 1260 | case gdb_agent_op_tracev: |
| 1261 | arg = aexpr->bytes[pc++]; |
| 1262 | arg = (arg << 8) + aexpr->bytes[pc++]; |
| 1263 | agent_tsv_read (ctx, arg); |
| 1264 | break; |
| 1265 | |
| 1266 | case gdb_agent_op_tracenz: |
| 1267 | agent_mem_read_string (ctx, NULL, (CORE_ADDR) stack[--sp], |
| 1268 | (ULONGEST) top); |
| 1269 | if (--sp >= 0) |
| 1270 | top = stack[sp]; |
| 1271 | break; |
| 1272 | |
| 1273 | case gdb_agent_op_printf: |
| 1274 | { |
| 1275 | int nargs, slen, i; |
| 1276 | CORE_ADDR fn = 0, chan = 0; |
| 1277 | /* Can't have more args than the entire size of the stack. */ |
| 1278 | ULONGEST args[STACK_MAX]; |
| 1279 | char *format; |
| 1280 | |
| 1281 | nargs = aexpr->bytes[pc++]; |
| 1282 | slen = aexpr->bytes[pc++]; |
| 1283 | slen = (slen << 8) + aexpr->bytes[pc++]; |
| 1284 | format = (char *) &(aexpr->bytes[pc]); |
| 1285 | pc += slen; |
| 1286 | /* Pop function and channel. */ |
| 1287 | fn = top; |
| 1288 | if (--sp >= 0) |
| 1289 | top = stack[sp]; |
| 1290 | chan = top; |
| 1291 | if (--sp >= 0) |
| 1292 | top = stack[sp]; |
| 1293 | /* Pop arguments into a dedicated array. */ |
| 1294 | for (i = 0; i < nargs; ++i) |
| 1295 | { |
| 1296 | args[i] = top; |
| 1297 | if (--sp >= 0) |
| 1298 | top = stack[sp]; |
| 1299 | } |
| 1300 | |
| 1301 | /* A bad format string means something is very wrong; give |
| 1302 | up immediately. */ |
| 1303 | if (format[slen - 1] != '\0') |
| 1304 | error (_("Unterminated format string in printf bytecode")); |
| 1305 | |
| 1306 | ax_printf (fn, chan, format, nargs, args); |
| 1307 | } |
| 1308 | break; |
| 1309 | |
| 1310 | /* GDB never (currently) generates any of these ops. */ |
| 1311 | case gdb_agent_op_float: |
| 1312 | case gdb_agent_op_ref_float: |
| 1313 | case gdb_agent_op_ref_double: |
| 1314 | case gdb_agent_op_ref_long_double: |
| 1315 | case gdb_agent_op_l_to_d: |
| 1316 | case gdb_agent_op_d_to_l: |
| 1317 | case gdb_agent_op_trace16: |
| 1318 | ax_debug ("Agent expression op 0x%x valid, but not handled", |
| 1319 | op); |
| 1320 | /* If ever GDB generates any of these, we don't have the |
| 1321 | option of ignoring. */ |
| 1322 | return 1; |
| 1323 | |
| 1324 | default: |
| 1325 | ax_debug ("Agent expression op 0x%x not recognized", op); |
| 1326 | /* Don't struggle on, things will just get worse. */ |
| 1327 | return expr_eval_unrecognized_opcode; |
| 1328 | } |
| 1329 | |
| 1330 | /* Check for stack badness. */ |
| 1331 | if (sp >= (STACK_MAX - 1)) |
| 1332 | { |
| 1333 | ax_debug ("Expression stack overflow"); |
| 1334 | return expr_eval_stack_overflow; |
| 1335 | } |
| 1336 | |
| 1337 | if (sp < 0) |
| 1338 | { |
| 1339 | ax_debug ("Expression stack underflow"); |
| 1340 | return expr_eval_stack_underflow; |
| 1341 | } |
| 1342 | |
| 1343 | ax_debug ("Op %s -> sp=%d, top=0x%s", |
| 1344 | gdb_agent_op_name (op), sp, phex_nz (top, 0)); |
| 1345 | } |
| 1346 | } |