| 1 | /* SystemTap probe support for GDB. |
| 2 | |
| 3 | Copyright (C) 2012-2013 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "stap-probe.h" |
| 22 | #include "probe.h" |
| 23 | #include "vec.h" |
| 24 | #include "ui-out.h" |
| 25 | #include "objfiles.h" |
| 26 | #include "arch-utils.h" |
| 27 | #include "command.h" |
| 28 | #include "gdbcmd.h" |
| 29 | #include "filenames.h" |
| 30 | #include "value.h" |
| 31 | #include "exceptions.h" |
| 32 | #include "ax.h" |
| 33 | #include "ax-gdb.h" |
| 34 | #include "complaints.h" |
| 35 | #include "cli/cli-utils.h" |
| 36 | #include "linespec.h" |
| 37 | #include "user-regs.h" |
| 38 | #include "parser-defs.h" |
| 39 | #include "language.h" |
| 40 | #include "elf-bfd.h" |
| 41 | |
| 42 | #include <ctype.h> |
| 43 | |
| 44 | /* The name of the SystemTap section where we will find information about |
| 45 | the probes. */ |
| 46 | |
| 47 | #define STAP_BASE_SECTION_NAME ".stapsdt.base" |
| 48 | |
| 49 | /* Forward declaration. */ |
| 50 | |
| 51 | static const struct probe_ops stap_probe_ops; |
| 52 | |
| 53 | /* Should we display debug information for the probe's argument expression |
| 54 | parsing? */ |
| 55 | |
| 56 | static unsigned int stap_expression_debug = 0; |
| 57 | |
| 58 | /* The various possibilities of bitness defined for a probe's argument. |
| 59 | |
| 60 | The relationship is: |
| 61 | |
| 62 | - STAP_ARG_BITNESS_UNDEFINED: The user hasn't specified the bitness. |
| 63 | - STAP_ARG_BITNESS_32BIT_UNSIGNED: argument string starts with `4@'. |
| 64 | - STAP_ARG_BITNESS_32BIT_SIGNED: argument string starts with `-4@'. |
| 65 | - STAP_ARG_BITNESS_64BIT_UNSIGNED: argument string starts with `8@'. |
| 66 | - STAP_ARG_BITNESS_64BIT_SIGNED: argument string starts with `-8@'. */ |
| 67 | |
| 68 | enum stap_arg_bitness |
| 69 | { |
| 70 | STAP_ARG_BITNESS_UNDEFINED, |
| 71 | STAP_ARG_BITNESS_32BIT_UNSIGNED, |
| 72 | STAP_ARG_BITNESS_32BIT_SIGNED, |
| 73 | STAP_ARG_BITNESS_64BIT_UNSIGNED, |
| 74 | STAP_ARG_BITNESS_64BIT_SIGNED, |
| 75 | }; |
| 76 | |
| 77 | /* The following structure represents a single argument for the probe. */ |
| 78 | |
| 79 | struct stap_probe_arg |
| 80 | { |
| 81 | /* The bitness of this argument. */ |
| 82 | enum stap_arg_bitness bitness; |
| 83 | |
| 84 | /* The corresponding `struct type *' to the bitness. */ |
| 85 | struct type *atype; |
| 86 | |
| 87 | /* The argument converted to an internal GDB expression. */ |
| 88 | struct expression *aexpr; |
| 89 | }; |
| 90 | |
| 91 | typedef struct stap_probe_arg stap_probe_arg_s; |
| 92 | DEF_VEC_O (stap_probe_arg_s); |
| 93 | |
| 94 | struct stap_probe |
| 95 | { |
| 96 | /* Generic information about the probe. This shall be the first element |
| 97 | of this struct, in order to maintain binary compatibility with the |
| 98 | `struct probe' and be able to fully abstract it. */ |
| 99 | struct probe p; |
| 100 | |
| 101 | /* If the probe has a semaphore associated, then this is the value of |
| 102 | it. */ |
| 103 | CORE_ADDR sem_addr; |
| 104 | |
| 105 | unsigned int args_parsed : 1; |
| 106 | union |
| 107 | { |
| 108 | const char *text; |
| 109 | |
| 110 | /* Information about each argument. This is an array of `stap_probe_arg', |
| 111 | with each entry representing one argument. */ |
| 112 | VEC (stap_probe_arg_s) *vec; |
| 113 | } |
| 114 | args_u; |
| 115 | }; |
| 116 | |
| 117 | /* When parsing the arguments, we have to establish different precedences |
| 118 | for the various kinds of asm operators. This enumeration represents those |
| 119 | precedences. |
| 120 | |
| 121 | This logic behind this is available at |
| 122 | <http://sourceware.org/binutils/docs/as/Infix-Ops.html#Infix-Ops>, or using |
| 123 | the command "info '(as)Infix Ops'". */ |
| 124 | |
| 125 | enum stap_operand_prec |
| 126 | { |
| 127 | /* Lowest precedence, used for non-recognized operands or for the beginning |
| 128 | of the parsing process. */ |
| 129 | STAP_OPERAND_PREC_NONE = 0, |
| 130 | |
| 131 | /* Precedence of logical OR. */ |
| 132 | STAP_OPERAND_PREC_LOGICAL_OR, |
| 133 | |
| 134 | /* Precedence of logical AND. */ |
| 135 | STAP_OPERAND_PREC_LOGICAL_AND, |
| 136 | |
| 137 | /* Precedence of additive (plus, minus) and comparative (equal, less, |
| 138 | greater-than, etc) operands. */ |
| 139 | STAP_OPERAND_PREC_ADD_CMP, |
| 140 | |
| 141 | /* Precedence of bitwise operands (bitwise OR, XOR, bitwise AND, |
| 142 | logical NOT). */ |
| 143 | STAP_OPERAND_PREC_BITWISE, |
| 144 | |
| 145 | /* Precedence of multiplicative operands (multiplication, division, |
| 146 | remainder, left shift and right shift). */ |
| 147 | STAP_OPERAND_PREC_MUL |
| 148 | }; |
| 149 | |
| 150 | static void stap_parse_argument_1 (struct stap_parse_info *p, int has_lhs, |
| 151 | enum stap_operand_prec prec); |
| 152 | |
| 153 | static void stap_parse_argument_conditionally (struct stap_parse_info *p); |
| 154 | |
| 155 | /* Returns 1 if *S is an operator, zero otherwise. */ |
| 156 | |
| 157 | static int stap_is_operator (const char *op); |
| 158 | |
| 159 | static void |
| 160 | show_stapexpressiondebug (struct ui_file *file, int from_tty, |
| 161 | struct cmd_list_element *c, const char *value) |
| 162 | { |
| 163 | fprintf_filtered (file, _("SystemTap Probe expression debugging is %s.\n"), |
| 164 | value); |
| 165 | } |
| 166 | |
| 167 | /* Returns the operator precedence level of OP, or STAP_OPERAND_PREC_NONE |
| 168 | if the operator code was not recognized. */ |
| 169 | |
| 170 | static enum stap_operand_prec |
| 171 | stap_get_operator_prec (enum exp_opcode op) |
| 172 | { |
| 173 | switch (op) |
| 174 | { |
| 175 | case BINOP_LOGICAL_OR: |
| 176 | return STAP_OPERAND_PREC_LOGICAL_OR; |
| 177 | |
| 178 | case BINOP_LOGICAL_AND: |
| 179 | return STAP_OPERAND_PREC_LOGICAL_AND; |
| 180 | |
| 181 | case BINOP_ADD: |
| 182 | case BINOP_SUB: |
| 183 | case BINOP_EQUAL: |
| 184 | case BINOP_NOTEQUAL: |
| 185 | case BINOP_LESS: |
| 186 | case BINOP_LEQ: |
| 187 | case BINOP_GTR: |
| 188 | case BINOP_GEQ: |
| 189 | return STAP_OPERAND_PREC_ADD_CMP; |
| 190 | |
| 191 | case BINOP_BITWISE_IOR: |
| 192 | case BINOP_BITWISE_AND: |
| 193 | case BINOP_BITWISE_XOR: |
| 194 | case UNOP_LOGICAL_NOT: |
| 195 | return STAP_OPERAND_PREC_BITWISE; |
| 196 | |
| 197 | case BINOP_MUL: |
| 198 | case BINOP_DIV: |
| 199 | case BINOP_REM: |
| 200 | case BINOP_LSH: |
| 201 | case BINOP_RSH: |
| 202 | return STAP_OPERAND_PREC_MUL; |
| 203 | |
| 204 | default: |
| 205 | return STAP_OPERAND_PREC_NONE; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* Given S, read the operator in it and fills the OP pointer with its code. |
| 210 | Return 1 on success, zero if the operator was not recognized. */ |
| 211 | |
| 212 | static enum exp_opcode |
| 213 | stap_get_opcode (const char **s) |
| 214 | { |
| 215 | const char c = **s; |
| 216 | enum exp_opcode op; |
| 217 | |
| 218 | *s += 1; |
| 219 | |
| 220 | switch (c) |
| 221 | { |
| 222 | case '*': |
| 223 | op = BINOP_MUL; |
| 224 | break; |
| 225 | |
| 226 | case '/': |
| 227 | op = BINOP_DIV; |
| 228 | break; |
| 229 | |
| 230 | case '%': |
| 231 | op = BINOP_REM; |
| 232 | break; |
| 233 | |
| 234 | case '<': |
| 235 | op = BINOP_LESS; |
| 236 | if (**s == '<') |
| 237 | { |
| 238 | *s += 1; |
| 239 | op = BINOP_LSH; |
| 240 | } |
| 241 | else if (**s == '=') |
| 242 | { |
| 243 | *s += 1; |
| 244 | op = BINOP_LEQ; |
| 245 | } |
| 246 | else if (**s == '>') |
| 247 | { |
| 248 | *s += 1; |
| 249 | op = BINOP_NOTEQUAL; |
| 250 | } |
| 251 | break; |
| 252 | |
| 253 | case '>': |
| 254 | op = BINOP_GTR; |
| 255 | if (**s == '>') |
| 256 | { |
| 257 | *s += 1; |
| 258 | op = BINOP_RSH; |
| 259 | } |
| 260 | else if (**s == '=') |
| 261 | { |
| 262 | *s += 1; |
| 263 | op = BINOP_GEQ; |
| 264 | } |
| 265 | break; |
| 266 | |
| 267 | case '|': |
| 268 | op = BINOP_BITWISE_IOR; |
| 269 | if (**s == '|') |
| 270 | { |
| 271 | *s += 1; |
| 272 | op = BINOP_LOGICAL_OR; |
| 273 | } |
| 274 | break; |
| 275 | |
| 276 | case '&': |
| 277 | op = BINOP_BITWISE_AND; |
| 278 | if (**s == '&') |
| 279 | { |
| 280 | *s += 1; |
| 281 | op = BINOP_LOGICAL_AND; |
| 282 | } |
| 283 | break; |
| 284 | |
| 285 | case '^': |
| 286 | op = BINOP_BITWISE_XOR; |
| 287 | break; |
| 288 | |
| 289 | case '!': |
| 290 | op = UNOP_LOGICAL_NOT; |
| 291 | break; |
| 292 | |
| 293 | case '+': |
| 294 | op = BINOP_ADD; |
| 295 | break; |
| 296 | |
| 297 | case '-': |
| 298 | op = BINOP_SUB; |
| 299 | break; |
| 300 | |
| 301 | case '=': |
| 302 | gdb_assert (**s == '='); |
| 303 | op = BINOP_EQUAL; |
| 304 | break; |
| 305 | |
| 306 | default: |
| 307 | internal_error (__FILE__, __LINE__, |
| 308 | _("Invalid opcode in expression `%s' for SystemTap" |
| 309 | "probe"), *s); |
| 310 | } |
| 311 | |
| 312 | return op; |
| 313 | } |
| 314 | |
| 315 | /* Given the bitness of the argument, represented by B, return the |
| 316 | corresponding `struct type *'. */ |
| 317 | |
| 318 | static struct type * |
| 319 | stap_get_expected_argument_type (struct gdbarch *gdbarch, |
| 320 | enum stap_arg_bitness b) |
| 321 | { |
| 322 | switch (b) |
| 323 | { |
| 324 | case STAP_ARG_BITNESS_UNDEFINED: |
| 325 | if (gdbarch_addr_bit (gdbarch) == 32) |
| 326 | return builtin_type (gdbarch)->builtin_uint32; |
| 327 | else |
| 328 | return builtin_type (gdbarch)->builtin_uint64; |
| 329 | |
| 330 | case STAP_ARG_BITNESS_32BIT_SIGNED: |
| 331 | return builtin_type (gdbarch)->builtin_int32; |
| 332 | |
| 333 | case STAP_ARG_BITNESS_32BIT_UNSIGNED: |
| 334 | return builtin_type (gdbarch)->builtin_uint32; |
| 335 | |
| 336 | case STAP_ARG_BITNESS_64BIT_SIGNED: |
| 337 | return builtin_type (gdbarch)->builtin_int64; |
| 338 | |
| 339 | case STAP_ARG_BITNESS_64BIT_UNSIGNED: |
| 340 | return builtin_type (gdbarch)->builtin_uint64; |
| 341 | |
| 342 | default: |
| 343 | internal_error (__FILE__, __LINE__, |
| 344 | _("Undefined bitness for probe.")); |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /* Function responsible for parsing a register operand according to |
| 350 | SystemTap parlance. Assuming: |
| 351 | |
| 352 | RP = register prefix |
| 353 | RS = register suffix |
| 354 | RIP = register indirection prefix |
| 355 | RIS = register indirection suffix |
| 356 | |
| 357 | Then a register operand can be: |
| 358 | |
| 359 | [RIP] [RP] REGISTER [RS] [RIS] |
| 360 | |
| 361 | This function takes care of a register's indirection, displacement and |
| 362 | direct access. It also takes into consideration the fact that some |
| 363 | registers are named differently inside and outside GDB, e.g., PPC's |
| 364 | general-purpose registers are represented by integers in the assembly |
| 365 | language (e.g., `15' is the 15th general-purpose register), but inside |
| 366 | GDB they have a prefix (the letter `r') appended. */ |
| 367 | |
| 368 | static void |
| 369 | stap_parse_register_operand (struct stap_parse_info *p) |
| 370 | { |
| 371 | /* Simple flag to indicate whether we have seen a minus signal before |
| 372 | certain number. */ |
| 373 | int got_minus = 0; |
| 374 | |
| 375 | /* Flags to indicate whether this register access is being displaced and/or |
| 376 | indirected. */ |
| 377 | int disp_p = 0, indirect_p = 0; |
| 378 | struct gdbarch *gdbarch = p->gdbarch; |
| 379 | |
| 380 | /* Needed to generate the register name as a part of an expression. */ |
| 381 | struct stoken str; |
| 382 | |
| 383 | /* Variables used to extract the register name from the probe's |
| 384 | argument. */ |
| 385 | const char *start; |
| 386 | char *regname; |
| 387 | int len; |
| 388 | |
| 389 | /* Prefixes for the parser. */ |
| 390 | const char *reg_prefix = gdbarch_stap_register_prefix (gdbarch); |
| 391 | const char *reg_ind_prefix |
| 392 | = gdbarch_stap_register_indirection_prefix (gdbarch); |
| 393 | const char *gdb_reg_prefix = gdbarch_stap_gdb_register_prefix (gdbarch); |
| 394 | int reg_prefix_len = reg_prefix ? strlen (reg_prefix) : 0; |
| 395 | int reg_ind_prefix_len = reg_ind_prefix ? strlen (reg_ind_prefix) : 0; |
| 396 | int gdb_reg_prefix_len = gdb_reg_prefix ? strlen (gdb_reg_prefix) : 0; |
| 397 | |
| 398 | /* Suffixes for the parser. */ |
| 399 | const char *reg_suffix = gdbarch_stap_register_suffix (gdbarch); |
| 400 | const char *reg_ind_suffix |
| 401 | = gdbarch_stap_register_indirection_suffix (gdbarch); |
| 402 | const char *gdb_reg_suffix = gdbarch_stap_gdb_register_suffix (gdbarch); |
| 403 | int reg_suffix_len = reg_suffix ? strlen (reg_suffix) : 0; |
| 404 | int reg_ind_suffix_len = reg_ind_suffix ? strlen (reg_ind_suffix) : 0; |
| 405 | int gdb_reg_suffix_len = gdb_reg_suffix ? strlen (gdb_reg_suffix) : 0; |
| 406 | |
| 407 | /* Checking for a displacement argument. */ |
| 408 | if (*p->arg == '+') |
| 409 | { |
| 410 | /* If it's a plus sign, we don't need to do anything, just advance the |
| 411 | pointer. */ |
| 412 | ++p->arg; |
| 413 | } |
| 414 | |
| 415 | if (*p->arg == '-') |
| 416 | { |
| 417 | got_minus = 1; |
| 418 | ++p->arg; |
| 419 | } |
| 420 | |
| 421 | if (isdigit (*p->arg)) |
| 422 | { |
| 423 | /* The value of the displacement. */ |
| 424 | long displacement; |
| 425 | char *endp; |
| 426 | |
| 427 | disp_p = 1; |
| 428 | displacement = strtol (p->arg, &endp, 10); |
| 429 | p->arg = endp; |
| 430 | |
| 431 | /* Generating the expression for the displacement. */ |
| 432 | write_exp_elt_opcode (OP_LONG); |
| 433 | write_exp_elt_type (builtin_type (gdbarch)->builtin_long); |
| 434 | write_exp_elt_longcst (displacement); |
| 435 | write_exp_elt_opcode (OP_LONG); |
| 436 | if (got_minus) |
| 437 | write_exp_elt_opcode (UNOP_NEG); |
| 438 | } |
| 439 | |
| 440 | /* Getting rid of register indirection prefix. */ |
| 441 | if (reg_ind_prefix |
| 442 | && strncmp (p->arg, reg_ind_prefix, reg_ind_prefix_len) == 0) |
| 443 | { |
| 444 | indirect_p = 1; |
| 445 | p->arg += reg_ind_prefix_len; |
| 446 | } |
| 447 | |
| 448 | if (disp_p && !indirect_p) |
| 449 | error (_("Invalid register displacement syntax on expression `%s'."), |
| 450 | p->saved_arg); |
| 451 | |
| 452 | /* Getting rid of register prefix. */ |
| 453 | if (reg_prefix && strncmp (p->arg, reg_prefix, reg_prefix_len) == 0) |
| 454 | p->arg += reg_prefix_len; |
| 455 | |
| 456 | /* Now we should have only the register name. Let's extract it and get |
| 457 | the associated number. */ |
| 458 | start = p->arg; |
| 459 | |
| 460 | /* We assume the register name is composed by letters and numbers. */ |
| 461 | while (isalnum (*p->arg)) |
| 462 | ++p->arg; |
| 463 | |
| 464 | len = p->arg - start; |
| 465 | |
| 466 | regname = alloca (len + gdb_reg_prefix_len + gdb_reg_suffix_len + 1); |
| 467 | regname[0] = '\0'; |
| 468 | |
| 469 | /* We only add the GDB's register prefix/suffix if we are dealing with |
| 470 | a numeric register. */ |
| 471 | if (gdb_reg_prefix && isdigit (*start)) |
| 472 | { |
| 473 | strncpy (regname, gdb_reg_prefix, gdb_reg_prefix_len); |
| 474 | strncpy (regname + gdb_reg_prefix_len, start, len); |
| 475 | |
| 476 | if (gdb_reg_suffix) |
| 477 | strncpy (regname + gdb_reg_prefix_len + len, |
| 478 | gdb_reg_suffix, gdb_reg_suffix_len); |
| 479 | |
| 480 | len += gdb_reg_prefix_len + gdb_reg_suffix_len; |
| 481 | } |
| 482 | else |
| 483 | strncpy (regname, start, len); |
| 484 | |
| 485 | regname[len] = '\0'; |
| 486 | |
| 487 | /* Is this a valid register name? */ |
| 488 | if (user_reg_map_name_to_regnum (gdbarch, regname, len) == -1) |
| 489 | error (_("Invalid register name `%s' on expression `%s'."), |
| 490 | regname, p->saved_arg); |
| 491 | |
| 492 | write_exp_elt_opcode (OP_REGISTER); |
| 493 | str.ptr = regname; |
| 494 | str.length = len; |
| 495 | write_exp_string (str); |
| 496 | write_exp_elt_opcode (OP_REGISTER); |
| 497 | |
| 498 | if (indirect_p) |
| 499 | { |
| 500 | if (disp_p) |
| 501 | write_exp_elt_opcode (BINOP_ADD); |
| 502 | |
| 503 | /* Casting to the expected type. */ |
| 504 | write_exp_elt_opcode (UNOP_CAST); |
| 505 | write_exp_elt_type (lookup_pointer_type (p->arg_type)); |
| 506 | write_exp_elt_opcode (UNOP_CAST); |
| 507 | |
| 508 | write_exp_elt_opcode (UNOP_IND); |
| 509 | } |
| 510 | |
| 511 | /* Getting rid of the register name suffix. */ |
| 512 | if (reg_suffix) |
| 513 | { |
| 514 | if (strncmp (p->arg, reg_suffix, reg_suffix_len) != 0) |
| 515 | error (_("Missing register name suffix `%s' on expression `%s'."), |
| 516 | reg_suffix, p->saved_arg); |
| 517 | |
| 518 | p->arg += reg_suffix_len; |
| 519 | } |
| 520 | |
| 521 | /* Getting rid of the register indirection suffix. */ |
| 522 | if (indirect_p && reg_ind_suffix) |
| 523 | { |
| 524 | if (strncmp (p->arg, reg_ind_suffix, reg_ind_suffix_len) != 0) |
| 525 | error (_("Missing indirection suffix `%s' on expression `%s'."), |
| 526 | reg_ind_suffix, p->saved_arg); |
| 527 | |
| 528 | p->arg += reg_ind_suffix_len; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /* This function is responsible for parsing a single operand. |
| 533 | |
| 534 | A single operand can be: |
| 535 | |
| 536 | - an unary operation (e.g., `-5', `~2', or even with subexpressions |
| 537 | like `-(2 + 1)') |
| 538 | - a register displacement, which will be treated as a register |
| 539 | operand (e.g., `-4(%eax)' on x86) |
| 540 | - a numeric constant, or |
| 541 | - a register operand (see function `stap_parse_register_operand') |
| 542 | |
| 543 | The function also calls special-handling functions to deal with |
| 544 | unrecognized operands, allowing arch-specific parsers to be |
| 545 | created. */ |
| 546 | |
| 547 | static void |
| 548 | stap_parse_single_operand (struct stap_parse_info *p) |
| 549 | { |
| 550 | struct gdbarch *gdbarch = p->gdbarch; |
| 551 | |
| 552 | /* Prefixes for the parser. */ |
| 553 | const char *const_prefix = gdbarch_stap_integer_prefix (gdbarch); |
| 554 | const char *reg_prefix = gdbarch_stap_register_prefix (gdbarch); |
| 555 | const char *reg_ind_prefix |
| 556 | = gdbarch_stap_register_indirection_prefix (gdbarch); |
| 557 | int const_prefix_len = const_prefix ? strlen (const_prefix) : 0; |
| 558 | int reg_prefix_len = reg_prefix ? strlen (reg_prefix) : 0; |
| 559 | int reg_ind_prefix_len = reg_ind_prefix ? strlen (reg_ind_prefix) : 0; |
| 560 | |
| 561 | /* Suffixes for the parser. */ |
| 562 | const char *const_suffix = gdbarch_stap_integer_suffix (gdbarch); |
| 563 | int const_suffix_len = const_suffix ? strlen (const_suffix) : 0; |
| 564 | |
| 565 | /* We first try to parse this token as a "special token". */ |
| 566 | if (gdbarch_stap_parse_special_token_p (gdbarch)) |
| 567 | { |
| 568 | int ret = gdbarch_stap_parse_special_token (gdbarch, p); |
| 569 | |
| 570 | if (ret) |
| 571 | { |
| 572 | /* If the return value of the above function is not zero, |
| 573 | it means it successfully parsed the special token. |
| 574 | |
| 575 | If it is NULL, we try to parse it using our method. */ |
| 576 | return; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+') |
| 581 | { |
| 582 | char c = *p->arg; |
| 583 | int number; |
| 584 | |
| 585 | /* We use this variable to do a lookahead. */ |
| 586 | const char *tmp = p->arg; |
| 587 | |
| 588 | ++tmp; |
| 589 | |
| 590 | /* This is an unary operation. Here is a list of allowed tokens |
| 591 | here: |
| 592 | |
| 593 | - numeric literal; |
| 594 | - number (from register displacement) |
| 595 | - subexpression (beginning with `(') |
| 596 | |
| 597 | We handle the register displacement here, and the other cases |
| 598 | recursively. */ |
| 599 | if (p->inside_paren_p) |
| 600 | tmp = skip_spaces_const (tmp); |
| 601 | |
| 602 | if (isdigit (*tmp)) |
| 603 | { |
| 604 | char *endp; |
| 605 | |
| 606 | number = strtol (tmp, &endp, 10); |
| 607 | tmp = endp; |
| 608 | } |
| 609 | |
| 610 | if (!reg_ind_prefix |
| 611 | || strncmp (tmp, reg_ind_prefix, reg_ind_prefix_len) != 0) |
| 612 | { |
| 613 | /* This is not a displacement. We skip the operator, and deal |
| 614 | with it later. */ |
| 615 | ++p->arg; |
| 616 | stap_parse_argument_conditionally (p); |
| 617 | if (c == '-') |
| 618 | write_exp_elt_opcode (UNOP_NEG); |
| 619 | else if (c == '~') |
| 620 | write_exp_elt_opcode (UNOP_COMPLEMENT); |
| 621 | } |
| 622 | else |
| 623 | { |
| 624 | /* If we are here, it means it is a displacement. The only |
| 625 | operations allowed here are `-' and `+'. */ |
| 626 | if (c == '~') |
| 627 | error (_("Invalid operator `%c' for register displacement " |
| 628 | "on expression `%s'."), c, p->saved_arg); |
| 629 | |
| 630 | stap_parse_register_operand (p); |
| 631 | } |
| 632 | } |
| 633 | else if (isdigit (*p->arg)) |
| 634 | { |
| 635 | /* A temporary variable, needed for lookahead. */ |
| 636 | const char *tmp = p->arg; |
| 637 | char *endp; |
| 638 | long number; |
| 639 | |
| 640 | /* We can be dealing with a numeric constant (if `const_prefix' is |
| 641 | NULL), or with a register displacement. */ |
| 642 | number = strtol (tmp, &endp, 10); |
| 643 | tmp = endp; |
| 644 | |
| 645 | if (p->inside_paren_p) |
| 646 | tmp = skip_spaces_const (tmp); |
| 647 | if (!const_prefix && reg_ind_prefix |
| 648 | && strncmp (tmp, reg_ind_prefix, reg_ind_prefix_len) != 0) |
| 649 | { |
| 650 | /* We are dealing with a numeric constant. */ |
| 651 | write_exp_elt_opcode (OP_LONG); |
| 652 | write_exp_elt_type (builtin_type (gdbarch)->builtin_long); |
| 653 | write_exp_elt_longcst (number); |
| 654 | write_exp_elt_opcode (OP_LONG); |
| 655 | |
| 656 | p->arg = tmp; |
| 657 | |
| 658 | if (const_suffix) |
| 659 | { |
| 660 | if (strncmp (p->arg, const_suffix, const_suffix_len) == 0) |
| 661 | p->arg += const_suffix_len; |
| 662 | else |
| 663 | error (_("Invalid constant suffix on expression `%s'."), |
| 664 | p->saved_arg); |
| 665 | } |
| 666 | } |
| 667 | else if (reg_ind_prefix |
| 668 | && strncmp (tmp, reg_ind_prefix, reg_ind_prefix_len) == 0) |
| 669 | stap_parse_register_operand (p); |
| 670 | else |
| 671 | error (_("Unknown numeric token on expression `%s'."), |
| 672 | p->saved_arg); |
| 673 | } |
| 674 | else if (const_prefix |
| 675 | && strncmp (p->arg, const_prefix, const_prefix_len) == 0) |
| 676 | { |
| 677 | /* We are dealing with a numeric constant. */ |
| 678 | long number; |
| 679 | char *endp; |
| 680 | |
| 681 | p->arg += const_prefix_len; |
| 682 | number = strtol (p->arg, &endp, 10); |
| 683 | p->arg = endp; |
| 684 | |
| 685 | write_exp_elt_opcode (OP_LONG); |
| 686 | write_exp_elt_type (builtin_type (gdbarch)->builtin_long); |
| 687 | write_exp_elt_longcst (number); |
| 688 | write_exp_elt_opcode (OP_LONG); |
| 689 | |
| 690 | if (const_suffix) |
| 691 | { |
| 692 | if (strncmp (p->arg, const_suffix, const_suffix_len) == 0) |
| 693 | p->arg += const_suffix_len; |
| 694 | else |
| 695 | error (_("Invalid constant suffix on expression `%s'."), |
| 696 | p->saved_arg); |
| 697 | } |
| 698 | } |
| 699 | else if ((reg_prefix |
| 700 | && strncmp (p->arg, reg_prefix, reg_prefix_len) == 0) |
| 701 | || (reg_ind_prefix |
| 702 | && strncmp (p->arg, reg_ind_prefix, reg_ind_prefix_len) == 0)) |
| 703 | stap_parse_register_operand (p); |
| 704 | else |
| 705 | error (_("Operator `%c' not recognized on expression `%s'."), |
| 706 | *p->arg, p->saved_arg); |
| 707 | } |
| 708 | |
| 709 | /* This function parses an argument conditionally, based on single or |
| 710 | non-single operands. A non-single operand would be a parenthesized |
| 711 | expression (e.g., `(2 + 1)'), and a single operand is anything that |
| 712 | starts with `-', `~', `+' (i.e., unary operators), a digit, or |
| 713 | something recognized by `gdbarch_stap_is_single_operand'. */ |
| 714 | |
| 715 | static void |
| 716 | stap_parse_argument_conditionally (struct stap_parse_info *p) |
| 717 | { |
| 718 | if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' /* Unary. */ |
| 719 | || isdigit (*p->arg) |
| 720 | || gdbarch_stap_is_single_operand (p->gdbarch, p->arg)) |
| 721 | stap_parse_single_operand (p); |
| 722 | else if (*p->arg == '(') |
| 723 | { |
| 724 | /* We are dealing with a parenthesized operand. It means we |
| 725 | have to parse it as it was a separate expression, without |
| 726 | left-side or precedence. */ |
| 727 | ++p->arg; |
| 728 | p->arg = skip_spaces_const (p->arg); |
| 729 | ++p->inside_paren_p; |
| 730 | |
| 731 | stap_parse_argument_1 (p, 0, STAP_OPERAND_PREC_NONE); |
| 732 | |
| 733 | --p->inside_paren_p; |
| 734 | if (*p->arg != ')') |
| 735 | error (_("Missign close-paren on expression `%s'."), |
| 736 | p->saved_arg); |
| 737 | |
| 738 | ++p->arg; |
| 739 | if (p->inside_paren_p) |
| 740 | p->arg = skip_spaces_const (p->arg); |
| 741 | } |
| 742 | else |
| 743 | error (_("Cannot parse expression `%s'."), p->saved_arg); |
| 744 | } |
| 745 | |
| 746 | /* Helper function for `stap_parse_argument'. Please, see its comments to |
| 747 | better understand what this function does. */ |
| 748 | |
| 749 | static void |
| 750 | stap_parse_argument_1 (struct stap_parse_info *p, int has_lhs, |
| 751 | enum stap_operand_prec prec) |
| 752 | { |
| 753 | /* This is an operator-precedence parser. |
| 754 | |
| 755 | We work with left- and right-sides of expressions, and |
| 756 | parse them depending on the precedence of the operators |
| 757 | we find. */ |
| 758 | |
| 759 | if (p->inside_paren_p) |
| 760 | p->arg = skip_spaces_const (p->arg); |
| 761 | |
| 762 | if (!has_lhs) |
| 763 | { |
| 764 | /* We were called without a left-side, either because this is the |
| 765 | first call, or because we were called to parse a parenthesized |
| 766 | expression. It doesn't really matter; we have to parse the |
| 767 | left-side in order to continue the process. */ |
| 768 | stap_parse_argument_conditionally (p); |
| 769 | } |
| 770 | |
| 771 | /* Start to parse the right-side, and to "join" left and right sides |
| 772 | depending on the operation specified. |
| 773 | |
| 774 | This loop shall continue until we run out of characters in the input, |
| 775 | or until we find a close-parenthesis, which means that we've reached |
| 776 | the end of a sub-expression. */ |
| 777 | while (p->arg && *p->arg && *p->arg != ')' && !isspace (*p->arg)) |
| 778 | { |
| 779 | const char *tmp_exp_buf; |
| 780 | enum exp_opcode opcode; |
| 781 | enum stap_operand_prec cur_prec; |
| 782 | |
| 783 | if (!stap_is_operator (p->arg)) |
| 784 | error (_("Invalid operator `%c' on expression `%s'."), *p->arg, |
| 785 | p->saved_arg); |
| 786 | |
| 787 | /* We have to save the current value of the expression buffer because |
| 788 | the `stap_get_opcode' modifies it in order to get the current |
| 789 | operator. If this operator's precedence is lower than PREC, we |
| 790 | should return and not advance the expression buffer pointer. */ |
| 791 | tmp_exp_buf = p->arg; |
| 792 | opcode = stap_get_opcode (&tmp_exp_buf); |
| 793 | |
| 794 | cur_prec = stap_get_operator_prec (opcode); |
| 795 | if (cur_prec < prec) |
| 796 | { |
| 797 | /* If the precedence of the operator that we are seeing now is |
| 798 | lower than the precedence of the first operator seen before |
| 799 | this parsing process began, it means we should stop parsing |
| 800 | and return. */ |
| 801 | break; |
| 802 | } |
| 803 | |
| 804 | p->arg = tmp_exp_buf; |
| 805 | if (p->inside_paren_p) |
| 806 | p->arg = skip_spaces_const (p->arg); |
| 807 | |
| 808 | /* Parse the right-side of the expression. */ |
| 809 | stap_parse_argument_conditionally (p); |
| 810 | |
| 811 | /* While we still have operators, try to parse another |
| 812 | right-side, but using the current right-side as a left-side. */ |
| 813 | while (*p->arg && stap_is_operator (p->arg)) |
| 814 | { |
| 815 | enum exp_opcode lookahead_opcode; |
| 816 | enum stap_operand_prec lookahead_prec; |
| 817 | |
| 818 | /* Saving the current expression buffer position. The explanation |
| 819 | is the same as above. */ |
| 820 | tmp_exp_buf = p->arg; |
| 821 | lookahead_opcode = stap_get_opcode (&tmp_exp_buf); |
| 822 | lookahead_prec = stap_get_operator_prec (lookahead_opcode); |
| 823 | |
| 824 | if (lookahead_prec <= prec) |
| 825 | { |
| 826 | /* If we are dealing with an operator whose precedence is lower |
| 827 | than the first one, just abandon the attempt. */ |
| 828 | break; |
| 829 | } |
| 830 | |
| 831 | /* Parse the right-side of the expression, but since we already |
| 832 | have a left-side at this point, set `has_lhs' to 1. */ |
| 833 | stap_parse_argument_1 (p, 1, lookahead_prec); |
| 834 | } |
| 835 | |
| 836 | write_exp_elt_opcode (opcode); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /* Parse a probe's argument. |
| 841 | |
| 842 | Assuming that: |
| 843 | |
| 844 | LP = literal integer prefix |
| 845 | LS = literal integer suffix |
| 846 | |
| 847 | RP = register prefix |
| 848 | RS = register suffix |
| 849 | |
| 850 | RIP = register indirection prefix |
| 851 | RIS = register indirection suffix |
| 852 | |
| 853 | This routine assumes that arguments' tokens are of the form: |
| 854 | |
| 855 | - [LP] NUMBER [LS] |
| 856 | - [RP] REGISTER [RS] |
| 857 | - [RIP] [RP] REGISTER [RS] [RIS] |
| 858 | - If we find a number without LP, we try to parse it as a literal integer |
| 859 | constant (if LP == NULL), or as a register displacement. |
| 860 | - We count parenthesis, and only skip whitespaces if we are inside them. |
| 861 | - If we find an operator, we skip it. |
| 862 | |
| 863 | This function can also call a special function that will try to match |
| 864 | unknown tokens. It will return 1 if the argument has been parsed |
| 865 | successfully, or zero otherwise. */ |
| 866 | |
| 867 | static struct expression * |
| 868 | stap_parse_argument (const char **arg, struct type *atype, |
| 869 | struct gdbarch *gdbarch) |
| 870 | { |
| 871 | struct stap_parse_info p; |
| 872 | struct cleanup *back_to; |
| 873 | |
| 874 | /* We need to initialize the expression buffer, in order to begin |
| 875 | our parsing efforts. The language here does not matter, since we |
| 876 | are using our own parser. */ |
| 877 | initialize_expout (10, current_language, gdbarch); |
| 878 | back_to = make_cleanup (free_current_contents, &expout); |
| 879 | |
| 880 | p.saved_arg = *arg; |
| 881 | p.arg = *arg; |
| 882 | p.arg_type = atype; |
| 883 | p.gdbarch = gdbarch; |
| 884 | p.inside_paren_p = 0; |
| 885 | |
| 886 | stap_parse_argument_1 (&p, 0, STAP_OPERAND_PREC_NONE); |
| 887 | |
| 888 | discard_cleanups (back_to); |
| 889 | |
| 890 | gdb_assert (p.inside_paren_p == 0); |
| 891 | |
| 892 | /* Casting the final expression to the appropriate type. */ |
| 893 | write_exp_elt_opcode (UNOP_CAST); |
| 894 | write_exp_elt_type (atype); |
| 895 | write_exp_elt_opcode (UNOP_CAST); |
| 896 | |
| 897 | reallocate_expout (); |
| 898 | |
| 899 | p.arg = skip_spaces_const (p.arg); |
| 900 | *arg = p.arg; |
| 901 | |
| 902 | return expout; |
| 903 | } |
| 904 | |
| 905 | /* Function which parses an argument string from PROBE, correctly splitting |
| 906 | the arguments and storing their information in properly ways. |
| 907 | |
| 908 | Consider the following argument string (x86 syntax): |
| 909 | |
| 910 | `4@%eax 4@$10' |
| 911 | |
| 912 | We have two arguments, `%eax' and `$10', both with 32-bit unsigned bitness. |
| 913 | This function basically handles them, properly filling some structures with |
| 914 | this information. */ |
| 915 | |
| 916 | static void |
| 917 | stap_parse_probe_arguments (struct stap_probe *probe) |
| 918 | { |
| 919 | const char *cur; |
| 920 | struct gdbarch *gdbarch = get_objfile_arch (probe->p.objfile); |
| 921 | |
| 922 | gdb_assert (!probe->args_parsed); |
| 923 | cur = probe->args_u.text; |
| 924 | probe->args_parsed = 1; |
| 925 | probe->args_u.vec = NULL; |
| 926 | |
| 927 | if (!cur || !*cur || *cur == ':') |
| 928 | return; |
| 929 | |
| 930 | while (*cur) |
| 931 | { |
| 932 | struct stap_probe_arg arg; |
| 933 | enum stap_arg_bitness b; |
| 934 | int got_minus = 0; |
| 935 | struct expression *expr; |
| 936 | |
| 937 | memset (&arg, 0, sizeof (arg)); |
| 938 | |
| 939 | /* We expect to find something like: |
| 940 | |
| 941 | N@OP |
| 942 | |
| 943 | Where `N' can be [+,-][4,8]. This is not mandatory, so |
| 944 | we check it here. If we don't find it, go to the next |
| 945 | state. */ |
| 946 | if ((*cur == '-' && cur[1] && cur[2] != '@') |
| 947 | && cur[1] != '@') |
| 948 | arg.bitness = STAP_ARG_BITNESS_UNDEFINED; |
| 949 | else |
| 950 | { |
| 951 | if (*cur == '-') |
| 952 | { |
| 953 | /* Discard the `-'. */ |
| 954 | ++cur; |
| 955 | got_minus = 1; |
| 956 | } |
| 957 | |
| 958 | if (*cur == '4') |
| 959 | b = (got_minus ? STAP_ARG_BITNESS_32BIT_SIGNED |
| 960 | : STAP_ARG_BITNESS_32BIT_UNSIGNED); |
| 961 | else if (*cur == '8') |
| 962 | b = (got_minus ? STAP_ARG_BITNESS_64BIT_SIGNED |
| 963 | : STAP_ARG_BITNESS_64BIT_UNSIGNED); |
| 964 | else |
| 965 | { |
| 966 | /* We have an error, because we don't expect anything |
| 967 | except 4 and 8. */ |
| 968 | complaint (&symfile_complaints, |
| 969 | _("unrecognized bitness `%c' for probe `%s'"), |
| 970 | *cur, probe->p.name); |
| 971 | return; |
| 972 | } |
| 973 | |
| 974 | arg.bitness = b; |
| 975 | arg.atype = stap_get_expected_argument_type (gdbarch, b); |
| 976 | |
| 977 | /* Discard the number and the `@' sign. */ |
| 978 | cur += 2; |
| 979 | } |
| 980 | |
| 981 | expr = stap_parse_argument (&cur, arg.atype, gdbarch); |
| 982 | |
| 983 | if (stap_expression_debug) |
| 984 | dump_raw_expression (expr, gdb_stdlog, |
| 985 | "before conversion to prefix form"); |
| 986 | |
| 987 | prefixify_expression (expr); |
| 988 | |
| 989 | if (stap_expression_debug) |
| 990 | dump_prefix_expression (expr, gdb_stdlog); |
| 991 | |
| 992 | arg.aexpr = expr; |
| 993 | |
| 994 | /* Start it over again. */ |
| 995 | cur = skip_spaces_const (cur); |
| 996 | |
| 997 | VEC_safe_push (stap_probe_arg_s, probe->args_u.vec, &arg); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | /* Given PROBE, returns the number of arguments present in that probe's |
| 1002 | argument string. */ |
| 1003 | |
| 1004 | static unsigned |
| 1005 | stap_get_probe_argument_count (struct probe *probe_generic) |
| 1006 | { |
| 1007 | struct stap_probe *probe = (struct stap_probe *) probe_generic; |
| 1008 | |
| 1009 | gdb_assert (probe_generic->pops == &stap_probe_ops); |
| 1010 | |
| 1011 | if (!probe->args_parsed) |
| 1012 | { |
| 1013 | if (probe_generic->pops->can_evaluate_probe_arguments (probe_generic)) |
| 1014 | stap_parse_probe_arguments (probe); |
| 1015 | else |
| 1016 | { |
| 1017 | static int have_warned_stap_incomplete = 0; |
| 1018 | |
| 1019 | if (!have_warned_stap_incomplete) |
| 1020 | { |
| 1021 | warning (_( |
| 1022 | "The SystemTap SDT probe support is not fully implemented on this target;\n" |
| 1023 | "you will not be able to inspect the arguments of the probes.\n" |
| 1024 | "Please report a bug against GDB requesting a port to this target.")); |
| 1025 | have_warned_stap_incomplete = 1; |
| 1026 | } |
| 1027 | |
| 1028 | /* Marking the arguments as "already parsed". */ |
| 1029 | probe->args_u.vec = NULL; |
| 1030 | probe->args_parsed = 1; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | gdb_assert (probe->args_parsed); |
| 1035 | return VEC_length (stap_probe_arg_s, probe->args_u.vec); |
| 1036 | } |
| 1037 | |
| 1038 | /* Return 1 if OP is a valid operator inside a probe argument, or zero |
| 1039 | otherwise. */ |
| 1040 | |
| 1041 | static int |
| 1042 | stap_is_operator (const char *op) |
| 1043 | { |
| 1044 | int ret = 1; |
| 1045 | |
| 1046 | switch (*op) |
| 1047 | { |
| 1048 | case '*': |
| 1049 | case '/': |
| 1050 | case '%': |
| 1051 | case '^': |
| 1052 | case '!': |
| 1053 | case '+': |
| 1054 | case '-': |
| 1055 | case '<': |
| 1056 | case '>': |
| 1057 | case '|': |
| 1058 | case '&': |
| 1059 | break; |
| 1060 | |
| 1061 | case '=': |
| 1062 | if (op[1] != '=') |
| 1063 | ret = 0; |
| 1064 | break; |
| 1065 | |
| 1066 | default: |
| 1067 | /* We didn't find any operator. */ |
| 1068 | ret = 0; |
| 1069 | } |
| 1070 | |
| 1071 | return ret; |
| 1072 | } |
| 1073 | |
| 1074 | static struct stap_probe_arg * |
| 1075 | stap_get_arg (struct stap_probe *probe, unsigned n) |
| 1076 | { |
| 1077 | if (!probe->args_parsed) |
| 1078 | stap_parse_probe_arguments (probe); |
| 1079 | |
| 1080 | return VEC_index (stap_probe_arg_s, probe->args_u.vec, n); |
| 1081 | } |
| 1082 | |
| 1083 | /* Implement the `can_evaluate_probe_arguments' method of probe_ops. */ |
| 1084 | |
| 1085 | static int |
| 1086 | stap_can_evaluate_probe_arguments (struct probe *probe_generic) |
| 1087 | { |
| 1088 | struct stap_probe *stap_probe = (struct stap_probe *) probe_generic; |
| 1089 | struct gdbarch *gdbarch = get_objfile_arch (stap_probe->p.objfile); |
| 1090 | |
| 1091 | /* For SystemTap probes, we have to guarantee that the method |
| 1092 | stap_is_single_operand is defined on gdbarch. If it is not, then it |
| 1093 | means that argument evaluation is not implemented on this target. */ |
| 1094 | return gdbarch_stap_is_single_operand_p (gdbarch); |
| 1095 | } |
| 1096 | |
| 1097 | /* Evaluate the probe's argument N (indexed from 0), returning a value |
| 1098 | corresponding to it. Assertion is thrown if N does not exist. */ |
| 1099 | |
| 1100 | static struct value * |
| 1101 | stap_evaluate_probe_argument (struct probe *probe_generic, unsigned n) |
| 1102 | { |
| 1103 | struct stap_probe *stap_probe = (struct stap_probe *) probe_generic; |
| 1104 | struct stap_probe_arg *arg; |
| 1105 | int pos = 0; |
| 1106 | |
| 1107 | gdb_assert (probe_generic->pops == &stap_probe_ops); |
| 1108 | |
| 1109 | arg = stap_get_arg (stap_probe, n); |
| 1110 | return evaluate_subexp_standard (arg->atype, arg->aexpr, &pos, EVAL_NORMAL); |
| 1111 | } |
| 1112 | |
| 1113 | /* Compile the probe's argument N (indexed from 0) to agent expression. |
| 1114 | Assertion is thrown if N does not exist. */ |
| 1115 | |
| 1116 | static void |
| 1117 | stap_compile_to_ax (struct probe *probe_generic, struct agent_expr *expr, |
| 1118 | struct axs_value *value, unsigned n) |
| 1119 | { |
| 1120 | struct stap_probe *stap_probe = (struct stap_probe *) probe_generic; |
| 1121 | struct stap_probe_arg *arg; |
| 1122 | union exp_element *pc; |
| 1123 | |
| 1124 | gdb_assert (probe_generic->pops == &stap_probe_ops); |
| 1125 | |
| 1126 | arg = stap_get_arg (stap_probe, n); |
| 1127 | |
| 1128 | pc = arg->aexpr->elts; |
| 1129 | gen_expr (arg->aexpr, &pc, expr, value); |
| 1130 | |
| 1131 | require_rvalue (expr, value); |
| 1132 | value->type = arg->atype; |
| 1133 | } |
| 1134 | |
| 1135 | /* Destroy (free) the data related to PROBE. PROBE memory itself is not feed |
| 1136 | as it is allocated from OBJFILE_OBSTACK. */ |
| 1137 | |
| 1138 | static void |
| 1139 | stap_probe_destroy (struct probe *probe_generic) |
| 1140 | { |
| 1141 | struct stap_probe *probe = (struct stap_probe *) probe_generic; |
| 1142 | |
| 1143 | gdb_assert (probe_generic->pops == &stap_probe_ops); |
| 1144 | |
| 1145 | if (probe->args_parsed) |
| 1146 | { |
| 1147 | struct stap_probe_arg *arg; |
| 1148 | int ix; |
| 1149 | |
| 1150 | for (ix = 0; VEC_iterate (stap_probe_arg_s, probe->args_u.vec, ix, arg); |
| 1151 | ++ix) |
| 1152 | xfree (arg->aexpr); |
| 1153 | VEC_free (stap_probe_arg_s, probe->args_u.vec); |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | \f |
| 1158 | |
| 1159 | /* This is called to compute the value of one of the $_probe_arg* |
| 1160 | convenience variables. */ |
| 1161 | |
| 1162 | static struct value * |
| 1163 | compute_probe_arg (struct gdbarch *arch, struct internalvar *ivar, |
| 1164 | void *data) |
| 1165 | { |
| 1166 | struct frame_info *frame = get_selected_frame (_("No frame selected")); |
| 1167 | CORE_ADDR pc = get_frame_pc (frame); |
| 1168 | int sel = (int) (uintptr_t) data; |
| 1169 | struct probe *pc_probe; |
| 1170 | const struct sym_probe_fns *pc_probe_fns; |
| 1171 | unsigned n_args; |
| 1172 | |
| 1173 | /* SEL == -1 means "_probe_argc". */ |
| 1174 | gdb_assert (sel >= -1); |
| 1175 | |
| 1176 | pc_probe = find_probe_by_pc (pc); |
| 1177 | if (pc_probe == NULL) |
| 1178 | error (_("No SystemTap probe at PC %s"), core_addr_to_string (pc)); |
| 1179 | |
| 1180 | n_args = get_probe_argument_count (pc_probe); |
| 1181 | if (sel == -1) |
| 1182 | return value_from_longest (builtin_type (arch)->builtin_int, n_args); |
| 1183 | |
| 1184 | if (sel >= n_args) |
| 1185 | error (_("Invalid probe argument %d -- probe has %u arguments available"), |
| 1186 | sel, n_args); |
| 1187 | |
| 1188 | return evaluate_probe_argument (pc_probe, sel); |
| 1189 | } |
| 1190 | |
| 1191 | /* This is called to compile one of the $_probe_arg* convenience |
| 1192 | variables into an agent expression. */ |
| 1193 | |
| 1194 | static void |
| 1195 | compile_probe_arg (struct internalvar *ivar, struct agent_expr *expr, |
| 1196 | struct axs_value *value, void *data) |
| 1197 | { |
| 1198 | CORE_ADDR pc = expr->scope; |
| 1199 | int sel = (int) (uintptr_t) data; |
| 1200 | struct probe *pc_probe; |
| 1201 | const struct sym_probe_fns *pc_probe_fns; |
| 1202 | int n_args; |
| 1203 | |
| 1204 | /* SEL == -1 means "_probe_argc". */ |
| 1205 | gdb_assert (sel >= -1); |
| 1206 | |
| 1207 | pc_probe = find_probe_by_pc (pc); |
| 1208 | if (pc_probe == NULL) |
| 1209 | error (_("No SystemTap probe at PC %s"), core_addr_to_string (pc)); |
| 1210 | |
| 1211 | n_args = get_probe_argument_count (pc_probe); |
| 1212 | |
| 1213 | if (sel == -1) |
| 1214 | { |
| 1215 | value->kind = axs_rvalue; |
| 1216 | value->type = builtin_type (expr->gdbarch)->builtin_int; |
| 1217 | ax_const_l (expr, n_args); |
| 1218 | return; |
| 1219 | } |
| 1220 | |
| 1221 | gdb_assert (sel >= 0); |
| 1222 | if (sel >= n_args) |
| 1223 | error (_("Invalid probe argument %d -- probe has %d arguments available"), |
| 1224 | sel, n_args); |
| 1225 | |
| 1226 | pc_probe->pops->compile_to_ax (pc_probe, expr, value, sel); |
| 1227 | } |
| 1228 | |
| 1229 | \f |
| 1230 | |
| 1231 | /* Set or clear a SystemTap semaphore. ADDRESS is the semaphore's |
| 1232 | address. SET is zero if the semaphore should be cleared, or one |
| 1233 | if it should be set. This is a helper function for `stap_semaphore_down' |
| 1234 | and `stap_semaphore_up'. */ |
| 1235 | |
| 1236 | static void |
| 1237 | stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch) |
| 1238 | { |
| 1239 | gdb_byte bytes[sizeof (LONGEST)]; |
| 1240 | /* The ABI specifies "unsigned short". */ |
| 1241 | struct type *type = builtin_type (gdbarch)->builtin_unsigned_short; |
| 1242 | ULONGEST value; |
| 1243 | |
| 1244 | if (address == 0) |
| 1245 | return; |
| 1246 | |
| 1247 | /* Swallow errors. */ |
| 1248 | if (target_read_memory (address, bytes, TYPE_LENGTH (type)) != 0) |
| 1249 | { |
| 1250 | warning (_("Could not read the value of a SystemTap semaphore.")); |
| 1251 | return; |
| 1252 | } |
| 1253 | |
| 1254 | value = extract_unsigned_integer (bytes, TYPE_LENGTH (type), |
| 1255 | gdbarch_byte_order (gdbarch)); |
| 1256 | /* Note that we explicitly don't worry about overflow or |
| 1257 | underflow. */ |
| 1258 | if (set) |
| 1259 | ++value; |
| 1260 | else |
| 1261 | --value; |
| 1262 | |
| 1263 | store_unsigned_integer (bytes, TYPE_LENGTH (type), |
| 1264 | gdbarch_byte_order (gdbarch), value); |
| 1265 | |
| 1266 | if (target_write_memory (address, bytes, TYPE_LENGTH (type)) != 0) |
| 1267 | warning (_("Could not write the value of a SystemTap semaphore.")); |
| 1268 | } |
| 1269 | |
| 1270 | /* Set a SystemTap semaphore. SEM is the semaphore's address. Semaphores |
| 1271 | act as reference counters, so calls to this function must be paired with |
| 1272 | calls to `stap_semaphore_down'. |
| 1273 | |
| 1274 | This function and `stap_semaphore_down' race with another tool changing |
| 1275 | the probes, but that is too rare to care. */ |
| 1276 | |
| 1277 | static void |
| 1278 | stap_set_semaphore (struct probe *probe_generic, struct gdbarch *gdbarch) |
| 1279 | { |
| 1280 | struct stap_probe *probe = (struct stap_probe *) probe_generic; |
| 1281 | |
| 1282 | gdb_assert (probe_generic->pops == &stap_probe_ops); |
| 1283 | |
| 1284 | stap_modify_semaphore (probe->sem_addr, 1, gdbarch); |
| 1285 | } |
| 1286 | |
| 1287 | /* Clear a SystemTap semaphore. SEM is the semaphore's address. */ |
| 1288 | |
| 1289 | static void |
| 1290 | stap_clear_semaphore (struct probe *probe_generic, struct gdbarch *gdbarch) |
| 1291 | { |
| 1292 | struct stap_probe *probe = (struct stap_probe *) probe_generic; |
| 1293 | |
| 1294 | gdb_assert (probe_generic->pops == &stap_probe_ops); |
| 1295 | |
| 1296 | stap_modify_semaphore (probe->sem_addr, 0, gdbarch); |
| 1297 | } |
| 1298 | |
| 1299 | /* Implementation of `$_probe_arg*' set of variables. */ |
| 1300 | |
| 1301 | static const struct internalvar_funcs probe_funcs = |
| 1302 | { |
| 1303 | compute_probe_arg, |
| 1304 | compile_probe_arg, |
| 1305 | NULL |
| 1306 | }; |
| 1307 | |
| 1308 | /* Helper function that parses the information contained in a |
| 1309 | SystemTap's probe. Basically, the information consists in: |
| 1310 | |
| 1311 | - Probe's PC address; |
| 1312 | - Link-time section address of `.stapsdt.base' section; |
| 1313 | - Link-time address of the semaphore variable, or ZERO if the |
| 1314 | probe doesn't have an associated semaphore; |
| 1315 | - Probe's provider name; |
| 1316 | - Probe's name; |
| 1317 | - Probe's argument format |
| 1318 | |
| 1319 | This function returns 1 if the handling was successful, and zero |
| 1320 | otherwise. */ |
| 1321 | |
| 1322 | static void |
| 1323 | handle_stap_probe (struct objfile *objfile, struct sdt_note *el, |
| 1324 | VEC (probe_p) **probesp, CORE_ADDR base) |
| 1325 | { |
| 1326 | bfd *abfd = objfile->obfd; |
| 1327 | int size = bfd_get_arch_size (abfd) / 8; |
| 1328 | struct gdbarch *gdbarch = get_objfile_arch (objfile); |
| 1329 | struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr; |
| 1330 | CORE_ADDR base_ref; |
| 1331 | const char *probe_args = NULL; |
| 1332 | struct stap_probe *ret; |
| 1333 | |
| 1334 | ret = obstack_alloc (&objfile->objfile_obstack, sizeof (*ret)); |
| 1335 | ret->p.pops = &stap_probe_ops; |
| 1336 | ret->p.objfile = objfile; |
| 1337 | |
| 1338 | /* Provider and the name of the probe. */ |
| 1339 | ret->p.provider = (char *) &el->data[3 * size]; |
| 1340 | ret->p.name = memchr (ret->p.provider, '\0', |
| 1341 | (char *) el->data + el->size - ret->p.provider); |
| 1342 | /* Making sure there is a name. */ |
| 1343 | if (!ret->p.name) |
| 1344 | { |
| 1345 | complaint (&symfile_complaints, _("corrupt probe name when " |
| 1346 | "reading `%s'"), |
| 1347 | objfile_name (objfile)); |
| 1348 | |
| 1349 | /* There is no way to use a probe without a name or a provider, so |
| 1350 | returning zero here makes sense. */ |
| 1351 | return; |
| 1352 | } |
| 1353 | else |
| 1354 | ++ret->p.name; |
| 1355 | |
| 1356 | /* Retrieving the probe's address. */ |
| 1357 | ret->p.address = extract_typed_address (&el->data[0], ptr_type); |
| 1358 | |
| 1359 | /* Link-time sh_addr of `.stapsdt.base' section. */ |
| 1360 | base_ref = extract_typed_address (&el->data[size], ptr_type); |
| 1361 | |
| 1362 | /* Semaphore address. */ |
| 1363 | ret->sem_addr = extract_typed_address (&el->data[2 * size], ptr_type); |
| 1364 | |
| 1365 | ret->p.address += (ANOFFSET (objfile->section_offsets, |
| 1366 | SECT_OFF_TEXT (objfile)) |
| 1367 | + base - base_ref); |
| 1368 | if (ret->sem_addr) |
| 1369 | ret->sem_addr += (ANOFFSET (objfile->section_offsets, |
| 1370 | SECT_OFF_DATA (objfile)) |
| 1371 | + base - base_ref); |
| 1372 | |
| 1373 | /* Arguments. We can only extract the argument format if there is a valid |
| 1374 | name for this probe. */ |
| 1375 | probe_args = memchr (ret->p.name, '\0', |
| 1376 | (char *) el->data + el->size - ret->p.name); |
| 1377 | |
| 1378 | if (probe_args != NULL) |
| 1379 | ++probe_args; |
| 1380 | |
| 1381 | if (probe_args == NULL || (memchr (probe_args, '\0', |
| 1382 | (char *) el->data + el->size - ret->p.name) |
| 1383 | != el->data + el->size - 1)) |
| 1384 | { |
| 1385 | complaint (&symfile_complaints, _("corrupt probe argument when " |
| 1386 | "reading `%s'"), |
| 1387 | objfile_name (objfile)); |
| 1388 | /* If the argument string is NULL, it means some problem happened with |
| 1389 | it. So we return 0. */ |
| 1390 | return; |
| 1391 | } |
| 1392 | |
| 1393 | ret->args_parsed = 0; |
| 1394 | ret->args_u.text = (void *) probe_args; |
| 1395 | |
| 1396 | /* Successfully created probe. */ |
| 1397 | VEC_safe_push (probe_p, *probesp, (struct probe *) ret); |
| 1398 | } |
| 1399 | |
| 1400 | /* Helper function which tries to find the base address of the SystemTap |
| 1401 | base section named STAP_BASE_SECTION_NAME. */ |
| 1402 | |
| 1403 | static void |
| 1404 | get_stap_base_address_1 (bfd *abfd, asection *sect, void *obj) |
| 1405 | { |
| 1406 | asection **ret = obj; |
| 1407 | |
| 1408 | if ((sect->flags & (SEC_DATA | SEC_ALLOC | SEC_HAS_CONTENTS)) |
| 1409 | && sect->name && !strcmp (sect->name, STAP_BASE_SECTION_NAME)) |
| 1410 | *ret = sect; |
| 1411 | } |
| 1412 | |
| 1413 | /* Helper function which iterates over every section in the BFD file, |
| 1414 | trying to find the base address of the SystemTap base section. |
| 1415 | Returns 1 if found (setting BASE to the proper value), zero otherwise. */ |
| 1416 | |
| 1417 | static int |
| 1418 | get_stap_base_address (bfd *obfd, bfd_vma *base) |
| 1419 | { |
| 1420 | asection *ret = NULL; |
| 1421 | |
| 1422 | bfd_map_over_sections (obfd, get_stap_base_address_1, (void *) &ret); |
| 1423 | |
| 1424 | if (!ret) |
| 1425 | { |
| 1426 | complaint (&symfile_complaints, _("could not obtain base address for " |
| 1427 | "SystemTap section on objfile `%s'."), |
| 1428 | obfd->filename); |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | if (base) |
| 1433 | *base = ret->vma; |
| 1434 | |
| 1435 | return 1; |
| 1436 | } |
| 1437 | |
| 1438 | /* Helper function for `elf_get_probes', which gathers information about all |
| 1439 | SystemTap probes from OBJFILE. */ |
| 1440 | |
| 1441 | static void |
| 1442 | stap_get_probes (VEC (probe_p) **probesp, struct objfile *objfile) |
| 1443 | { |
| 1444 | /* If we are here, then this is the first time we are parsing the |
| 1445 | SystemTap probe's information. We basically have to count how many |
| 1446 | probes the objfile has, and then fill in the necessary information |
| 1447 | for each one. */ |
| 1448 | bfd *obfd = objfile->obfd; |
| 1449 | bfd_vma base; |
| 1450 | struct sdt_note *iter; |
| 1451 | unsigned save_probesp_len = VEC_length (probe_p, *probesp); |
| 1452 | |
| 1453 | if (objfile->separate_debug_objfile_backlink != NULL) |
| 1454 | { |
| 1455 | /* This is a .debug file, not the objfile itself. */ |
| 1456 | return; |
| 1457 | } |
| 1458 | |
| 1459 | if (!elf_tdata (obfd)->sdt_note_head) |
| 1460 | { |
| 1461 | /* There isn't any probe here. */ |
| 1462 | return; |
| 1463 | } |
| 1464 | |
| 1465 | if (!get_stap_base_address (obfd, &base)) |
| 1466 | { |
| 1467 | /* There was an error finding the base address for the section. |
| 1468 | Just return NULL. */ |
| 1469 | return; |
| 1470 | } |
| 1471 | |
| 1472 | /* Parsing each probe's information. */ |
| 1473 | for (iter = elf_tdata (obfd)->sdt_note_head; iter; iter = iter->next) |
| 1474 | { |
| 1475 | /* We first have to handle all the information about the |
| 1476 | probe which is present in the section. */ |
| 1477 | handle_stap_probe (objfile, iter, probesp, base); |
| 1478 | } |
| 1479 | |
| 1480 | if (save_probesp_len == VEC_length (probe_p, *probesp)) |
| 1481 | { |
| 1482 | /* If we are here, it means we have failed to parse every known |
| 1483 | probe. */ |
| 1484 | complaint (&symfile_complaints, _("could not parse SystemTap probe(s) " |
| 1485 | "from inferior")); |
| 1486 | return; |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | static void |
| 1491 | stap_relocate (struct probe *probe_generic, CORE_ADDR delta) |
| 1492 | { |
| 1493 | struct stap_probe *probe = (struct stap_probe *) probe_generic; |
| 1494 | |
| 1495 | gdb_assert (probe_generic->pops == &stap_probe_ops); |
| 1496 | |
| 1497 | probe->p.address += delta; |
| 1498 | if (probe->sem_addr) |
| 1499 | probe->sem_addr += delta; |
| 1500 | } |
| 1501 | |
| 1502 | static int |
| 1503 | stap_probe_is_linespec (const char **linespecp) |
| 1504 | { |
| 1505 | static const char *const keywords[] = { "-pstap", "-probe-stap", NULL }; |
| 1506 | |
| 1507 | return probe_is_linespec_by_keyword (linespecp, keywords); |
| 1508 | } |
| 1509 | |
| 1510 | static void |
| 1511 | stap_gen_info_probes_table_header (VEC (info_probe_column_s) **heads) |
| 1512 | { |
| 1513 | info_probe_column_s stap_probe_column; |
| 1514 | |
| 1515 | stap_probe_column.field_name = "semaphore"; |
| 1516 | stap_probe_column.print_name = _("Semaphore"); |
| 1517 | |
| 1518 | VEC_safe_push (info_probe_column_s, *heads, &stap_probe_column); |
| 1519 | } |
| 1520 | |
| 1521 | static void |
| 1522 | stap_gen_info_probes_table_values (struct probe *probe_generic, |
| 1523 | VEC (const_char_ptr) **ret) |
| 1524 | { |
| 1525 | struct stap_probe *probe = (struct stap_probe *) probe_generic; |
| 1526 | struct gdbarch *gdbarch; |
| 1527 | const char *val = NULL; |
| 1528 | |
| 1529 | gdb_assert (probe_generic->pops == &stap_probe_ops); |
| 1530 | |
| 1531 | gdbarch = get_objfile_arch (probe->p.objfile); |
| 1532 | |
| 1533 | if (probe->sem_addr) |
| 1534 | val = print_core_address (gdbarch, probe->sem_addr); |
| 1535 | |
| 1536 | VEC_safe_push (const_char_ptr, *ret, val); |
| 1537 | } |
| 1538 | |
| 1539 | /* SystemTap probe_ops. */ |
| 1540 | |
| 1541 | static const struct probe_ops stap_probe_ops = |
| 1542 | { |
| 1543 | stap_probe_is_linespec, |
| 1544 | stap_get_probes, |
| 1545 | stap_relocate, |
| 1546 | stap_get_probe_argument_count, |
| 1547 | stap_can_evaluate_probe_arguments, |
| 1548 | stap_evaluate_probe_argument, |
| 1549 | stap_compile_to_ax, |
| 1550 | stap_set_semaphore, |
| 1551 | stap_clear_semaphore, |
| 1552 | stap_probe_destroy, |
| 1553 | stap_gen_info_probes_table_header, |
| 1554 | stap_gen_info_probes_table_values, |
| 1555 | }; |
| 1556 | |
| 1557 | /* Implementation of the `info probes stap' command. */ |
| 1558 | |
| 1559 | static void |
| 1560 | info_probes_stap_command (char *arg, int from_tty) |
| 1561 | { |
| 1562 | info_probes_for_ops (arg, from_tty, &stap_probe_ops); |
| 1563 | } |
| 1564 | |
| 1565 | void _initialize_stap_probe (void); |
| 1566 | |
| 1567 | void |
| 1568 | _initialize_stap_probe (void) |
| 1569 | { |
| 1570 | VEC_safe_push (probe_ops_cp, all_probe_ops, &stap_probe_ops); |
| 1571 | |
| 1572 | add_setshow_zuinteger_cmd ("stap-expression", class_maintenance, |
| 1573 | &stap_expression_debug, |
| 1574 | _("Set SystemTap expression debugging."), |
| 1575 | _("Show SystemTap expression debugging."), |
| 1576 | _("When non-zero, the internal representation " |
| 1577 | "of SystemTap expressions will be printed."), |
| 1578 | NULL, |
| 1579 | show_stapexpressiondebug, |
| 1580 | &setdebuglist, &showdebuglist); |
| 1581 | |
| 1582 | create_internalvar_type_lazy ("_probe_argc", &probe_funcs, |
| 1583 | (void *) (uintptr_t) -1); |
| 1584 | create_internalvar_type_lazy ("_probe_arg0", &probe_funcs, |
| 1585 | (void *) (uintptr_t) 0); |
| 1586 | create_internalvar_type_lazy ("_probe_arg1", &probe_funcs, |
| 1587 | (void *) (uintptr_t) 1); |
| 1588 | create_internalvar_type_lazy ("_probe_arg2", &probe_funcs, |
| 1589 | (void *) (uintptr_t) 2); |
| 1590 | create_internalvar_type_lazy ("_probe_arg3", &probe_funcs, |
| 1591 | (void *) (uintptr_t) 3); |
| 1592 | create_internalvar_type_lazy ("_probe_arg4", &probe_funcs, |
| 1593 | (void *) (uintptr_t) 4); |
| 1594 | create_internalvar_type_lazy ("_probe_arg5", &probe_funcs, |
| 1595 | (void *) (uintptr_t) 5); |
| 1596 | create_internalvar_type_lazy ("_probe_arg6", &probe_funcs, |
| 1597 | (void *) (uintptr_t) 6); |
| 1598 | create_internalvar_type_lazy ("_probe_arg7", &probe_funcs, |
| 1599 | (void *) (uintptr_t) 7); |
| 1600 | create_internalvar_type_lazy ("_probe_arg8", &probe_funcs, |
| 1601 | (void *) (uintptr_t) 8); |
| 1602 | create_internalvar_type_lazy ("_probe_arg9", &probe_funcs, |
| 1603 | (void *) (uintptr_t) 9); |
| 1604 | create_internalvar_type_lazy ("_probe_arg10", &probe_funcs, |
| 1605 | (void *) (uintptr_t) 10); |
| 1606 | create_internalvar_type_lazy ("_probe_arg11", &probe_funcs, |
| 1607 | (void *) (uintptr_t) 11); |
| 1608 | |
| 1609 | add_cmd ("stap", class_info, info_probes_stap_command, |
| 1610 | _("\ |
| 1611 | Show information about SystemTap static probes.\n\ |
| 1612 | Usage: info probes stap [PROVIDER [NAME [OBJECT]]]\n\ |
| 1613 | Each argument is a regular expression, used to select probes.\n\ |
| 1614 | PROVIDER matches probe provider names.\n\ |
| 1615 | NAME matches the probe names.\n\ |
| 1616 | OBJECT matches the executable or shared library name."), |
| 1617 | info_probes_cmdlist_get ()); |
| 1618 | |
| 1619 | } |