GDBserver
|
00001 /* Agent expression code for remote server. 00002 Copyright (C) 2009-2013 Free Software Foundation, Inc. 00003 00004 This file is part of GDB. 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00018 00019 #include "server.h" 00020 #include "ax.h" 00021 #include "format.h" 00022 #include "tracepoint.h" 00023 00024 static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2); 00025 00026 #ifdef IN_PROCESS_AGENT 00027 int debug_agent = 0; 00028 #endif 00029 00030 static void 00031 ax_vdebug (const char *fmt, ...) 00032 { 00033 char buf[1024]; 00034 va_list ap; 00035 00036 va_start (ap, fmt); 00037 vsprintf (buf, fmt, ap); 00038 fprintf (stderr, PROG "/ax: %s\n", buf); 00039 va_end (ap); 00040 } 00041 00042 #define ax_debug_1(level, fmt, args...) \ 00043 do { \ 00044 if (level <= debug_threads) \ 00045 ax_vdebug ((fmt), ##args); \ 00046 } while (0) 00047 00048 #define ax_debug(FMT, args...) \ 00049 ax_debug_1 (1, FMT, ##args) 00050 00051 /* This enum must exactly match what is documented in 00052 gdb/doc/agentexpr.texi, including all the numerical values. */ 00053 00054 enum gdb_agent_op 00055 { 00056 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \ 00057 gdb_agent_op_ ## NAME = VALUE, 00058 #include "ax.def" 00059 #undef DEFOP 00060 gdb_agent_op_last 00061 }; 00062 00063 static const char *gdb_agent_op_names [gdb_agent_op_last] = 00064 { 00065 "?undef?" 00066 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME 00067 #include "ax.def" 00068 #undef DEFOP 00069 }; 00070 00071 static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] = 00072 { 00073 0 00074 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE 00075 #include "ax.def" 00076 #undef DEFOP 00077 }; 00078 00079 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */ 00080 00081 static const char * 00082 gdb_agent_op_name (int op) 00083 { 00084 if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL) 00085 return "?undef?"; 00086 return gdb_agent_op_names[op]; 00087 } 00088 00089 #ifndef IN_PROCESS_AGENT 00090 00091 /* The packet form of an agent expression consists of an 'X', number 00092 of bytes in expression, a comma, and then the bytes. */ 00093 00094 struct agent_expr * 00095 gdb_parse_agent_expr (char **actparm) 00096 { 00097 char *act = *actparm; 00098 ULONGEST xlen; 00099 struct agent_expr *aexpr; 00100 00101 ++act; /* skip the X */ 00102 act = unpack_varlen_hex (act, &xlen); 00103 ++act; /* skip a comma */ 00104 aexpr = xmalloc (sizeof (struct agent_expr)); 00105 aexpr->length = xlen; 00106 aexpr->bytes = xmalloc (xlen); 00107 convert_ascii_to_int (act, aexpr->bytes, xlen); 00108 *actparm = act + (xlen * 2); 00109 return aexpr; 00110 } 00111 00112 /* Convert the bytes of an agent expression back into hex digits, so 00113 they can be printed or uploaded. This allocates the buffer, 00114 callers should free when they are done with it. */ 00115 00116 char * 00117 gdb_unparse_agent_expr (struct agent_expr *aexpr) 00118 { 00119 char *rslt; 00120 00121 rslt = xmalloc (2 * aexpr->length + 1); 00122 convert_int_to_ascii (aexpr->bytes, rslt, aexpr->length); 00123 return rslt; 00124 } 00125 00126 /* Bytecode compilation. */ 00127 00128 CORE_ADDR current_insn_ptr; 00129 00130 int emit_error; 00131 00132 struct bytecode_address 00133 { 00134 int pc; 00135 CORE_ADDR address; 00136 int goto_pc; 00137 /* Offset and size of field to be modified in the goto block. */ 00138 int from_offset, from_size; 00139 struct bytecode_address *next; 00140 } *bytecode_address_table; 00141 00142 void 00143 emit_prologue (void) 00144 { 00145 target_emit_ops ()->emit_prologue (); 00146 } 00147 00148 void 00149 emit_epilogue (void) 00150 { 00151 target_emit_ops ()->emit_epilogue (); 00152 } 00153 00154 static void 00155 emit_add (void) 00156 { 00157 target_emit_ops ()->emit_add (); 00158 } 00159 00160 static void 00161 emit_sub (void) 00162 { 00163 target_emit_ops ()->emit_sub (); 00164 } 00165 00166 static void 00167 emit_mul (void) 00168 { 00169 target_emit_ops ()->emit_mul (); 00170 } 00171 00172 static void 00173 emit_lsh (void) 00174 { 00175 target_emit_ops ()->emit_lsh (); 00176 } 00177 00178 static void 00179 emit_rsh_signed (void) 00180 { 00181 target_emit_ops ()->emit_rsh_signed (); 00182 } 00183 00184 static void 00185 emit_rsh_unsigned (void) 00186 { 00187 target_emit_ops ()->emit_rsh_unsigned (); 00188 } 00189 00190 static void 00191 emit_ext (int arg) 00192 { 00193 target_emit_ops ()->emit_ext (arg); 00194 } 00195 00196 static void 00197 emit_log_not (void) 00198 { 00199 target_emit_ops ()->emit_log_not (); 00200 } 00201 00202 static void 00203 emit_bit_and (void) 00204 { 00205 target_emit_ops ()->emit_bit_and (); 00206 } 00207 00208 static void 00209 emit_bit_or (void) 00210 { 00211 target_emit_ops ()->emit_bit_or (); 00212 } 00213 00214 static void 00215 emit_bit_xor (void) 00216 { 00217 target_emit_ops ()->emit_bit_xor (); 00218 } 00219 00220 static void 00221 emit_bit_not (void) 00222 { 00223 target_emit_ops ()->emit_bit_not (); 00224 } 00225 00226 static void 00227 emit_equal (void) 00228 { 00229 target_emit_ops ()->emit_equal (); 00230 } 00231 00232 static void 00233 emit_less_signed (void) 00234 { 00235 target_emit_ops ()->emit_less_signed (); 00236 } 00237 00238 static void 00239 emit_less_unsigned (void) 00240 { 00241 target_emit_ops ()->emit_less_unsigned (); 00242 } 00243 00244 static void 00245 emit_ref (int size) 00246 { 00247 target_emit_ops ()->emit_ref (size); 00248 } 00249 00250 static void 00251 emit_if_goto (int *offset_p, int *size_p) 00252 { 00253 target_emit_ops ()->emit_if_goto (offset_p, size_p); 00254 } 00255 00256 static void 00257 emit_goto (int *offset_p, int *size_p) 00258 { 00259 target_emit_ops ()->emit_goto (offset_p, size_p); 00260 } 00261 00262 static void 00263 write_goto_address (CORE_ADDR from, CORE_ADDR to, int size) 00264 { 00265 target_emit_ops ()->write_goto_address (from, to, size); 00266 } 00267 00268 static void 00269 emit_const (LONGEST num) 00270 { 00271 target_emit_ops ()->emit_const (num); 00272 } 00273 00274 static void 00275 emit_reg (int reg) 00276 { 00277 target_emit_ops ()->emit_reg (reg); 00278 } 00279 00280 static void 00281 emit_pop (void) 00282 { 00283 target_emit_ops ()->emit_pop (); 00284 } 00285 00286 static void 00287 emit_stack_flush (void) 00288 { 00289 target_emit_ops ()->emit_stack_flush (); 00290 } 00291 00292 static void 00293 emit_zero_ext (int arg) 00294 { 00295 target_emit_ops ()->emit_zero_ext (arg); 00296 } 00297 00298 static void 00299 emit_swap (void) 00300 { 00301 target_emit_ops ()->emit_swap (); 00302 } 00303 00304 static void 00305 emit_stack_adjust (int n) 00306 { 00307 target_emit_ops ()->emit_stack_adjust (n); 00308 } 00309 00310 /* FN's prototype is `LONGEST(*fn)(int)'. */ 00311 00312 static void 00313 emit_int_call_1 (CORE_ADDR fn, int arg1) 00314 { 00315 target_emit_ops ()->emit_int_call_1 (fn, arg1); 00316 } 00317 00318 /* FN's prototype is `void(*fn)(int,LONGEST)'. */ 00319 00320 static void 00321 emit_void_call_2 (CORE_ADDR fn, int arg1) 00322 { 00323 target_emit_ops ()->emit_void_call_2 (fn, arg1); 00324 } 00325 00326 static void 00327 emit_eq_goto (int *offset_p, int *size_p) 00328 { 00329 target_emit_ops ()->emit_eq_goto (offset_p, size_p); 00330 } 00331 00332 static void 00333 emit_ne_goto (int *offset_p, int *size_p) 00334 { 00335 target_emit_ops ()->emit_ne_goto (offset_p, size_p); 00336 } 00337 00338 static void 00339 emit_lt_goto (int *offset_p, int *size_p) 00340 { 00341 target_emit_ops ()->emit_lt_goto (offset_p, size_p); 00342 } 00343 00344 static void 00345 emit_ge_goto (int *offset_p, int *size_p) 00346 { 00347 target_emit_ops ()->emit_ge_goto (offset_p, size_p); 00348 } 00349 00350 static void 00351 emit_gt_goto (int *offset_p, int *size_p) 00352 { 00353 target_emit_ops ()->emit_gt_goto (offset_p, size_p); 00354 } 00355 00356 static void 00357 emit_le_goto (int *offset_p, int *size_p) 00358 { 00359 target_emit_ops ()->emit_le_goto (offset_p, size_p); 00360 } 00361 00362 /* Scan an agent expression for any evidence that the given PC is the 00363 target of a jump bytecode in the expression. */ 00364 00365 int 00366 is_goto_target (struct agent_expr *aexpr, int pc) 00367 { 00368 int i; 00369 unsigned char op; 00370 00371 for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op]) 00372 { 00373 op = aexpr->bytes[i]; 00374 00375 if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto) 00376 { 00377 int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2]; 00378 if (target == pc) 00379 return 1; 00380 } 00381 } 00382 00383 return 0; 00384 } 00385 00386 /* Given an agent expression, turn it into native code. */ 00387 00388 enum eval_result_type 00389 compile_bytecodes (struct agent_expr *aexpr) 00390 { 00391 int pc = 0; 00392 int done = 0; 00393 unsigned char op, next_op; 00394 int arg; 00395 /* This is only used to build 64-bit value for constants. */ 00396 ULONGEST top; 00397 struct bytecode_address *aentry, *aentry2; 00398 00399 #define UNHANDLED \ 00400 do \ 00401 { \ 00402 ax_debug ("Cannot compile op 0x%x\n", op); \ 00403 return expr_eval_unhandled_opcode; \ 00404 } while (0) 00405 00406 if (aexpr->length == 0) 00407 { 00408 ax_debug ("empty agent expression\n"); 00409 return expr_eval_empty_expression; 00410 } 00411 00412 bytecode_address_table = NULL; 00413 00414 while (!done) 00415 { 00416 op = aexpr->bytes[pc]; 00417 00418 ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc); 00419 00420 /* Record the compiled-code address of the bytecode, for use by 00421 jump instructions. */ 00422 aentry = xmalloc (sizeof (struct bytecode_address)); 00423 aentry->pc = pc; 00424 aentry->address = current_insn_ptr; 00425 aentry->goto_pc = -1; 00426 aentry->from_offset = aentry->from_size = 0; 00427 aentry->next = bytecode_address_table; 00428 bytecode_address_table = aentry; 00429 00430 ++pc; 00431 00432 emit_error = 0; 00433 00434 switch (op) 00435 { 00436 case gdb_agent_op_add: 00437 emit_add (); 00438 break; 00439 00440 case gdb_agent_op_sub: 00441 emit_sub (); 00442 break; 00443 00444 case gdb_agent_op_mul: 00445 emit_mul (); 00446 break; 00447 00448 case gdb_agent_op_div_signed: 00449 UNHANDLED; 00450 break; 00451 00452 case gdb_agent_op_div_unsigned: 00453 UNHANDLED; 00454 break; 00455 00456 case gdb_agent_op_rem_signed: 00457 UNHANDLED; 00458 break; 00459 00460 case gdb_agent_op_rem_unsigned: 00461 UNHANDLED; 00462 break; 00463 00464 case gdb_agent_op_lsh: 00465 emit_lsh (); 00466 break; 00467 00468 case gdb_agent_op_rsh_signed: 00469 emit_rsh_signed (); 00470 break; 00471 00472 case gdb_agent_op_rsh_unsigned: 00473 emit_rsh_unsigned (); 00474 break; 00475 00476 case gdb_agent_op_trace: 00477 UNHANDLED; 00478 break; 00479 00480 case gdb_agent_op_trace_quick: 00481 UNHANDLED; 00482 break; 00483 00484 case gdb_agent_op_log_not: 00485 emit_log_not (); 00486 break; 00487 00488 case gdb_agent_op_bit_and: 00489 emit_bit_and (); 00490 break; 00491 00492 case gdb_agent_op_bit_or: 00493 emit_bit_or (); 00494 break; 00495 00496 case gdb_agent_op_bit_xor: 00497 emit_bit_xor (); 00498 break; 00499 00500 case gdb_agent_op_bit_not: 00501 emit_bit_not (); 00502 break; 00503 00504 case gdb_agent_op_equal: 00505 next_op = aexpr->bytes[pc]; 00506 if (next_op == gdb_agent_op_if_goto 00507 && !is_goto_target (aexpr, pc) 00508 && target_emit_ops ()->emit_eq_goto) 00509 { 00510 ax_debug ("Combining equal & if_goto"); 00511 pc += 1; 00512 aentry->pc = pc; 00513 arg = aexpr->bytes[pc++]; 00514 arg = (arg << 8) + aexpr->bytes[pc++]; 00515 aentry->goto_pc = arg; 00516 emit_eq_goto (&(aentry->from_offset), &(aentry->from_size)); 00517 } 00518 else if (next_op == gdb_agent_op_log_not 00519 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto) 00520 && !is_goto_target (aexpr, pc + 1) 00521 && target_emit_ops ()->emit_ne_goto) 00522 { 00523 ax_debug ("Combining equal & log_not & if_goto"); 00524 pc += 2; 00525 aentry->pc = pc; 00526 arg = aexpr->bytes[pc++]; 00527 arg = (arg << 8) + aexpr->bytes[pc++]; 00528 aentry->goto_pc = arg; 00529 emit_ne_goto (&(aentry->from_offset), &(aentry->from_size)); 00530 } 00531 else 00532 emit_equal (); 00533 break; 00534 00535 case gdb_agent_op_less_signed: 00536 next_op = aexpr->bytes[pc]; 00537 if (next_op == gdb_agent_op_if_goto 00538 && !is_goto_target (aexpr, pc)) 00539 { 00540 ax_debug ("Combining less_signed & if_goto"); 00541 pc += 1; 00542 aentry->pc = pc; 00543 arg = aexpr->bytes[pc++]; 00544 arg = (arg << 8) + aexpr->bytes[pc++]; 00545 aentry->goto_pc = arg; 00546 emit_lt_goto (&(aentry->from_offset), &(aentry->from_size)); 00547 } 00548 else if (next_op == gdb_agent_op_log_not 00549 && !is_goto_target (aexpr, pc) 00550 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto) 00551 && !is_goto_target (aexpr, pc + 1)) 00552 { 00553 ax_debug ("Combining less_signed & log_not & if_goto"); 00554 pc += 2; 00555 aentry->pc = pc; 00556 arg = aexpr->bytes[pc++]; 00557 arg = (arg << 8) + aexpr->bytes[pc++]; 00558 aentry->goto_pc = arg; 00559 emit_ge_goto (&(aentry->from_offset), &(aentry->from_size)); 00560 } 00561 else 00562 emit_less_signed (); 00563 break; 00564 00565 case gdb_agent_op_less_unsigned: 00566 emit_less_unsigned (); 00567 break; 00568 00569 case gdb_agent_op_ext: 00570 arg = aexpr->bytes[pc++]; 00571 if (arg < (sizeof (LONGEST) * 8)) 00572 emit_ext (arg); 00573 break; 00574 00575 case gdb_agent_op_ref8: 00576 emit_ref (1); 00577 break; 00578 00579 case gdb_agent_op_ref16: 00580 emit_ref (2); 00581 break; 00582 00583 case gdb_agent_op_ref32: 00584 emit_ref (4); 00585 break; 00586 00587 case gdb_agent_op_ref64: 00588 emit_ref (8); 00589 break; 00590 00591 case gdb_agent_op_if_goto: 00592 arg = aexpr->bytes[pc++]; 00593 arg = (arg << 8) + aexpr->bytes[pc++]; 00594 aentry->goto_pc = arg; 00595 emit_if_goto (&(aentry->from_offset), &(aentry->from_size)); 00596 break; 00597 00598 case gdb_agent_op_goto: 00599 arg = aexpr->bytes[pc++]; 00600 arg = (arg << 8) + aexpr->bytes[pc++]; 00601 aentry->goto_pc = arg; 00602 emit_goto (&(aentry->from_offset), &(aentry->from_size)); 00603 break; 00604 00605 case gdb_agent_op_const8: 00606 emit_stack_flush (); 00607 top = aexpr->bytes[pc++]; 00608 emit_const (top); 00609 break; 00610 00611 case gdb_agent_op_const16: 00612 emit_stack_flush (); 00613 top = aexpr->bytes[pc++]; 00614 top = (top << 8) + aexpr->bytes[pc++]; 00615 emit_const (top); 00616 break; 00617 00618 case gdb_agent_op_const32: 00619 emit_stack_flush (); 00620 top = aexpr->bytes[pc++]; 00621 top = (top << 8) + aexpr->bytes[pc++]; 00622 top = (top << 8) + aexpr->bytes[pc++]; 00623 top = (top << 8) + aexpr->bytes[pc++]; 00624 emit_const (top); 00625 break; 00626 00627 case gdb_agent_op_const64: 00628 emit_stack_flush (); 00629 top = aexpr->bytes[pc++]; 00630 top = (top << 8) + aexpr->bytes[pc++]; 00631 top = (top << 8) + aexpr->bytes[pc++]; 00632 top = (top << 8) + aexpr->bytes[pc++]; 00633 top = (top << 8) + aexpr->bytes[pc++]; 00634 top = (top << 8) + aexpr->bytes[pc++]; 00635 top = (top << 8) + aexpr->bytes[pc++]; 00636 top = (top << 8) + aexpr->bytes[pc++]; 00637 emit_const (top); 00638 break; 00639 00640 case gdb_agent_op_reg: 00641 emit_stack_flush (); 00642 arg = aexpr->bytes[pc++]; 00643 arg = (arg << 8) + aexpr->bytes[pc++]; 00644 emit_reg (arg); 00645 break; 00646 00647 case gdb_agent_op_end: 00648 ax_debug ("At end of expression\n"); 00649 00650 /* Assume there is one stack element left, and that it is 00651 cached in "top" where emit_epilogue can get to it. */ 00652 emit_stack_adjust (1); 00653 00654 done = 1; 00655 break; 00656 00657 case gdb_agent_op_dup: 00658 /* In our design, dup is equivalent to stack flushing. */ 00659 emit_stack_flush (); 00660 break; 00661 00662 case gdb_agent_op_pop: 00663 emit_pop (); 00664 break; 00665 00666 case gdb_agent_op_zero_ext: 00667 arg = aexpr->bytes[pc++]; 00668 if (arg < (sizeof (LONGEST) * 8)) 00669 emit_zero_ext (arg); 00670 break; 00671 00672 case gdb_agent_op_swap: 00673 next_op = aexpr->bytes[pc]; 00674 /* Detect greater-than comparison sequences. */ 00675 if (next_op == gdb_agent_op_less_signed 00676 && !is_goto_target (aexpr, pc) 00677 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto) 00678 && !is_goto_target (aexpr, pc + 1)) 00679 { 00680 ax_debug ("Combining swap & less_signed & if_goto"); 00681 pc += 2; 00682 aentry->pc = pc; 00683 arg = aexpr->bytes[pc++]; 00684 arg = (arg << 8) + aexpr->bytes[pc++]; 00685 aentry->goto_pc = arg; 00686 emit_gt_goto (&(aentry->from_offset), &(aentry->from_size)); 00687 } 00688 else if (next_op == gdb_agent_op_less_signed 00689 && !is_goto_target (aexpr, pc) 00690 && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not) 00691 && !is_goto_target (aexpr, pc + 1) 00692 && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto) 00693 && !is_goto_target (aexpr, pc + 2)) 00694 { 00695 ax_debug ("Combining swap & less_signed & log_not & if_goto"); 00696 pc += 3; 00697 aentry->pc = pc; 00698 arg = aexpr->bytes[pc++]; 00699 arg = (arg << 8) + aexpr->bytes[pc++]; 00700 aentry->goto_pc = arg; 00701 emit_le_goto (&(aentry->from_offset), &(aentry->from_size)); 00702 } 00703 else 00704 emit_swap (); 00705 break; 00706 00707 case gdb_agent_op_getv: 00708 emit_stack_flush (); 00709 arg = aexpr->bytes[pc++]; 00710 arg = (arg << 8) + aexpr->bytes[pc++]; 00711 emit_int_call_1 (get_get_tsv_func_addr (), 00712 arg); 00713 break; 00714 00715 case gdb_agent_op_setv: 00716 arg = aexpr->bytes[pc++]; 00717 arg = (arg << 8) + aexpr->bytes[pc++]; 00718 emit_void_call_2 (get_set_tsv_func_addr (), 00719 arg); 00720 break; 00721 00722 case gdb_agent_op_tracev: 00723 UNHANDLED; 00724 break; 00725 00726 /* GDB never (currently) generates any of these ops. */ 00727 case gdb_agent_op_float: 00728 case gdb_agent_op_ref_float: 00729 case gdb_agent_op_ref_double: 00730 case gdb_agent_op_ref_long_double: 00731 case gdb_agent_op_l_to_d: 00732 case gdb_agent_op_d_to_l: 00733 case gdb_agent_op_trace16: 00734 UNHANDLED; 00735 break; 00736 00737 default: 00738 ax_debug ("Agent expression op 0x%x not recognized\n", op); 00739 /* Don't struggle on, things will just get worse. */ 00740 return expr_eval_unrecognized_opcode; 00741 } 00742 00743 /* This catches errors that occur in target-specific code 00744 emission. */ 00745 if (emit_error) 00746 { 00747 ax_debug ("Error %d while emitting code for %s\n", 00748 emit_error, gdb_agent_op_name (op)); 00749 return expr_eval_unhandled_opcode; 00750 } 00751 00752 ax_debug ("Op %s compiled\n", gdb_agent_op_name (op)); 00753 } 00754 00755 /* Now fill in real addresses as goto destinations. */ 00756 for (aentry = bytecode_address_table; aentry; aentry = aentry->next) 00757 { 00758 int written = 0; 00759 00760 if (aentry->goto_pc < 0) 00761 continue; 00762 00763 /* Find the location that we are going to, and call back into 00764 target-specific code to write the actual address or 00765 displacement. */ 00766 for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next) 00767 { 00768 if (aentry2->pc == aentry->goto_pc) 00769 { 00770 ax_debug ("Want to jump from %s to %s\n", 00771 paddress (aentry->address), 00772 paddress (aentry2->address)); 00773 write_goto_address (aentry->address + aentry->from_offset, 00774 aentry2->address, aentry->from_size); 00775 written = 1; 00776 break; 00777 } 00778 } 00779 00780 /* Error out if we didn't find a destination. */ 00781 if (!written) 00782 { 00783 ax_debug ("Destination of goto %d not found\n", 00784 aentry->goto_pc); 00785 return expr_eval_invalid_goto; 00786 } 00787 } 00788 00789 return expr_eval_no_error; 00790 } 00791 00792 #endif 00793 00794 /* Make printf-type calls using arguments supplied from the host. We 00795 need to parse the format string ourselves, and call the formatting 00796 function with one argument at a time, partly because there is no 00797 safe portable way to construct a varargs call, and partly to serve 00798 as a security barrier against bad format strings that might get 00799 in. */ 00800 00801 static void 00802 ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format, 00803 int nargs, ULONGEST *args) 00804 { 00805 const char *f = format; 00806 struct format_piece *fpieces; 00807 int i, fp; 00808 char *current_substring; 00809 int nargs_wanted; 00810 00811 ax_debug ("Printf of \"%s\" with %d args", format, nargs); 00812 00813 fpieces = parse_format_string (&f); 00814 00815 nargs_wanted = 0; 00816 for (fp = 0; fpieces[fp].string != NULL; fp++) 00817 if (fpieces[fp].argclass != literal_piece) 00818 ++nargs_wanted; 00819 00820 if (nargs != nargs_wanted) 00821 error (_("Wrong number of arguments for specified format-string")); 00822 00823 i = 0; 00824 for (fp = 0; fpieces[fp].string != NULL; fp++) 00825 { 00826 current_substring = fpieces[fp].string; 00827 ax_debug ("current substring is '%s', class is %d", 00828 current_substring, fpieces[fp].argclass); 00829 switch (fpieces[fp].argclass) 00830 { 00831 case string_arg: 00832 { 00833 gdb_byte *str; 00834 CORE_ADDR tem; 00835 int j; 00836 00837 tem = args[i]; 00838 00839 /* This is a %s argument. Find the length of the string. */ 00840 for (j = 0;; j++) 00841 { 00842 gdb_byte c; 00843 00844 read_inferior_memory (tem + j, &c, 1); 00845 if (c == 0) 00846 break; 00847 } 00848 00849 /* Copy the string contents into a string inside GDB. */ 00850 str = (gdb_byte *) alloca (j + 1); 00851 if (j != 0) 00852 read_inferior_memory (tem, str, j); 00853 str[j] = 0; 00854 00855 printf (current_substring, (char *) str); 00856 } 00857 break; 00858 00859 case long_long_arg: 00860 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG) 00861 { 00862 long long val = args[i]; 00863 00864 printf (current_substring, val); 00865 break; 00866 } 00867 #else 00868 error (_("long long not supported in agent printf")); 00869 #endif 00870 case int_arg: 00871 { 00872 int val = args[i]; 00873 00874 printf (current_substring, val); 00875 break; 00876 } 00877 00878 case long_arg: 00879 { 00880 long val = args[i]; 00881 00882 printf (current_substring, val); 00883 break; 00884 } 00885 00886 case literal_piece: 00887 /* Print a portion of the format string that has no 00888 directives. Note that this will not include any 00889 ordinary %-specs, but it might include "%%". That is 00890 why we use printf_filtered and not puts_filtered here. 00891 Also, we pass a dummy argument because some platforms 00892 have modified GCC to include -Wformat-security by 00893 default, which will warn here if there is no 00894 argument. */ 00895 printf (current_substring, 0); 00896 break; 00897 00898 default: 00899 error (_("Format directive in '%s' not supported in agent printf"), 00900 current_substring); 00901 } 00902 00903 /* Maybe advance to the next argument. */ 00904 if (fpieces[fp].argclass != literal_piece) 00905 ++i; 00906 } 00907 00908 free_format_pieces (fpieces); 00909 fflush (stdout); 00910 } 00911 00912 /* The agent expression evaluator, as specified by the GDB docs. It 00913 returns 0 if everything went OK, and a nonzero error code 00914 otherwise. */ 00915 00916 enum eval_result_type 00917 gdb_eval_agent_expr (struct eval_agent_expr_context *ctx, 00918 struct agent_expr *aexpr, 00919 ULONGEST *rslt) 00920 { 00921 int pc = 0; 00922 #define STACK_MAX 100 00923 ULONGEST stack[STACK_MAX], top; 00924 int sp = 0; 00925 unsigned char op; 00926 int arg; 00927 00928 /* This union is a convenient way to convert representations. For 00929 now, assume a standard architecture where the hardware integer 00930 types have 8, 16, 32, 64 bit types. A more robust solution would 00931 be to import stdint.h from gnulib. */ 00932 union 00933 { 00934 union 00935 { 00936 unsigned char bytes[1]; 00937 unsigned char val; 00938 } u8; 00939 union 00940 { 00941 unsigned char bytes[2]; 00942 unsigned short val; 00943 } u16; 00944 union 00945 { 00946 unsigned char bytes[4]; 00947 unsigned int val; 00948 } u32; 00949 union 00950 { 00951 unsigned char bytes[8]; 00952 ULONGEST val; 00953 } u64; 00954 } cnv; 00955 00956 if (aexpr->length == 0) 00957 { 00958 ax_debug ("empty agent expression"); 00959 return expr_eval_empty_expression; 00960 } 00961 00962 /* Cache the stack top in its own variable. Much of the time we can 00963 operate on this variable, rather than dinking with the stack. It 00964 needs to be copied to the stack when sp changes. */ 00965 top = 0; 00966 00967 while (1) 00968 { 00969 op = aexpr->bytes[pc++]; 00970 00971 ax_debug ("About to interpret byte 0x%x", op); 00972 00973 switch (op) 00974 { 00975 case gdb_agent_op_add: 00976 top += stack[--sp]; 00977 break; 00978 00979 case gdb_agent_op_sub: 00980 top = stack[--sp] - top; 00981 break; 00982 00983 case gdb_agent_op_mul: 00984 top *= stack[--sp]; 00985 break; 00986 00987 case gdb_agent_op_div_signed: 00988 if (top == 0) 00989 { 00990 ax_debug ("Attempted to divide by zero"); 00991 return expr_eval_divide_by_zero; 00992 } 00993 top = ((LONGEST) stack[--sp]) / ((LONGEST) top); 00994 break; 00995 00996 case gdb_agent_op_div_unsigned: 00997 if (top == 0) 00998 { 00999 ax_debug ("Attempted to divide by zero"); 01000 return expr_eval_divide_by_zero; 01001 } 01002 top = stack[--sp] / top; 01003 break; 01004 01005 case gdb_agent_op_rem_signed: 01006 if (top == 0) 01007 { 01008 ax_debug ("Attempted to divide by zero"); 01009 return expr_eval_divide_by_zero; 01010 } 01011 top = ((LONGEST) stack[--sp]) % ((LONGEST) top); 01012 break; 01013 01014 case gdb_agent_op_rem_unsigned: 01015 if (top == 0) 01016 { 01017 ax_debug ("Attempted to divide by zero"); 01018 return expr_eval_divide_by_zero; 01019 } 01020 top = stack[--sp] % top; 01021 break; 01022 01023 case gdb_agent_op_lsh: 01024 top = stack[--sp] << top; 01025 break; 01026 01027 case gdb_agent_op_rsh_signed: 01028 top = ((LONGEST) stack[--sp]) >> top; 01029 break; 01030 01031 case gdb_agent_op_rsh_unsigned: 01032 top = stack[--sp] >> top; 01033 break; 01034 01035 case gdb_agent_op_trace: 01036 agent_mem_read (ctx, NULL, (CORE_ADDR) stack[--sp], 01037 (ULONGEST) top); 01038 if (--sp >= 0) 01039 top = stack[sp]; 01040 break; 01041 01042 case gdb_agent_op_trace_quick: 01043 arg = aexpr->bytes[pc++]; 01044 agent_mem_read (ctx, NULL, (CORE_ADDR) top, (ULONGEST) arg); 01045 break; 01046 01047 case gdb_agent_op_log_not: 01048 top = !top; 01049 break; 01050 01051 case gdb_agent_op_bit_and: 01052 top &= stack[--sp]; 01053 break; 01054 01055 case gdb_agent_op_bit_or: 01056 top |= stack[--sp]; 01057 break; 01058 01059 case gdb_agent_op_bit_xor: 01060 top ^= stack[--sp]; 01061 break; 01062 01063 case gdb_agent_op_bit_not: 01064 top = ~top; 01065 break; 01066 01067 case gdb_agent_op_equal: 01068 top = (stack[--sp] == top); 01069 break; 01070 01071 case gdb_agent_op_less_signed: 01072 top = (((LONGEST) stack[--sp]) < ((LONGEST) top)); 01073 break; 01074 01075 case gdb_agent_op_less_unsigned: 01076 top = (stack[--sp] < top); 01077 break; 01078 01079 case gdb_agent_op_ext: 01080 arg = aexpr->bytes[pc++]; 01081 if (arg < (sizeof (LONGEST) * 8)) 01082 { 01083 LONGEST mask = 1 << (arg - 1); 01084 top &= ((LONGEST) 1 << arg) - 1; 01085 top = (top ^ mask) - mask; 01086 } 01087 break; 01088 01089 case gdb_agent_op_ref8: 01090 agent_mem_read (ctx, cnv.u8.bytes, (CORE_ADDR) top, 1); 01091 top = cnv.u8.val; 01092 break; 01093 01094 case gdb_agent_op_ref16: 01095 agent_mem_read (ctx, cnv.u16.bytes, (CORE_ADDR) top, 2); 01096 top = cnv.u16.val; 01097 break; 01098 01099 case gdb_agent_op_ref32: 01100 agent_mem_read (ctx, cnv.u32.bytes, (CORE_ADDR) top, 4); 01101 top = cnv.u32.val; 01102 break; 01103 01104 case gdb_agent_op_ref64: 01105 agent_mem_read (ctx, cnv.u64.bytes, (CORE_ADDR) top, 8); 01106 top = cnv.u64.val; 01107 break; 01108 01109 case gdb_agent_op_if_goto: 01110 if (top) 01111 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]); 01112 else 01113 pc += 2; 01114 if (--sp >= 0) 01115 top = stack[sp]; 01116 break; 01117 01118 case gdb_agent_op_goto: 01119 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]); 01120 break; 01121 01122 case gdb_agent_op_const8: 01123 /* Flush the cached stack top. */ 01124 stack[sp++] = top; 01125 top = aexpr->bytes[pc++]; 01126 break; 01127 01128 case gdb_agent_op_const16: 01129 /* Flush the cached stack top. */ 01130 stack[sp++] = top; 01131 top = aexpr->bytes[pc++]; 01132 top = (top << 8) + aexpr->bytes[pc++]; 01133 break; 01134 01135 case gdb_agent_op_const32: 01136 /* Flush the cached stack top. */ 01137 stack[sp++] = top; 01138 top = aexpr->bytes[pc++]; 01139 top = (top << 8) + aexpr->bytes[pc++]; 01140 top = (top << 8) + aexpr->bytes[pc++]; 01141 top = (top << 8) + aexpr->bytes[pc++]; 01142 break; 01143 01144 case gdb_agent_op_const64: 01145 /* Flush the cached stack top. */ 01146 stack[sp++] = top; 01147 top = aexpr->bytes[pc++]; 01148 top = (top << 8) + aexpr->bytes[pc++]; 01149 top = (top << 8) + aexpr->bytes[pc++]; 01150 top = (top << 8) + aexpr->bytes[pc++]; 01151 top = (top << 8) + aexpr->bytes[pc++]; 01152 top = (top << 8) + aexpr->bytes[pc++]; 01153 top = (top << 8) + aexpr->bytes[pc++]; 01154 top = (top << 8) + aexpr->bytes[pc++]; 01155 break; 01156 01157 case gdb_agent_op_reg: 01158 /* Flush the cached stack top. */ 01159 stack[sp++] = top; 01160 arg = aexpr->bytes[pc++]; 01161 arg = (arg << 8) + aexpr->bytes[pc++]; 01162 { 01163 int regnum = arg; 01164 struct regcache *regcache = ctx->regcache; 01165 01166 switch (register_size (regcache->tdesc, regnum)) 01167 { 01168 case 8: 01169 collect_register (regcache, regnum, cnv.u64.bytes); 01170 top = cnv.u64.val; 01171 break; 01172 case 4: 01173 collect_register (regcache, regnum, cnv.u32.bytes); 01174 top = cnv.u32.val; 01175 break; 01176 case 2: 01177 collect_register (regcache, regnum, cnv.u16.bytes); 01178 top = cnv.u16.val; 01179 break; 01180 case 1: 01181 collect_register (regcache, regnum, cnv.u8.bytes); 01182 top = cnv.u8.val; 01183 break; 01184 default: 01185 internal_error (__FILE__, __LINE__, 01186 "unhandled register size"); 01187 } 01188 } 01189 break; 01190 01191 case gdb_agent_op_end: 01192 ax_debug ("At end of expression, sp=%d, stack top cache=0x%s", 01193 sp, pulongest (top)); 01194 if (rslt) 01195 { 01196 if (sp <= 0) 01197 { 01198 /* This should be an error */ 01199 ax_debug ("Stack is empty, nothing to return"); 01200 return expr_eval_empty_stack; 01201 } 01202 *rslt = top; 01203 } 01204 return expr_eval_no_error; 01205 01206 case gdb_agent_op_dup: 01207 stack[sp++] = top; 01208 break; 01209 01210 case gdb_agent_op_pop: 01211 if (--sp >= 0) 01212 top = stack[sp]; 01213 break; 01214 01215 case gdb_agent_op_pick: 01216 arg = aexpr->bytes[pc++]; 01217 stack[sp] = top; 01218 top = stack[sp - arg]; 01219 ++sp; 01220 break; 01221 01222 case gdb_agent_op_rot: 01223 { 01224 ULONGEST tem = stack[sp - 1]; 01225 01226 stack[sp - 1] = stack[sp - 2]; 01227 stack[sp - 2] = top; 01228 top = tem; 01229 } 01230 break; 01231 01232 case gdb_agent_op_zero_ext: 01233 arg = aexpr->bytes[pc++]; 01234 if (arg < (sizeof (LONGEST) * 8)) 01235 top &= ((LONGEST) 1 << arg) - 1; 01236 break; 01237 01238 case gdb_agent_op_swap: 01239 /* Interchange top two stack elements, making sure top gets 01240 copied back onto stack. */ 01241 stack[sp] = top; 01242 top = stack[sp - 1]; 01243 stack[sp - 1] = stack[sp]; 01244 break; 01245 01246 case gdb_agent_op_getv: 01247 /* Flush the cached stack top. */ 01248 stack[sp++] = top; 01249 arg = aexpr->bytes[pc++]; 01250 arg = (arg << 8) + aexpr->bytes[pc++]; 01251 top = agent_get_trace_state_variable_value (arg); 01252 break; 01253 01254 case gdb_agent_op_setv: 01255 arg = aexpr->bytes[pc++]; 01256 arg = (arg << 8) + aexpr->bytes[pc++]; 01257 agent_set_trace_state_variable_value (arg, top); 01258 /* Note that we leave the value on the stack, for the 01259 benefit of later/enclosing expressions. */ 01260 break; 01261 01262 case gdb_agent_op_tracev: 01263 arg = aexpr->bytes[pc++]; 01264 arg = (arg << 8) + aexpr->bytes[pc++]; 01265 agent_tsv_read (ctx, arg); 01266 break; 01267 01268 case gdb_agent_op_tracenz: 01269 agent_mem_read_string (ctx, NULL, (CORE_ADDR) stack[--sp], 01270 (ULONGEST) top); 01271 if (--sp >= 0) 01272 top = stack[sp]; 01273 break; 01274 01275 case gdb_agent_op_printf: 01276 { 01277 int nargs, slen, i; 01278 CORE_ADDR fn = 0, chan = 0; 01279 /* Can't have more args than the entire size of the stack. */ 01280 ULONGEST args[STACK_MAX]; 01281 char *format; 01282 01283 nargs = aexpr->bytes[pc++]; 01284 slen = aexpr->bytes[pc++]; 01285 slen = (slen << 8) + aexpr->bytes[pc++]; 01286 format = (char *) &(aexpr->bytes[pc]); 01287 pc += slen; 01288 /* Pop function and channel. */ 01289 fn = top; 01290 if (--sp >= 0) 01291 top = stack[sp]; 01292 chan = top; 01293 if (--sp >= 0) 01294 top = stack[sp]; 01295 /* Pop arguments into a dedicated array. */ 01296 for (i = 0; i < nargs; ++i) 01297 { 01298 args[i] = top; 01299 if (--sp >= 0) 01300 top = stack[sp]; 01301 } 01302 01303 /* A bad format string means something is very wrong; give 01304 up immediately. */ 01305 if (format[slen - 1] != '\0') 01306 error (_("Unterminated format string in printf bytecode")); 01307 01308 ax_printf (fn, chan, format, nargs, args); 01309 } 01310 break; 01311 01312 /* GDB never (currently) generates any of these ops. */ 01313 case gdb_agent_op_float: 01314 case gdb_agent_op_ref_float: 01315 case gdb_agent_op_ref_double: 01316 case gdb_agent_op_ref_long_double: 01317 case gdb_agent_op_l_to_d: 01318 case gdb_agent_op_d_to_l: 01319 case gdb_agent_op_trace16: 01320 ax_debug ("Agent expression op 0x%x valid, but not handled", 01321 op); 01322 /* If ever GDB generates any of these, we don't have the 01323 option of ignoring. */ 01324 return 1; 01325 01326 default: 01327 ax_debug ("Agent expression op 0x%x not recognized", op); 01328 /* Don't struggle on, things will just get worse. */ 01329 return expr_eval_unrecognized_opcode; 01330 } 01331 01332 /* Check for stack badness. */ 01333 if (sp >= (STACK_MAX - 1)) 01334 { 01335 ax_debug ("Expression stack overflow"); 01336 return expr_eval_stack_overflow; 01337 } 01338 01339 if (sp < 0) 01340 { 01341 ax_debug ("Expression stack underflow"); 01342 return expr_eval_stack_underflow; 01343 } 01344 01345 ax_debug ("Op %s -> sp=%d, top=0x%s", 01346 gdb_agent_op_name (op), sp, phex_nz (top, 0)); 01347 } 01348 }