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