Commit | Line | Data |
---|---|---|
55aa24fb SDJ |
1 | /* SystemTap probe support for GDB. |
2 | ||
3666a048 | 3 | Copyright (C) 2012-2021 Free Software Foundation, Inc. |
55aa24fb SDJ |
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" | |
55aa24fb SDJ |
23 | #include "ui-out.h" |
24 | #include "objfiles.h" | |
25 | #include "arch-utils.h" | |
26 | #include "command.h" | |
27 | #include "gdbcmd.h" | |
28 | #include "filenames.h" | |
29 | #include "value.h" | |
55aa24fb SDJ |
30 | #include "ax.h" |
31 | #include "ax-gdb.h" | |
32 | #include "complaints.h" | |
33 | #include "cli/cli-utils.h" | |
34 | #include "linespec.h" | |
35 | #include "user-regs.h" | |
36 | #include "parser-defs.h" | |
37 | #include "language.h" | |
38 | #include "elf-bfd.h" | |
4c5e7a93 TT |
39 | #include "expop.h" |
40 | #include <unordered_map> | |
55aa24fb SDJ |
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 | ||
55aa24fb SDJ |
49 | /* Should we display debug information for the probe's argument expression |
50 | parsing? */ | |
51 | ||
ccce17b0 | 52 | static unsigned int stap_expression_debug = 0; |
55aa24fb SDJ |
53 | |
54 | /* The various possibilities of bitness defined for a probe's argument. | |
55 | ||
56 | The relationship is: | |
57 | ||
58 | - STAP_ARG_BITNESS_UNDEFINED: The user hasn't specified the bitness. | |
30a1e6cc SDJ |
59 | - STAP_ARG_BITNESS_8BIT_UNSIGNED: argument string starts with `1@'. |
60 | - STAP_ARG_BITNESS_8BIT_SIGNED: argument string starts with `-1@'. | |
61 | - STAP_ARG_BITNESS_16BIT_UNSIGNED: argument string starts with `2@'. | |
62 | - STAP_ARG_BITNESS_16BIT_SIGNED: argument string starts with `-2@'. | |
55aa24fb SDJ |
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, | |
30a1e6cc SDJ |
71 | STAP_ARG_BITNESS_8BIT_UNSIGNED, |
72 | STAP_ARG_BITNESS_8BIT_SIGNED, | |
73 | STAP_ARG_BITNESS_16BIT_UNSIGNED, | |
74 | STAP_ARG_BITNESS_16BIT_SIGNED, | |
55aa24fb SDJ |
75 | STAP_ARG_BITNESS_32BIT_UNSIGNED, |
76 | STAP_ARG_BITNESS_32BIT_SIGNED, | |
77 | STAP_ARG_BITNESS_64BIT_UNSIGNED, | |
78 | STAP_ARG_BITNESS_64BIT_SIGNED, | |
79 | }; | |
80 | ||
81 | /* The following structure represents a single argument for the probe. */ | |
82 | ||
83 | struct stap_probe_arg | |
84 | { | |
0e9ae10f SDJ |
85 | /* Constructor for stap_probe_arg. */ |
86 | stap_probe_arg (enum stap_arg_bitness bitness_, struct type *atype_, | |
87 | expression_up &&aexpr_) | |
88 | : bitness (bitness_), atype (atype_), aexpr (std::move (aexpr_)) | |
89 | {} | |
90 | ||
55aa24fb SDJ |
91 | /* The bitness of this argument. */ |
92 | enum stap_arg_bitness bitness; | |
93 | ||
94 | /* The corresponding `struct type *' to the bitness. */ | |
95 | struct type *atype; | |
96 | ||
97 | /* The argument converted to an internal GDB expression. */ | |
0e9ae10f | 98 | expression_up aexpr; |
55aa24fb SDJ |
99 | }; |
100 | ||
0e9ae10f | 101 | /* Class that implements the static probe methods for "stap" probes. */ |
55aa24fb | 102 | |
0e9ae10f | 103 | class stap_static_probe_ops : public static_probe_ops |
55aa24fb | 104 | { |
0e9ae10f | 105 | public: |
4212d509 TT |
106 | /* We need a user-provided constructor to placate some compilers. |
107 | See PR build/24937. */ | |
108 | stap_static_probe_ops () | |
109 | { | |
110 | } | |
111 | ||
0e9ae10f SDJ |
112 | /* See probe.h. */ |
113 | bool is_linespec (const char **linespecp) const override; | |
55aa24fb | 114 | |
0e9ae10f | 115 | /* See probe.h. */ |
814cf43a | 116 | void get_probes (std::vector<std::unique_ptr<probe>> *probesp, |
0e9ae10f SDJ |
117 | struct objfile *objfile) const override; |
118 | ||
119 | /* See probe.h. */ | |
120 | const char *type_name () const override; | |
121 | ||
122 | /* See probe.h. */ | |
123 | std::vector<struct info_probe_column> gen_info_probes_table_header | |
124 | () const override; | |
125 | }; | |
126 | ||
127 | /* SystemTap static_probe_ops. */ | |
128 | ||
3dcfdc58 | 129 | const stap_static_probe_ops stap_static_probe_ops {}; |
0e9ae10f SDJ |
130 | |
131 | class stap_probe : public probe | |
132 | { | |
133 | public: | |
134 | /* Constructor for stap_probe. */ | |
135 | stap_probe (std::string &&name_, std::string &&provider_, CORE_ADDR address_, | |
136 | struct gdbarch *arch_, CORE_ADDR sem_addr, const char *args_text) | |
137 | : probe (std::move (name_), std::move (provider_), address_, arch_), | |
138 | m_sem_addr (sem_addr), | |
139 | m_have_parsed_args (false), m_unparsed_args_text (args_text) | |
140 | {} | |
141 | ||
142 | /* See probe.h. */ | |
143 | CORE_ADDR get_relocated_address (struct objfile *objfile) override; | |
144 | ||
145 | /* See probe.h. */ | |
fe01123e | 146 | unsigned get_argument_count (struct gdbarch *gdbarch) override; |
0e9ae10f SDJ |
147 | |
148 | /* See probe.h. */ | |
149 | bool can_evaluate_arguments () const override; | |
150 | ||
151 | /* See probe.h. */ | |
152 | struct value *evaluate_argument (unsigned n, | |
153 | struct frame_info *frame) override; | |
154 | ||
155 | /* See probe.h. */ | |
156 | void compile_to_ax (struct agent_expr *aexpr, | |
157 | struct axs_value *axs_value, | |
158 | unsigned n) override; | |
159 | ||
160 | /* See probe.h. */ | |
161 | void set_semaphore (struct objfile *objfile, | |
162 | struct gdbarch *gdbarch) override; | |
163 | ||
164 | /* See probe.h. */ | |
165 | void clear_semaphore (struct objfile *objfile, | |
166 | struct gdbarch *gdbarch) override; | |
167 | ||
168 | /* See probe.h. */ | |
169 | const static_probe_ops *get_static_ops () const override; | |
170 | ||
171 | /* See probe.h. */ | |
172 | std::vector<const char *> gen_info_probes_table_values () const override; | |
173 | ||
174 | /* Return argument N of probe. | |
175 | ||
176 | If the probe's arguments have not been parsed yet, parse them. If | |
177 | there are no arguments, throw an exception (error). Otherwise, | |
178 | return the requested argument. */ | |
179 | struct stap_probe_arg *get_arg_by_number (unsigned n, | |
180 | struct gdbarch *gdbarch) | |
181 | { | |
182 | if (!m_have_parsed_args) | |
183 | this->parse_arguments (gdbarch); | |
184 | ||
185 | gdb_assert (m_have_parsed_args); | |
186 | if (m_parsed_args.empty ()) | |
187 | internal_error (__FILE__, __LINE__, | |
188 | _("Probe '%s' apparently does not have arguments, but \n" | |
189 | "GDB is requesting its argument number %u anyway. " | |
190 | "This should not happen. Please report this bug."), | |
191 | this->get_name ().c_str (), n); | |
192 | ||
193 | if (n > m_parsed_args.size ()) | |
194 | internal_error (__FILE__, __LINE__, | |
195 | _("Probe '%s' has %d arguments, but GDB is requesting\n" | |
196 | "argument %u. This should not happen. Please\n" | |
197 | "report this bug."), | |
198 | this->get_name ().c_str (), | |
199 | (int) m_parsed_args.size (), n); | |
200 | ||
201 | return &m_parsed_args[n]; | |
202 | } | |
203 | ||
204 | /* Function which parses an argument string from the probe, | |
205 | correctly splitting the arguments and storing their information | |
206 | in properly ways. | |
207 | ||
208 | Consider the following argument string (x86 syntax): | |
209 | ||
210 | `4@%eax 4@$10' | |
211 | ||
212 | We have two arguments, `%eax' and `$10', both with 32-bit | |
213 | unsigned bitness. This function basically handles them, properly | |
214 | filling some structures with this information. */ | |
215 | void parse_arguments (struct gdbarch *gdbarch); | |
216 | ||
217 | private: | |
55aa24fb | 218 | /* If the probe has a semaphore associated, then this is the value of |
729662a5 | 219 | it, relative to SECT_OFF_DATA. */ |
0e9ae10f | 220 | CORE_ADDR m_sem_addr; |
55aa24fb | 221 | |
0e9ae10f SDJ |
222 | /* True if the arguments have been parsed. */ |
223 | bool m_have_parsed_args; | |
97c2dca0 | 224 | |
0e9ae10f SDJ |
225 | /* The text version of the probe's arguments, unparsed. */ |
226 | const char *m_unparsed_args_text; | |
55aa24fb | 227 | |
0e9ae10f SDJ |
228 | /* Information about each argument. This is an array of `stap_probe_arg', |
229 | with each entry representing one argument. This is only valid if | |
230 | M_ARGS_PARSED is true. */ | |
231 | std::vector<struct stap_probe_arg> m_parsed_args; | |
55aa24fb SDJ |
232 | }; |
233 | ||
234 | /* When parsing the arguments, we have to establish different precedences | |
235 | for the various kinds of asm operators. This enumeration represents those | |
236 | precedences. | |
237 | ||
238 | This logic behind this is available at | |
239 | <http://sourceware.org/binutils/docs/as/Infix-Ops.html#Infix-Ops>, or using | |
240 | the command "info '(as)Infix Ops'". */ | |
241 | ||
242 | enum stap_operand_prec | |
243 | { | |
244 | /* Lowest precedence, used for non-recognized operands or for the beginning | |
245 | of the parsing process. */ | |
246 | STAP_OPERAND_PREC_NONE = 0, | |
247 | ||
248 | /* Precedence of logical OR. */ | |
249 | STAP_OPERAND_PREC_LOGICAL_OR, | |
250 | ||
251 | /* Precedence of logical AND. */ | |
252 | STAP_OPERAND_PREC_LOGICAL_AND, | |
253 | ||
254 | /* Precedence of additive (plus, minus) and comparative (equal, less, | |
255 | greater-than, etc) operands. */ | |
256 | STAP_OPERAND_PREC_ADD_CMP, | |
257 | ||
258 | /* Precedence of bitwise operands (bitwise OR, XOR, bitwise AND, | |
259 | logical NOT). */ | |
260 | STAP_OPERAND_PREC_BITWISE, | |
261 | ||
262 | /* Precedence of multiplicative operands (multiplication, division, | |
263 | remainder, left shift and right shift). */ | |
264 | STAP_OPERAND_PREC_MUL | |
265 | }; | |
266 | ||
4c5e7a93 TT |
267 | static expr::operation_up stap_parse_argument_1 (struct stap_parse_info *p, |
268 | expr::operation_up &&lhs, | |
269 | enum stap_operand_prec prec) | |
270 | ATTRIBUTE_UNUSED_RESULT; | |
55aa24fb | 271 | |
4c5e7a93 TT |
272 | static expr::operation_up stap_parse_argument_conditionally |
273 | (struct stap_parse_info *p) ATTRIBUTE_UNUSED_RESULT; | |
55aa24fb | 274 | |
af2d9bee | 275 | /* Returns true if *S is an operator, false otherwise. */ |
55aa24fb | 276 | |
af2d9bee | 277 | static bool stap_is_operator (const char *op); |
55aa24fb SDJ |
278 | |
279 | static void | |
280 | show_stapexpressiondebug (struct ui_file *file, int from_tty, | |
281 | struct cmd_list_element *c, const char *value) | |
282 | { | |
283 | fprintf_filtered (file, _("SystemTap Probe expression debugging is %s.\n"), | |
284 | value); | |
285 | } | |
286 | ||
287 | /* Returns the operator precedence level of OP, or STAP_OPERAND_PREC_NONE | |
288 | if the operator code was not recognized. */ | |
289 | ||
290 | static enum stap_operand_prec | |
291 | stap_get_operator_prec (enum exp_opcode op) | |
292 | { | |
293 | switch (op) | |
294 | { | |
295 | case BINOP_LOGICAL_OR: | |
296 | return STAP_OPERAND_PREC_LOGICAL_OR; | |
297 | ||
298 | case BINOP_LOGICAL_AND: | |
299 | return STAP_OPERAND_PREC_LOGICAL_AND; | |
300 | ||
301 | case BINOP_ADD: | |
302 | case BINOP_SUB: | |
303 | case BINOP_EQUAL: | |
304 | case BINOP_NOTEQUAL: | |
305 | case BINOP_LESS: | |
306 | case BINOP_LEQ: | |
307 | case BINOP_GTR: | |
308 | case BINOP_GEQ: | |
309 | return STAP_OPERAND_PREC_ADD_CMP; | |
310 | ||
311 | case BINOP_BITWISE_IOR: | |
312 | case BINOP_BITWISE_AND: | |
313 | case BINOP_BITWISE_XOR: | |
314 | case UNOP_LOGICAL_NOT: | |
315 | return STAP_OPERAND_PREC_BITWISE; | |
316 | ||
317 | case BINOP_MUL: | |
318 | case BINOP_DIV: | |
319 | case BINOP_REM: | |
320 | case BINOP_LSH: | |
321 | case BINOP_RSH: | |
322 | return STAP_OPERAND_PREC_MUL; | |
323 | ||
324 | default: | |
325 | return STAP_OPERAND_PREC_NONE; | |
326 | } | |
327 | } | |
328 | ||
3ca58cde SDJ |
329 | /* Given S, read the operator in it. Return the EXP_OPCODE which |
330 | represents the operator detected, or throw an error if no operator | |
331 | was found. */ | |
55aa24fb | 332 | |
fcf57f19 SDJ |
333 | static enum exp_opcode |
334 | stap_get_opcode (const char **s) | |
55aa24fb SDJ |
335 | { |
336 | const char c = **s; | |
fcf57f19 | 337 | enum exp_opcode op; |
55aa24fb SDJ |
338 | |
339 | *s += 1; | |
340 | ||
341 | switch (c) | |
342 | { | |
343 | case '*': | |
fcf57f19 | 344 | op = BINOP_MUL; |
55aa24fb SDJ |
345 | break; |
346 | ||
347 | case '/': | |
fcf57f19 | 348 | op = BINOP_DIV; |
55aa24fb SDJ |
349 | break; |
350 | ||
351 | case '%': | |
fcf57f19 | 352 | op = BINOP_REM; |
55aa24fb SDJ |
353 | break; |
354 | ||
355 | case '<': | |
fcf57f19 | 356 | op = BINOP_LESS; |
55aa24fb SDJ |
357 | if (**s == '<') |
358 | { | |
359 | *s += 1; | |
fcf57f19 | 360 | op = BINOP_LSH; |
55aa24fb SDJ |
361 | } |
362 | else if (**s == '=') | |
363 | { | |
364 | *s += 1; | |
fcf57f19 | 365 | op = BINOP_LEQ; |
55aa24fb SDJ |
366 | } |
367 | else if (**s == '>') | |
368 | { | |
369 | *s += 1; | |
fcf57f19 | 370 | op = BINOP_NOTEQUAL; |
55aa24fb SDJ |
371 | } |
372 | break; | |
373 | ||
374 | case '>': | |
fcf57f19 | 375 | op = BINOP_GTR; |
55aa24fb SDJ |
376 | if (**s == '>') |
377 | { | |
378 | *s += 1; | |
fcf57f19 | 379 | op = BINOP_RSH; |
55aa24fb SDJ |
380 | } |
381 | else if (**s == '=') | |
382 | { | |
383 | *s += 1; | |
fcf57f19 | 384 | op = BINOP_GEQ; |
55aa24fb SDJ |
385 | } |
386 | break; | |
387 | ||
388 | case '|': | |
fcf57f19 | 389 | op = BINOP_BITWISE_IOR; |
55aa24fb SDJ |
390 | if (**s == '|') |
391 | { | |
392 | *s += 1; | |
fcf57f19 | 393 | op = BINOP_LOGICAL_OR; |
55aa24fb SDJ |
394 | } |
395 | break; | |
396 | ||
397 | case '&': | |
fcf57f19 | 398 | op = BINOP_BITWISE_AND; |
55aa24fb SDJ |
399 | if (**s == '&') |
400 | { | |
401 | *s += 1; | |
fcf57f19 | 402 | op = BINOP_LOGICAL_AND; |
55aa24fb SDJ |
403 | } |
404 | break; | |
405 | ||
406 | case '^': | |
fcf57f19 | 407 | op = BINOP_BITWISE_XOR; |
55aa24fb SDJ |
408 | break; |
409 | ||
410 | case '!': | |
fcf57f19 | 411 | op = UNOP_LOGICAL_NOT; |
55aa24fb SDJ |
412 | break; |
413 | ||
414 | case '+': | |
fcf57f19 | 415 | op = BINOP_ADD; |
55aa24fb SDJ |
416 | break; |
417 | ||
418 | case '-': | |
fcf57f19 | 419 | op = BINOP_SUB; |
55aa24fb SDJ |
420 | break; |
421 | ||
422 | case '=': | |
fcf57f19 SDJ |
423 | gdb_assert (**s == '='); |
424 | op = BINOP_EQUAL; | |
55aa24fb SDJ |
425 | break; |
426 | ||
427 | default: | |
f469e8ce SDJ |
428 | error (_("Invalid opcode in expression `%s' for SystemTap" |
429 | "probe"), *s); | |
55aa24fb SDJ |
430 | } |
431 | ||
fcf57f19 | 432 | return op; |
55aa24fb SDJ |
433 | } |
434 | ||
4c5e7a93 TT |
435 | typedef expr::operation_up binop_maker_ftype (expr::operation_up &&, |
436 | expr::operation_up &&); | |
437 | /* Map from an expression opcode to a function that can create a | |
438 | binary operation of that type. */ | |
439 | static std::unordered_map<exp_opcode, binop_maker_ftype *> stap_maker_map; | |
440 | ||
441 | /* Helper function to create a binary operation. */ | |
442 | static expr::operation_up | |
443 | stap_make_binop (enum exp_opcode opcode, expr::operation_up &&lhs, | |
444 | expr::operation_up &&rhs) | |
445 | { | |
446 | auto iter = stap_maker_map.find (opcode); | |
447 | gdb_assert (iter != stap_maker_map.end ()); | |
448 | return iter->second (std::move (lhs), std::move (rhs)); | |
449 | } | |
450 | ||
55aa24fb | 451 | /* Given the bitness of the argument, represented by B, return the |
3ca58cde SDJ |
452 | corresponding `struct type *', or throw an error if B is |
453 | unknown. */ | |
55aa24fb SDJ |
454 | |
455 | static struct type * | |
456 | stap_get_expected_argument_type (struct gdbarch *gdbarch, | |
f469e8ce | 457 | enum stap_arg_bitness b, |
0e9ae10f | 458 | const char *probe_name) |
55aa24fb SDJ |
459 | { |
460 | switch (b) | |
461 | { | |
462 | case STAP_ARG_BITNESS_UNDEFINED: | |
463 | if (gdbarch_addr_bit (gdbarch) == 32) | |
464 | return builtin_type (gdbarch)->builtin_uint32; | |
465 | else | |
466 | return builtin_type (gdbarch)->builtin_uint64; | |
467 | ||
30a1e6cc SDJ |
468 | case STAP_ARG_BITNESS_8BIT_UNSIGNED: |
469 | return builtin_type (gdbarch)->builtin_uint8; | |
470 | ||
471 | case STAP_ARG_BITNESS_8BIT_SIGNED: | |
472 | return builtin_type (gdbarch)->builtin_int8; | |
473 | ||
474 | case STAP_ARG_BITNESS_16BIT_UNSIGNED: | |
475 | return builtin_type (gdbarch)->builtin_uint16; | |
476 | ||
477 | case STAP_ARG_BITNESS_16BIT_SIGNED: | |
478 | return builtin_type (gdbarch)->builtin_int16; | |
479 | ||
55aa24fb SDJ |
480 | case STAP_ARG_BITNESS_32BIT_SIGNED: |
481 | return builtin_type (gdbarch)->builtin_int32; | |
482 | ||
483 | case STAP_ARG_BITNESS_32BIT_UNSIGNED: | |
484 | return builtin_type (gdbarch)->builtin_uint32; | |
485 | ||
486 | case STAP_ARG_BITNESS_64BIT_SIGNED: | |
487 | return builtin_type (gdbarch)->builtin_int64; | |
488 | ||
489 | case STAP_ARG_BITNESS_64BIT_UNSIGNED: | |
490 | return builtin_type (gdbarch)->builtin_uint64; | |
491 | ||
492 | default: | |
0e9ae10f | 493 | error (_("Undefined bitness for probe '%s'."), probe_name); |
55aa24fb SDJ |
494 | break; |
495 | } | |
496 | } | |
497 | ||
05c0465e SDJ |
498 | /* Helper function to check for a generic list of prefixes. GDBARCH |
499 | is the current gdbarch being used. S is the expression being | |
500 | analyzed. If R is not NULL, it will be used to return the found | |
501 | prefix. PREFIXES is the list of expected prefixes. | |
502 | ||
503 | This function does a case-insensitive match. | |
504 | ||
af2d9bee | 505 | Return true if any prefix has been found, false otherwise. */ |
05c0465e | 506 | |
af2d9bee | 507 | static bool |
05c0465e SDJ |
508 | stap_is_generic_prefix (struct gdbarch *gdbarch, const char *s, |
509 | const char **r, const char *const *prefixes) | |
510 | { | |
511 | const char *const *p; | |
512 | ||
513 | if (prefixes == NULL) | |
514 | { | |
515 | if (r != NULL) | |
516 | *r = ""; | |
517 | ||
af2d9bee | 518 | return true; |
05c0465e SDJ |
519 | } |
520 | ||
521 | for (p = prefixes; *p != NULL; ++p) | |
97c2dca0 SDJ |
522 | if (strncasecmp (s, *p, strlen (*p)) == 0) |
523 | { | |
524 | if (r != NULL) | |
525 | *r = *p; | |
05c0465e | 526 | |
af2d9bee | 527 | return true; |
97c2dca0 | 528 | } |
05c0465e | 529 | |
af2d9bee | 530 | return false; |
05c0465e SDJ |
531 | } |
532 | ||
af2d9bee SDJ |
533 | /* Return true if S points to a register prefix, false otherwise. For |
534 | a description of the arguments, look at stap_is_generic_prefix. */ | |
05c0465e | 535 | |
af2d9bee | 536 | static bool |
05c0465e SDJ |
537 | stap_is_register_prefix (struct gdbarch *gdbarch, const char *s, |
538 | const char **r) | |
539 | { | |
540 | const char *const *t = gdbarch_stap_register_prefixes (gdbarch); | |
541 | ||
542 | return stap_is_generic_prefix (gdbarch, s, r, t); | |
543 | } | |
544 | ||
af2d9bee | 545 | /* Return true if S points to a register indirection prefix, false |
05c0465e SDJ |
546 | otherwise. For a description of the arguments, look at |
547 | stap_is_generic_prefix. */ | |
548 | ||
af2d9bee | 549 | static bool |
05c0465e SDJ |
550 | stap_is_register_indirection_prefix (struct gdbarch *gdbarch, const char *s, |
551 | const char **r) | |
552 | { | |
553 | const char *const *t = gdbarch_stap_register_indirection_prefixes (gdbarch); | |
554 | ||
555 | return stap_is_generic_prefix (gdbarch, s, r, t); | |
556 | } | |
557 | ||
af2d9bee SDJ |
558 | /* Return true if S points to an integer prefix, false otherwise. For |
559 | a description of the arguments, look at stap_is_generic_prefix. | |
05c0465e SDJ |
560 | |
561 | This function takes care of analyzing whether we are dealing with | |
562 | an expected integer prefix, or, if there is no integer prefix to be | |
563 | expected, whether we are dealing with a digit. It does a | |
564 | case-insensitive match. */ | |
565 | ||
af2d9bee | 566 | static bool |
05c0465e SDJ |
567 | stap_is_integer_prefix (struct gdbarch *gdbarch, const char *s, |
568 | const char **r) | |
569 | { | |
570 | const char *const *t = gdbarch_stap_integer_prefixes (gdbarch); | |
571 | const char *const *p; | |
572 | ||
573 | if (t == NULL) | |
574 | { | |
575 | /* A NULL value here means that integers do not have a prefix. | |
576 | We just check for a digit then. */ | |
577 | if (r != NULL) | |
578 | *r = ""; | |
579 | ||
af2d9bee | 580 | return isdigit (*s) > 0; |
05c0465e SDJ |
581 | } |
582 | ||
583 | for (p = t; *p != NULL; ++p) | |
584 | { | |
585 | size_t len = strlen (*p); | |
586 | ||
587 | if ((len == 0 && isdigit (*s)) | |
588 | || (len > 0 && strncasecmp (s, *p, len) == 0)) | |
589 | { | |
590 | /* Integers may or may not have a prefix. The "len == 0" | |
591 | check covers the case when integers do not have a prefix | |
592 | (therefore, we just check if we have a digit). The call | |
593 | to "strncasecmp" covers the case when they have a | |
594 | prefix. */ | |
595 | if (r != NULL) | |
596 | *r = *p; | |
597 | ||
af2d9bee | 598 | return true; |
05c0465e SDJ |
599 | } |
600 | } | |
601 | ||
af2d9bee | 602 | return false; |
05c0465e SDJ |
603 | } |
604 | ||
605 | /* Helper function to check for a generic list of suffixes. If we are | |
606 | not expecting any suffixes, then it just returns 1. If we are | |
af2d9bee SDJ |
607 | expecting at least one suffix, then it returns true if a suffix has |
608 | been found, false otherwise. GDBARCH is the current gdbarch being | |
05c0465e SDJ |
609 | used. S is the expression being analyzed. If R is not NULL, it |
610 | will be used to return the found suffix. SUFFIXES is the list of | |
611 | expected suffixes. This function does a case-insensitive | |
612 | match. */ | |
613 | ||
af2d9bee | 614 | static bool |
05c0465e SDJ |
615 | stap_generic_check_suffix (struct gdbarch *gdbarch, const char *s, |
616 | const char **r, const char *const *suffixes) | |
617 | { | |
618 | const char *const *p; | |
af2d9bee | 619 | bool found = false; |
05c0465e SDJ |
620 | |
621 | if (suffixes == NULL) | |
622 | { | |
623 | if (r != NULL) | |
624 | *r = ""; | |
625 | ||
af2d9bee | 626 | return true; |
05c0465e SDJ |
627 | } |
628 | ||
629 | for (p = suffixes; *p != NULL; ++p) | |
630 | if (strncasecmp (s, *p, strlen (*p)) == 0) | |
631 | { | |
632 | if (r != NULL) | |
633 | *r = *p; | |
634 | ||
af2d9bee | 635 | found = true; |
05c0465e SDJ |
636 | break; |
637 | } | |
638 | ||
639 | return found; | |
640 | } | |
641 | ||
af2d9bee SDJ |
642 | /* Return true if S points to an integer suffix, false otherwise. For |
643 | a description of the arguments, look at | |
05c0465e SDJ |
644 | stap_generic_check_suffix. */ |
645 | ||
af2d9bee | 646 | static bool |
05c0465e SDJ |
647 | stap_check_integer_suffix (struct gdbarch *gdbarch, const char *s, |
648 | const char **r) | |
649 | { | |
650 | const char *const *p = gdbarch_stap_integer_suffixes (gdbarch); | |
651 | ||
652 | return stap_generic_check_suffix (gdbarch, s, r, p); | |
653 | } | |
654 | ||
af2d9bee SDJ |
655 | /* Return true if S points to a register suffix, false otherwise. For |
656 | a description of the arguments, look at | |
05c0465e SDJ |
657 | stap_generic_check_suffix. */ |
658 | ||
af2d9bee | 659 | static bool |
05c0465e SDJ |
660 | stap_check_register_suffix (struct gdbarch *gdbarch, const char *s, |
661 | const char **r) | |
662 | { | |
663 | const char *const *p = gdbarch_stap_register_suffixes (gdbarch); | |
664 | ||
665 | return stap_generic_check_suffix (gdbarch, s, r, p); | |
666 | } | |
667 | ||
af2d9bee | 668 | /* Return true if S points to a register indirection suffix, false |
05c0465e SDJ |
669 | otherwise. For a description of the arguments, look at |
670 | stap_generic_check_suffix. */ | |
671 | ||
af2d9bee | 672 | static bool |
05c0465e SDJ |
673 | stap_check_register_indirection_suffix (struct gdbarch *gdbarch, const char *s, |
674 | const char **r) | |
675 | { | |
676 | const char *const *p = gdbarch_stap_register_indirection_suffixes (gdbarch); | |
677 | ||
678 | return stap_generic_check_suffix (gdbarch, s, r, p); | |
679 | } | |
680 | ||
55aa24fb SDJ |
681 | /* Function responsible for parsing a register operand according to |
682 | SystemTap parlance. Assuming: | |
683 | ||
684 | RP = register prefix | |
685 | RS = register suffix | |
686 | RIP = register indirection prefix | |
687 | RIS = register indirection suffix | |
688 | ||
689 | Then a register operand can be: | |
690 | ||
691 | [RIP] [RP] REGISTER [RS] [RIS] | |
692 | ||
693 | This function takes care of a register's indirection, displacement and | |
694 | direct access. It also takes into consideration the fact that some | |
695 | registers are named differently inside and outside GDB, e.g., PPC's | |
696 | general-purpose registers are represented by integers in the assembly | |
697 | language (e.g., `15' is the 15th general-purpose register), but inside | |
698 | GDB they have a prefix (the letter `r') appended. */ | |
699 | ||
4c5e7a93 | 700 | static expr::operation_up |
55aa24fb SDJ |
701 | stap_parse_register_operand (struct stap_parse_info *p) |
702 | { | |
703 | /* Simple flag to indicate whether we have seen a minus signal before | |
704 | certain number. */ | |
af2d9bee | 705 | bool got_minus = false; |
4c5e7a93 | 706 | /* Flag to indicate whether this register access is being |
55aa24fb | 707 | indirected. */ |
af2d9bee | 708 | bool indirect_p = false; |
55aa24fb | 709 | struct gdbarch *gdbarch = p->gdbarch; |
55aa24fb SDJ |
710 | /* Variables used to extract the register name from the probe's |
711 | argument. */ | |
712 | const char *start; | |
55aa24fb | 713 | const char *gdb_reg_prefix = gdbarch_stap_gdb_register_prefix (gdbarch); |
55aa24fb | 714 | const char *gdb_reg_suffix = gdbarch_stap_gdb_register_suffix (gdbarch); |
05c0465e SDJ |
715 | const char *reg_prefix; |
716 | const char *reg_ind_prefix; | |
717 | const char *reg_suffix; | |
718 | const char *reg_ind_suffix; | |
55aa24fb | 719 | |
4c5e7a93 TT |
720 | using namespace expr; |
721 | ||
55aa24fb SDJ |
722 | /* Checking for a displacement argument. */ |
723 | if (*p->arg == '+') | |
724 | { | |
725 | /* If it's a plus sign, we don't need to do anything, just advance the | |
726 | pointer. */ | |
727 | ++p->arg; | |
728 | } | |
f1bb75ab | 729 | else if (*p->arg == '-') |
55aa24fb | 730 | { |
af2d9bee | 731 | got_minus = true; |
55aa24fb SDJ |
732 | ++p->arg; |
733 | } | |
734 | ||
4c5e7a93 TT |
735 | struct type *long_type = builtin_type (gdbarch)->builtin_long; |
736 | operation_up disp_op; | |
55aa24fb SDJ |
737 | if (isdigit (*p->arg)) |
738 | { | |
739 | /* The value of the displacement. */ | |
740 | long displacement; | |
a0bcdaa7 | 741 | char *endp; |
55aa24fb | 742 | |
a0bcdaa7 PA |
743 | displacement = strtol (p->arg, &endp, 10); |
744 | p->arg = endp; | |
55aa24fb SDJ |
745 | |
746 | /* Generating the expression for the displacement. */ | |
55aa24fb | 747 | if (got_minus) |
4c5e7a93 TT |
748 | displacement = -displacement; |
749 | disp_op = make_operation<long_const_operation> (long_type, displacement); | |
55aa24fb SDJ |
750 | } |
751 | ||
752 | /* Getting rid of register indirection prefix. */ | |
05c0465e | 753 | if (stap_is_register_indirection_prefix (gdbarch, p->arg, ®_ind_prefix)) |
55aa24fb | 754 | { |
af2d9bee | 755 | indirect_p = true; |
05c0465e | 756 | p->arg += strlen (reg_ind_prefix); |
55aa24fb SDJ |
757 | } |
758 | ||
4c5e7a93 | 759 | if (disp_op != nullptr && !indirect_p) |
55aa24fb SDJ |
760 | error (_("Invalid register displacement syntax on expression `%s'."), |
761 | p->saved_arg); | |
762 | ||
763 | /* Getting rid of register prefix. */ | |
05c0465e SDJ |
764 | if (stap_is_register_prefix (gdbarch, p->arg, ®_prefix)) |
765 | p->arg += strlen (reg_prefix); | |
55aa24fb SDJ |
766 | |
767 | /* Now we should have only the register name. Let's extract it and get | |
768 | the associated number. */ | |
769 | start = p->arg; | |
770 | ||
771 | /* We assume the register name is composed by letters and numbers. */ | |
772 | while (isalnum (*p->arg)) | |
773 | ++p->arg; | |
774 | ||
677052f2 | 775 | std::string regname (start, p->arg - start); |
55aa24fb SDJ |
776 | |
777 | /* We only add the GDB's register prefix/suffix if we are dealing with | |
778 | a numeric register. */ | |
677052f2 | 779 | if (isdigit (*start)) |
55aa24fb | 780 | { |
677052f2 SDJ |
781 | if (gdb_reg_prefix != NULL) |
782 | regname = gdb_reg_prefix + regname; | |
55aa24fb | 783 | |
677052f2 SDJ |
784 | if (gdb_reg_suffix != NULL) |
785 | regname += gdb_reg_suffix; | |
55aa24fb | 786 | } |
55aa24fb | 787 | |
7d7571f0 SDJ |
788 | int regnum = user_reg_map_name_to_regnum (gdbarch, regname.c_str (), |
789 | regname.size ()); | |
790 | ||
55aa24fb | 791 | /* Is this a valid register name? */ |
7d7571f0 | 792 | if (regnum == -1) |
55aa24fb | 793 | error (_("Invalid register name `%s' on expression `%s'."), |
677052f2 | 794 | regname.c_str (), p->saved_arg); |
55aa24fb | 795 | |
7d7571f0 SDJ |
796 | /* Check if there's any special treatment that the arch-specific |
797 | code would like to perform on the register name. */ | |
798 | if (gdbarch_stap_adjust_register_p (gdbarch)) | |
799 | { | |
6b78c3f8 AB |
800 | std::string newregname |
801 | = gdbarch_stap_adjust_register (gdbarch, p, regname, regnum); | |
7d7571f0 | 802 | |
6b78c3f8 | 803 | if (regname != newregname) |
7d7571f0 SDJ |
804 | { |
805 | /* This is just a check we perform to make sure that the | |
806 | arch-dependent code has provided us with a valid | |
807 | register name. */ | |
6b78c3f8 AB |
808 | regnum = user_reg_map_name_to_regnum (gdbarch, newregname.c_str (), |
809 | newregname.size ()); | |
7d7571f0 SDJ |
810 | |
811 | if (regnum == -1) | |
812 | internal_error (__FILE__, __LINE__, | |
813 | _("Invalid register name '%s' after replacing it" | |
814 | " (previous name was '%s')"), | |
6b78c3f8 AB |
815 | newregname.c_str (), regname.c_str ()); |
816 | ||
4c5e7a93 | 817 | regname = std::move (newregname); |
7d7571f0 SDJ |
818 | } |
819 | } | |
820 | ||
4c5e7a93 | 821 | operation_up reg = make_operation<register_operation> (std::move (regname)); |
55aa24fb SDJ |
822 | |
823 | if (indirect_p) | |
824 | { | |
4c5e7a93 TT |
825 | if (disp_op != nullptr) |
826 | reg = make_operation<add_operation> (std::move (disp_op), | |
827 | std::move (reg)); | |
55aa24fb SDJ |
828 | |
829 | /* Casting to the expected type. */ | |
4c5e7a93 TT |
830 | struct type *arg_ptr_type = lookup_pointer_type (p->arg_type); |
831 | reg = make_operation<unop_cast_operation> (std::move (reg), | |
832 | arg_ptr_type); | |
833 | reg = make_operation<unop_ind_operation> (std::move (reg)); | |
55aa24fb SDJ |
834 | } |
835 | ||
836 | /* Getting rid of the register name suffix. */ | |
05c0465e SDJ |
837 | if (stap_check_register_suffix (gdbarch, p->arg, ®_suffix)) |
838 | p->arg += strlen (reg_suffix); | |
839 | else | |
840 | error (_("Missing register name suffix on expression `%s'."), | |
841 | p->saved_arg); | |
55aa24fb SDJ |
842 | |
843 | /* Getting rid of the register indirection suffix. */ | |
05c0465e | 844 | if (indirect_p) |
55aa24fb | 845 | { |
05c0465e SDJ |
846 | if (stap_check_register_indirection_suffix (gdbarch, p->arg, |
847 | ®_ind_suffix)) | |
848 | p->arg += strlen (reg_ind_suffix); | |
849 | else | |
850 | error (_("Missing indirection suffix on expression `%s'."), | |
851 | p->saved_arg); | |
55aa24fb | 852 | } |
4c5e7a93 TT |
853 | |
854 | return reg; | |
55aa24fb SDJ |
855 | } |
856 | ||
857 | /* This function is responsible for parsing a single operand. | |
858 | ||
859 | A single operand can be: | |
860 | ||
861 | - an unary operation (e.g., `-5', `~2', or even with subexpressions | |
dda83cd7 | 862 | like `-(2 + 1)') |
55aa24fb | 863 | - a register displacement, which will be treated as a register |
dda83cd7 | 864 | operand (e.g., `-4(%eax)' on x86) |
55aa24fb SDJ |
865 | - a numeric constant, or |
866 | - a register operand (see function `stap_parse_register_operand') | |
867 | ||
868 | The function also calls special-handling functions to deal with | |
869 | unrecognized operands, allowing arch-specific parsers to be | |
870 | created. */ | |
871 | ||
4c5e7a93 | 872 | static expr::operation_up |
55aa24fb SDJ |
873 | stap_parse_single_operand (struct stap_parse_info *p) |
874 | { | |
875 | struct gdbarch *gdbarch = p->gdbarch; | |
05c0465e | 876 | const char *int_prefix = NULL; |
55aa24fb | 877 | |
4c5e7a93 TT |
878 | using namespace expr; |
879 | ||
55aa24fb | 880 | /* We first try to parse this token as a "special token". */ |
4c5e7a93 | 881 | if (gdbarch_stap_parse_special_token_p (gdbarch)) |
f1bb75ab | 882 | { |
4c5e7a93 TT |
883 | operation_up token = gdbarch_stap_parse_special_token (gdbarch, p); |
884 | if (token != nullptr) | |
885 | return token; | |
f1bb75ab | 886 | } |
55aa24fb | 887 | |
4c5e7a93 TT |
888 | struct type *long_type = builtin_type (gdbarch)->builtin_long; |
889 | operation_up result; | |
6f52fdf4 | 890 | if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' || *p->arg == '!') |
55aa24fb SDJ |
891 | { |
892 | char c = *p->arg; | |
55aa24fb SDJ |
893 | /* We use this variable to do a lookahead. */ |
894 | const char *tmp = p->arg; | |
af2d9bee | 895 | bool has_digit = false; |
55aa24fb | 896 | |
97c2dca0 | 897 | /* Skipping signal. */ |
55aa24fb SDJ |
898 | ++tmp; |
899 | ||
900 | /* This is an unary operation. Here is a list of allowed tokens | |
901 | here: | |
902 | ||
903 | - numeric literal; | |
904 | - number (from register displacement) | |
905 | - subexpression (beginning with `(') | |
906 | ||
907 | We handle the register displacement here, and the other cases | |
908 | recursively. */ | |
909 | if (p->inside_paren_p) | |
f1735a53 | 910 | tmp = skip_spaces (tmp); |
55aa24fb | 911 | |
474ca4f6 | 912 | while (isdigit (*tmp)) |
a0bcdaa7 | 913 | { |
474ca4f6 SDJ |
914 | /* We skip the digit here because we are only interested in |
915 | knowing what kind of unary operation this is. The digit | |
916 | will be handled by one of the functions that will be | |
917 | called below ('stap_parse_argument_conditionally' or | |
918 | 'stap_parse_register_operand'). */ | |
919 | ++tmp; | |
af2d9bee | 920 | has_digit = true; |
a0bcdaa7 | 921 | } |
55aa24fb | 922 | |
474ca4f6 SDJ |
923 | if (has_digit && stap_is_register_indirection_prefix (gdbarch, tmp, |
924 | NULL)) | |
55aa24fb SDJ |
925 | { |
926 | /* If we are here, it means it is a displacement. The only | |
927 | operations allowed here are `-' and `+'. */ | |
f1bb75ab | 928 | if (c != '-' && c != '+') |
55aa24fb SDJ |
929 | error (_("Invalid operator `%c' for register displacement " |
930 | "on expression `%s'."), c, p->saved_arg); | |
931 | ||
4c5e7a93 | 932 | result = stap_parse_register_operand (p); |
55aa24fb | 933 | } |
474ca4f6 SDJ |
934 | else |
935 | { | |
936 | /* This is not a displacement. We skip the operator, and | |
937 | deal with it when the recursion returns. */ | |
938 | ++p->arg; | |
4c5e7a93 | 939 | result = stap_parse_argument_conditionally (p); |
474ca4f6 | 940 | if (c == '-') |
4c5e7a93 | 941 | result = make_operation<unary_neg_operation> (std::move (result)); |
474ca4f6 | 942 | else if (c == '~') |
4c5e7a93 TT |
943 | result = (make_operation<unary_complement_operation> |
944 | (std::move (result))); | |
6f52fdf4 | 945 | else if (c == '!') |
4c5e7a93 TT |
946 | result = (make_operation<unary_logical_not_operation> |
947 | (std::move (result))); | |
474ca4f6 | 948 | } |
55aa24fb SDJ |
949 | } |
950 | else if (isdigit (*p->arg)) | |
951 | { | |
952 | /* A temporary variable, needed for lookahead. */ | |
953 | const char *tmp = p->arg; | |
a0bcdaa7 | 954 | char *endp; |
55aa24fb SDJ |
955 | long number; |
956 | ||
05c0465e SDJ |
957 | /* We can be dealing with a numeric constant, or with a register |
958 | displacement. */ | |
a0bcdaa7 PA |
959 | number = strtol (tmp, &endp, 10); |
960 | tmp = endp; | |
55aa24fb SDJ |
961 | |
962 | if (p->inside_paren_p) | |
f1735a53 | 963 | tmp = skip_spaces (tmp); |
05c0465e SDJ |
964 | |
965 | /* If "stap_is_integer_prefix" returns true, it means we can | |
966 | accept integers without a prefix here. But we also need to | |
967 | check whether the next token (i.e., "tmp") is not a register | |
968 | indirection prefix. */ | |
969 | if (stap_is_integer_prefix (gdbarch, p->arg, NULL) | |
970 | && !stap_is_register_indirection_prefix (gdbarch, tmp, NULL)) | |
55aa24fb | 971 | { |
05c0465e SDJ |
972 | const char *int_suffix; |
973 | ||
55aa24fb | 974 | /* We are dealing with a numeric constant. */ |
4c5e7a93 | 975 | result = make_operation<long_const_operation> (long_type, number); |
55aa24fb SDJ |
976 | |
977 | p->arg = tmp; | |
978 | ||
05c0465e SDJ |
979 | if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix)) |
980 | p->arg += strlen (int_suffix); | |
981 | else | |
982 | error (_("Invalid constant suffix on expression `%s'."), | |
983 | p->saved_arg); | |
55aa24fb | 984 | } |
05c0465e | 985 | else if (stap_is_register_indirection_prefix (gdbarch, tmp, NULL)) |
4c5e7a93 | 986 | result = stap_parse_register_operand (p); |
55aa24fb SDJ |
987 | else |
988 | error (_("Unknown numeric token on expression `%s'."), | |
989 | p->saved_arg); | |
990 | } | |
05c0465e | 991 | else if (stap_is_integer_prefix (gdbarch, p->arg, &int_prefix)) |
55aa24fb SDJ |
992 | { |
993 | /* We are dealing with a numeric constant. */ | |
994 | long number; | |
a0bcdaa7 | 995 | char *endp; |
05c0465e | 996 | const char *int_suffix; |
55aa24fb | 997 | |
05c0465e | 998 | p->arg += strlen (int_prefix); |
a0bcdaa7 PA |
999 | number = strtol (p->arg, &endp, 10); |
1000 | p->arg = endp; | |
55aa24fb | 1001 | |
4c5e7a93 | 1002 | result = make_operation<long_const_operation> (long_type, number); |
55aa24fb | 1003 | |
05c0465e SDJ |
1004 | if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix)) |
1005 | p->arg += strlen (int_suffix); | |
1006 | else | |
1007 | error (_("Invalid constant suffix on expression `%s'."), | |
1008 | p->saved_arg); | |
55aa24fb | 1009 | } |
05c0465e SDJ |
1010 | else if (stap_is_register_prefix (gdbarch, p->arg, NULL) |
1011 | || stap_is_register_indirection_prefix (gdbarch, p->arg, NULL)) | |
4c5e7a93 | 1012 | result = stap_parse_register_operand (p); |
55aa24fb SDJ |
1013 | else |
1014 | error (_("Operator `%c' not recognized on expression `%s'."), | |
1015 | *p->arg, p->saved_arg); | |
4c5e7a93 TT |
1016 | |
1017 | return result; | |
55aa24fb SDJ |
1018 | } |
1019 | ||
1020 | /* This function parses an argument conditionally, based on single or | |
1021 | non-single operands. A non-single operand would be a parenthesized | |
1022 | expression (e.g., `(2 + 1)'), and a single operand is anything that | |
1023 | starts with `-', `~', `+' (i.e., unary operators), a digit, or | |
1024 | something recognized by `gdbarch_stap_is_single_operand'. */ | |
1025 | ||
4c5e7a93 | 1026 | static expr::operation_up |
55aa24fb SDJ |
1027 | stap_parse_argument_conditionally (struct stap_parse_info *p) |
1028 | { | |
97c2dca0 SDJ |
1029 | gdb_assert (gdbarch_stap_is_single_operand_p (p->gdbarch)); |
1030 | ||
4c5e7a93 | 1031 | expr::operation_up result; |
6f52fdf4 | 1032 | if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' || *p->arg == '!' |
55aa24fb SDJ |
1033 | || isdigit (*p->arg) |
1034 | || gdbarch_stap_is_single_operand (p->gdbarch, p->arg)) | |
4c5e7a93 | 1035 | result = stap_parse_single_operand (p); |
55aa24fb SDJ |
1036 | else if (*p->arg == '(') |
1037 | { | |
1038 | /* We are dealing with a parenthesized operand. It means we | |
1039 | have to parse it as it was a separate expression, without | |
1040 | left-side or precedence. */ | |
1041 | ++p->arg; | |
f1735a53 | 1042 | p->arg = skip_spaces (p->arg); |
55aa24fb SDJ |
1043 | ++p->inside_paren_p; |
1044 | ||
4c5e7a93 | 1045 | result = stap_parse_argument_1 (p, {}, STAP_OPERAND_PREC_NONE); |
55aa24fb | 1046 | |
6f52fdf4 | 1047 | p->arg = skip_spaces (p->arg); |
55aa24fb | 1048 | if (*p->arg != ')') |
9bb305b3 | 1049 | error (_("Missing close-parenthesis on expression `%s'."), |
55aa24fb SDJ |
1050 | p->saved_arg); |
1051 | ||
6f52fdf4 | 1052 | --p->inside_paren_p; |
55aa24fb SDJ |
1053 | ++p->arg; |
1054 | if (p->inside_paren_p) | |
f1735a53 | 1055 | p->arg = skip_spaces (p->arg); |
55aa24fb SDJ |
1056 | } |
1057 | else | |
1058 | error (_("Cannot parse expression `%s'."), p->saved_arg); | |
4c5e7a93 TT |
1059 | |
1060 | return result; | |
55aa24fb SDJ |
1061 | } |
1062 | ||
1063 | /* Helper function for `stap_parse_argument'. Please, see its comments to | |
1064 | better understand what this function does. */ | |
1065 | ||
4c5e7a93 TT |
1066 | static expr::operation_up ATTRIBUTE_UNUSED_RESULT |
1067 | stap_parse_argument_1 (struct stap_parse_info *p, | |
1068 | expr::operation_up &&lhs_in, | |
55aa24fb SDJ |
1069 | enum stap_operand_prec prec) |
1070 | { | |
1071 | /* This is an operator-precedence parser. | |
1072 | ||
1073 | We work with left- and right-sides of expressions, and | |
1074 | parse them depending on the precedence of the operators | |
1075 | we find. */ | |
1076 | ||
97c2dca0 SDJ |
1077 | gdb_assert (p->arg != NULL); |
1078 | ||
55aa24fb | 1079 | if (p->inside_paren_p) |
f1735a53 | 1080 | p->arg = skip_spaces (p->arg); |
55aa24fb | 1081 | |
4c5e7a93 TT |
1082 | using namespace expr; |
1083 | operation_up lhs = std::move (lhs_in); | |
1084 | if (lhs == nullptr) | |
55aa24fb SDJ |
1085 | { |
1086 | /* We were called without a left-side, either because this is the | |
1087 | first call, or because we were called to parse a parenthesized | |
1088 | expression. It doesn't really matter; we have to parse the | |
1089 | left-side in order to continue the process. */ | |
4c5e7a93 | 1090 | lhs = stap_parse_argument_conditionally (p); |
55aa24fb SDJ |
1091 | } |
1092 | ||
6f52fdf4 SDJ |
1093 | if (p->inside_paren_p) |
1094 | p->arg = skip_spaces (p->arg); | |
1095 | ||
55aa24fb SDJ |
1096 | /* Start to parse the right-side, and to "join" left and right sides |
1097 | depending on the operation specified. | |
1098 | ||
1099 | This loop shall continue until we run out of characters in the input, | |
1100 | or until we find a close-parenthesis, which means that we've reached | |
1101 | the end of a sub-expression. */ | |
97c2dca0 | 1102 | while (*p->arg != '\0' && *p->arg != ')' && !isspace (*p->arg)) |
55aa24fb SDJ |
1103 | { |
1104 | const char *tmp_exp_buf; | |
1105 | enum exp_opcode opcode; | |
1106 | enum stap_operand_prec cur_prec; | |
1107 | ||
fcf57f19 | 1108 | if (!stap_is_operator (p->arg)) |
55aa24fb SDJ |
1109 | error (_("Invalid operator `%c' on expression `%s'."), *p->arg, |
1110 | p->saved_arg); | |
1111 | ||
1112 | /* We have to save the current value of the expression buffer because | |
1113 | the `stap_get_opcode' modifies it in order to get the current | |
1114 | operator. If this operator's precedence is lower than PREC, we | |
1115 | should return and not advance the expression buffer pointer. */ | |
1116 | tmp_exp_buf = p->arg; | |
fcf57f19 | 1117 | opcode = stap_get_opcode (&tmp_exp_buf); |
55aa24fb SDJ |
1118 | |
1119 | cur_prec = stap_get_operator_prec (opcode); | |
1120 | if (cur_prec < prec) | |
1121 | { | |
1122 | /* If the precedence of the operator that we are seeing now is | |
1123 | lower than the precedence of the first operator seen before | |
1124 | this parsing process began, it means we should stop parsing | |
1125 | and return. */ | |
1126 | break; | |
1127 | } | |
1128 | ||
1129 | p->arg = tmp_exp_buf; | |
1130 | if (p->inside_paren_p) | |
f1735a53 | 1131 | p->arg = skip_spaces (p->arg); |
55aa24fb | 1132 | |
6f52fdf4 SDJ |
1133 | /* Parse the right-side of the expression. |
1134 | ||
1135 | We save whether the right-side is a parenthesized | |
1136 | subexpression because, if it is, we will have to finish | |
1137 | processing this part of the expression before continuing. */ | |
1138 | bool paren_subexp = *p->arg == '('; | |
1139 | ||
4c5e7a93 | 1140 | operation_up rhs = stap_parse_argument_conditionally (p); |
6f52fdf4 SDJ |
1141 | if (p->inside_paren_p) |
1142 | p->arg = skip_spaces (p->arg); | |
1143 | if (paren_subexp) | |
1144 | { | |
4c5e7a93 | 1145 | lhs = stap_make_binop (opcode, std::move (lhs), std::move (rhs)); |
6f52fdf4 SDJ |
1146 | continue; |
1147 | } | |
55aa24fb SDJ |
1148 | |
1149 | /* While we still have operators, try to parse another | |
1150 | right-side, but using the current right-side as a left-side. */ | |
97c2dca0 | 1151 | while (*p->arg != '\0' && stap_is_operator (p->arg)) |
55aa24fb SDJ |
1152 | { |
1153 | enum exp_opcode lookahead_opcode; | |
1154 | enum stap_operand_prec lookahead_prec; | |
1155 | ||
1156 | /* Saving the current expression buffer position. The explanation | |
1157 | is the same as above. */ | |
1158 | tmp_exp_buf = p->arg; | |
fcf57f19 | 1159 | lookahead_opcode = stap_get_opcode (&tmp_exp_buf); |
55aa24fb SDJ |
1160 | lookahead_prec = stap_get_operator_prec (lookahead_opcode); |
1161 | ||
1162 | if (lookahead_prec <= prec) | |
1163 | { | |
1164 | /* If we are dealing with an operator whose precedence is lower | |
1165 | than the first one, just abandon the attempt. */ | |
1166 | break; | |
1167 | } | |
1168 | ||
4c5e7a93 TT |
1169 | /* Parse the right-side of the expression, using the current |
1170 | right-hand-side as the left-hand-side of the new | |
1171 | subexpression. */ | |
1172 | rhs = stap_parse_argument_1 (p, std::move (rhs), lookahead_prec); | |
6f52fdf4 SDJ |
1173 | if (p->inside_paren_p) |
1174 | p->arg = skip_spaces (p->arg); | |
55aa24fb SDJ |
1175 | } |
1176 | ||
4c5e7a93 | 1177 | lhs = stap_make_binop (opcode, std::move (lhs), std::move (rhs)); |
55aa24fb | 1178 | } |
4c5e7a93 TT |
1179 | |
1180 | return lhs; | |
55aa24fb SDJ |
1181 | } |
1182 | ||
1183 | /* Parse a probe's argument. | |
1184 | ||
1185 | Assuming that: | |
1186 | ||
1187 | LP = literal integer prefix | |
1188 | LS = literal integer suffix | |
1189 | ||
1190 | RP = register prefix | |
1191 | RS = register suffix | |
1192 | ||
1193 | RIP = register indirection prefix | |
1194 | RIS = register indirection suffix | |
1195 | ||
1196 | This routine assumes that arguments' tokens are of the form: | |
1197 | ||
1198 | - [LP] NUMBER [LS] | |
1199 | - [RP] REGISTER [RS] | |
1200 | - [RIP] [RP] REGISTER [RS] [RIS] | |
1201 | - If we find a number without LP, we try to parse it as a literal integer | |
1202 | constant (if LP == NULL), or as a register displacement. | |
1203 | - We count parenthesis, and only skip whitespaces if we are inside them. | |
1204 | - If we find an operator, we skip it. | |
1205 | ||
1206 | This function can also call a special function that will try to match | |
0e9ae10f SDJ |
1207 | unknown tokens. It will return the expression_up generated from |
1208 | parsing the argument. */ | |
55aa24fb | 1209 | |
0e9ae10f | 1210 | static expression_up |
55aa24fb SDJ |
1211 | stap_parse_argument (const char **arg, struct type *atype, |
1212 | struct gdbarch *gdbarch) | |
1213 | { | |
55aa24fb | 1214 | /* We need to initialize the expression buffer, in order to begin |
f7088df3 SDJ |
1215 | our parsing efforts. We use language_c here because we may need |
1216 | to do pointer arithmetics. */ | |
1201a264 | 1217 | struct stap_parse_info p (*arg, atype, language_def (language_c), |
e9d9f57e | 1218 | gdbarch); |
55aa24fb | 1219 | |
4c5e7a93 TT |
1220 | using namespace expr; |
1221 | operation_up result = stap_parse_argument_1 (&p, {}, STAP_OPERAND_PREC_NONE); | |
55aa24fb | 1222 | |
55aa24fb SDJ |
1223 | gdb_assert (p.inside_paren_p == 0); |
1224 | ||
1225 | /* Casting the final expression to the appropriate type. */ | |
4c5e7a93 TT |
1226 | result = make_operation<unop_cast_operation> (std::move (result), atype); |
1227 | p.pstate.set_operation (std::move (result)); | |
55aa24fb | 1228 | |
f1735a53 | 1229 | p.arg = skip_spaces (p.arg); |
55aa24fb SDJ |
1230 | *arg = p.arg; |
1231 | ||
e9d9f57e | 1232 | return p.pstate.release (); |
55aa24fb SDJ |
1233 | } |
1234 | ||
0e9ae10f | 1235 | /* Implementation of 'parse_arguments' method. */ |
55aa24fb | 1236 | |
0e9ae10f SDJ |
1237 | void |
1238 | stap_probe::parse_arguments (struct gdbarch *gdbarch) | |
55aa24fb SDJ |
1239 | { |
1240 | const char *cur; | |
55aa24fb | 1241 | |
0e9ae10f SDJ |
1242 | gdb_assert (!m_have_parsed_args); |
1243 | cur = m_unparsed_args_text; | |
1244 | m_have_parsed_args = true; | |
55aa24fb | 1245 | |
97c2dca0 | 1246 | if (cur == NULL || *cur == '\0' || *cur == ':') |
55aa24fb SDJ |
1247 | return; |
1248 | ||
97c2dca0 | 1249 | while (*cur != '\0') |
55aa24fb | 1250 | { |
0e9ae10f SDJ |
1251 | enum stap_arg_bitness bitness; |
1252 | bool got_minus = false; | |
55aa24fb SDJ |
1253 | |
1254 | /* We expect to find something like: | |
1255 | ||
1256 | N@OP | |
1257 | ||
30a1e6cc | 1258 | Where `N' can be [+,-][1,2,4,8]. This is not mandatory, so |
55aa24fb SDJ |
1259 | we check it here. If we don't find it, go to the next |
1260 | state. */ | |
f33da99a SDJ |
1261 | if ((cur[0] == '-' && isdigit (cur[1]) && cur[2] == '@') |
1262 | || (isdigit (cur[0]) && cur[1] == '@')) | |
55aa24fb SDJ |
1263 | { |
1264 | if (*cur == '-') | |
1265 | { | |
1266 | /* Discard the `-'. */ | |
1267 | ++cur; | |
0e9ae10f | 1268 | got_minus = true; |
55aa24fb SDJ |
1269 | } |
1270 | ||
30a1e6cc SDJ |
1271 | /* Defining the bitness. */ |
1272 | switch (*cur) | |
55aa24fb | 1273 | { |
30a1e6cc | 1274 | case '1': |
0e9ae10f SDJ |
1275 | bitness = (got_minus ? STAP_ARG_BITNESS_8BIT_SIGNED |
1276 | : STAP_ARG_BITNESS_8BIT_UNSIGNED); | |
30a1e6cc SDJ |
1277 | break; |
1278 | ||
1279 | case '2': | |
0e9ae10f SDJ |
1280 | bitness = (got_minus ? STAP_ARG_BITNESS_16BIT_SIGNED |
1281 | : STAP_ARG_BITNESS_16BIT_UNSIGNED); | |
30a1e6cc SDJ |
1282 | break; |
1283 | ||
1284 | case '4': | |
0e9ae10f SDJ |
1285 | bitness = (got_minus ? STAP_ARG_BITNESS_32BIT_SIGNED |
1286 | : STAP_ARG_BITNESS_32BIT_UNSIGNED); | |
30a1e6cc SDJ |
1287 | break; |
1288 | ||
1289 | case '8': | |
0e9ae10f SDJ |
1290 | bitness = (got_minus ? STAP_ARG_BITNESS_64BIT_SIGNED |
1291 | : STAP_ARG_BITNESS_64BIT_UNSIGNED); | |
30a1e6cc SDJ |
1292 | break; |
1293 | ||
1294 | default: | |
1295 | { | |
1296 | /* We have an error, because we don't expect anything | |
1297 | except 1, 2, 4 and 8. */ | |
1298 | warning (_("unrecognized bitness %s%c' for probe `%s'"), | |
0e9ae10f SDJ |
1299 | got_minus ? "`-" : "`", *cur, |
1300 | this->get_name ().c_str ()); | |
30a1e6cc SDJ |
1301 | return; |
1302 | } | |
55aa24fb | 1303 | } |
55aa24fb SDJ |
1304 | /* Discard the number and the `@' sign. */ |
1305 | cur += 2; | |
1306 | } | |
f33da99a | 1307 | else |
0e9ae10f | 1308 | bitness = STAP_ARG_BITNESS_UNDEFINED; |
f33da99a | 1309 | |
0e9ae10f SDJ |
1310 | struct type *atype |
1311 | = stap_get_expected_argument_type (gdbarch, bitness, | |
1312 | this->get_name ().c_str ()); | |
55aa24fb | 1313 | |
0e9ae10f | 1314 | expression_up expr = stap_parse_argument (&cur, atype, gdbarch); |
55aa24fb | 1315 | |
55aa24fb | 1316 | if (stap_expression_debug) |
0e9ae10f | 1317 | dump_prefix_expression (expr.get (), gdb_stdlog); |
55aa24fb | 1318 | |
0e9ae10f | 1319 | m_parsed_args.emplace_back (bitness, atype, std::move (expr)); |
55aa24fb SDJ |
1320 | |
1321 | /* Start it over again. */ | |
f1735a53 | 1322 | cur = skip_spaces (cur); |
55aa24fb SDJ |
1323 | } |
1324 | } | |
1325 | ||
685de8c2 SDJ |
1326 | /* Helper function to relocate an address. */ |
1327 | ||
1328 | static CORE_ADDR | |
1329 | relocate_address (CORE_ADDR address, struct objfile *objfile) | |
1330 | { | |
b3b3bada | 1331 | return address + objfile->data_section_offset (); |
685de8c2 SDJ |
1332 | } |
1333 | ||
0e9ae10f | 1334 | /* Implementation of the get_relocated_address method. */ |
729662a5 | 1335 | |
0e9ae10f SDJ |
1336 | CORE_ADDR |
1337 | stap_probe::get_relocated_address (struct objfile *objfile) | |
729662a5 | 1338 | { |
685de8c2 | 1339 | return relocate_address (this->get_address (), objfile); |
729662a5 TT |
1340 | } |
1341 | ||
55aa24fb SDJ |
1342 | /* Given PROBE, returns the number of arguments present in that probe's |
1343 | argument string. */ | |
1344 | ||
0e9ae10f | 1345 | unsigned |
fe01123e | 1346 | stap_probe::get_argument_count (struct gdbarch *gdbarch) |
55aa24fb | 1347 | { |
0e9ae10f | 1348 | if (!m_have_parsed_args) |
25f9533e | 1349 | { |
0e9ae10f SDJ |
1350 | if (this->can_evaluate_arguments ()) |
1351 | this->parse_arguments (gdbarch); | |
25f9533e SDJ |
1352 | else |
1353 | { | |
af2d9bee | 1354 | static bool have_warned_stap_incomplete = false; |
25f9533e SDJ |
1355 | |
1356 | if (!have_warned_stap_incomplete) | |
1357 | { | |
1358 | warning (_( | |
1359 | "The SystemTap SDT probe support is not fully implemented on this target;\n" | |
1360 | "you will not be able to inspect the arguments of the probes.\n" | |
1361 | "Please report a bug against GDB requesting a port to this target.")); | |
af2d9bee | 1362 | have_warned_stap_incomplete = true; |
25f9533e SDJ |
1363 | } |
1364 | ||
1365 | /* Marking the arguments as "already parsed". */ | |
0e9ae10f | 1366 | m_have_parsed_args = true; |
25f9533e SDJ |
1367 | } |
1368 | } | |
55aa24fb | 1369 | |
0e9ae10f SDJ |
1370 | gdb_assert (m_have_parsed_args); |
1371 | return m_parsed_args.size (); | |
55aa24fb SDJ |
1372 | } |
1373 | ||
af2d9bee SDJ |
1374 | /* Return true if OP is a valid operator inside a probe argument, or |
1375 | false otherwise. */ | |
55aa24fb | 1376 | |
af2d9bee | 1377 | static bool |
fcf57f19 | 1378 | stap_is_operator (const char *op) |
55aa24fb | 1379 | { |
af2d9bee | 1380 | bool ret = true; |
fcf57f19 SDJ |
1381 | |
1382 | switch (*op) | |
1383 | { | |
1384 | case '*': | |
1385 | case '/': | |
1386 | case '%': | |
1387 | case '^': | |
1388 | case '!': | |
1389 | case '+': | |
1390 | case '-': | |
1391 | case '<': | |
1392 | case '>': | |
1393 | case '|': | |
1394 | case '&': | |
1395 | break; | |
1396 | ||
1397 | case '=': | |
1398 | if (op[1] != '=') | |
af2d9bee | 1399 | ret = false; |
fcf57f19 SDJ |
1400 | break; |
1401 | ||
1402 | default: | |
1403 | /* We didn't find any operator. */ | |
af2d9bee | 1404 | ret = false; |
fcf57f19 SDJ |
1405 | } |
1406 | ||
1407 | return ret; | |
55aa24fb SDJ |
1408 | } |
1409 | ||
0e9ae10f | 1410 | /* Implement the `can_evaluate_arguments' method. */ |
f469e8ce | 1411 | |
0e9ae10f SDJ |
1412 | bool |
1413 | stap_probe::can_evaluate_arguments () const | |
25f9533e | 1414 | { |
0e9ae10f | 1415 | struct gdbarch *gdbarch = this->get_gdbarch (); |
25f9533e SDJ |
1416 | |
1417 | /* For SystemTap probes, we have to guarantee that the method | |
1418 | stap_is_single_operand is defined on gdbarch. If it is not, then it | |
1419 | means that argument evaluation is not implemented on this target. */ | |
1420 | return gdbarch_stap_is_single_operand_p (gdbarch); | |
1421 | } | |
1422 | ||
55aa24fb SDJ |
1423 | /* Evaluate the probe's argument N (indexed from 0), returning a value |
1424 | corresponding to it. Assertion is thrown if N does not exist. */ | |
1425 | ||
0e9ae10f SDJ |
1426 | struct value * |
1427 | stap_probe::evaluate_argument (unsigned n, struct frame_info *frame) | |
55aa24fb | 1428 | { |
55aa24fb | 1429 | struct stap_probe_arg *arg; |
0e9ae10f | 1430 | struct gdbarch *gdbarch = get_frame_arch (frame); |
55aa24fb | 1431 | |
0e9ae10f | 1432 | arg = this->get_arg_by_number (n, gdbarch); |
efd7ff14 | 1433 | return evaluate_expression (arg->aexpr.get (), arg->atype); |
55aa24fb SDJ |
1434 | } |
1435 | ||
1436 | /* Compile the probe's argument N (indexed from 0) to agent expression. | |
1437 | Assertion is thrown if N does not exist. */ | |
1438 | ||
0e9ae10f SDJ |
1439 | void |
1440 | stap_probe::compile_to_ax (struct agent_expr *expr, struct axs_value *value, | |
1441 | unsigned n) | |
55aa24fb | 1442 | { |
55aa24fb | 1443 | struct stap_probe_arg *arg; |
55aa24fb | 1444 | |
0e9ae10f | 1445 | arg = this->get_arg_by_number (n, expr->gdbarch); |
55aa24fb | 1446 | |
1eaebe02 | 1447 | arg->aexpr->op->generate_ax (arg->aexpr.get (), expr, value); |
55aa24fb SDJ |
1448 | |
1449 | require_rvalue (expr, value); | |
1450 | value->type = arg->atype; | |
1451 | } | |
55aa24fb SDJ |
1452 | \f |
1453 | ||
55aa24fb | 1454 | /* Set or clear a SystemTap semaphore. ADDRESS is the semaphore's |
0e9ae10f SDJ |
1455 | address. SET is zero if the semaphore should be cleared, or one if |
1456 | it should be set. This is a helper function for | |
1457 | 'stap_probe::set_semaphore' and 'stap_probe::clear_semaphore'. */ | |
55aa24fb SDJ |
1458 | |
1459 | static void | |
1460 | stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch) | |
1461 | { | |
1462 | gdb_byte bytes[sizeof (LONGEST)]; | |
1463 | /* The ABI specifies "unsigned short". */ | |
1464 | struct type *type = builtin_type (gdbarch)->builtin_unsigned_short; | |
1465 | ULONGEST value; | |
1466 | ||
55aa24fb SDJ |
1467 | /* Swallow errors. */ |
1468 | if (target_read_memory (address, bytes, TYPE_LENGTH (type)) != 0) | |
1469 | { | |
1470 | warning (_("Could not read the value of a SystemTap semaphore.")); | |
1471 | return; | |
1472 | } | |
1473 | ||
34877895 PJ |
1474 | enum bfd_endian byte_order = type_byte_order (type); |
1475 | value = extract_unsigned_integer (bytes, TYPE_LENGTH (type), byte_order); | |
55aa24fb SDJ |
1476 | /* Note that we explicitly don't worry about overflow or |
1477 | underflow. */ | |
1478 | if (set) | |
1479 | ++value; | |
1480 | else | |
1481 | --value; | |
1482 | ||
34877895 | 1483 | store_unsigned_integer (bytes, TYPE_LENGTH (type), byte_order, value); |
55aa24fb SDJ |
1484 | |
1485 | if (target_write_memory (address, bytes, TYPE_LENGTH (type)) != 0) | |
1486 | warning (_("Could not write the value of a SystemTap semaphore.")); | |
1487 | } | |
1488 | ||
0e9ae10f | 1489 | /* Implementation of the 'set_semaphore' method. |
55aa24fb | 1490 | |
0e9ae10f SDJ |
1491 | SystemTap semaphores act as reference counters, so calls to this |
1492 | function must be paired with calls to 'clear_semaphore'. | |
55aa24fb | 1493 | |
0e9ae10f SDJ |
1494 | This function and 'clear_semaphore' race with another tool |
1495 | changing the probes, but that is too rare to care. */ | |
1496 | ||
1497 | void | |
1498 | stap_probe::set_semaphore (struct objfile *objfile, struct gdbarch *gdbarch) | |
55aa24fb | 1499 | { |
7f0ae84c GB |
1500 | if (m_sem_addr == 0) |
1501 | return; | |
685de8c2 | 1502 | stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 1, gdbarch); |
0e9ae10f | 1503 | } |
55aa24fb | 1504 | |
0e9ae10f | 1505 | /* Implementation of the 'clear_semaphore' method. */ |
55aa24fb | 1506 | |
0e9ae10f SDJ |
1507 | void |
1508 | stap_probe::clear_semaphore (struct objfile *objfile, struct gdbarch *gdbarch) | |
1509 | { | |
7f0ae84c GB |
1510 | if (m_sem_addr == 0) |
1511 | return; | |
685de8c2 | 1512 | stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 0, gdbarch); |
55aa24fb SDJ |
1513 | } |
1514 | ||
0e9ae10f | 1515 | /* Implementation of the 'get_static_ops' method. */ |
55aa24fb | 1516 | |
0e9ae10f SDJ |
1517 | const static_probe_ops * |
1518 | stap_probe::get_static_ops () const | |
1519 | { | |
1520 | return &stap_static_probe_ops; | |
1521 | } | |
1522 | ||
1523 | /* Implementation of the 'gen_info_probes_table_values' method. */ | |
1524 | ||
1525 | std::vector<const char *> | |
1526 | stap_probe::gen_info_probes_table_values () const | |
55aa24fb | 1527 | { |
0e9ae10f | 1528 | const char *val = NULL; |
55aa24fb | 1529 | |
0e9ae10f SDJ |
1530 | if (m_sem_addr != 0) |
1531 | val = print_core_address (this->get_gdbarch (), m_sem_addr); | |
55aa24fb | 1532 | |
0e9ae10f | 1533 | return std::vector<const char *> { val }; |
55aa24fb SDJ |
1534 | } |
1535 | ||
55aa24fb SDJ |
1536 | /* Helper function that parses the information contained in a |
1537 | SystemTap's probe. Basically, the information consists in: | |
1538 | ||
1539 | - Probe's PC address; | |
1540 | - Link-time section address of `.stapsdt.base' section; | |
1541 | - Link-time address of the semaphore variable, or ZERO if the | |
1542 | probe doesn't have an associated semaphore; | |
1543 | - Probe's provider name; | |
1544 | - Probe's name; | |
3ca58cde | 1545 | - Probe's argument format. */ |
55aa24fb SDJ |
1546 | |
1547 | static void | |
1548 | handle_stap_probe (struct objfile *objfile, struct sdt_note *el, | |
814cf43a TT |
1549 | std::vector<std::unique_ptr<probe>> *probesp, |
1550 | CORE_ADDR base) | |
55aa24fb SDJ |
1551 | { |
1552 | bfd *abfd = objfile->obfd; | |
1553 | int size = bfd_get_arch_size (abfd) / 8; | |
08feed99 | 1554 | struct gdbarch *gdbarch = objfile->arch (); |
55aa24fb | 1555 | struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr; |
55aa24fb SDJ |
1556 | |
1557 | /* Provider and the name of the probe. */ | |
0e9ae10f SDJ |
1558 | const char *provider = (const char *) &el->data[3 * size]; |
1559 | const char *name = ((const char *) | |
1560 | memchr (provider, '\0', | |
1561 | (char *) el->data + el->size - provider)); | |
55aa24fb | 1562 | /* Making sure there is a name. */ |
0e9ae10f | 1563 | if (name == NULL) |
55aa24fb | 1564 | { |
f3da9116 | 1565 | complaint (_("corrupt probe name when reading `%s'"), |
4262abfb | 1566 | objfile_name (objfile)); |
55aa24fb SDJ |
1567 | |
1568 | /* There is no way to use a probe without a name or a provider, so | |
f3da9116 | 1569 | returning here makes sense. */ |
55aa24fb SDJ |
1570 | return; |
1571 | } | |
1572 | else | |
0e9ae10f | 1573 | ++name; |
55aa24fb SDJ |
1574 | |
1575 | /* Retrieving the probe's address. */ | |
0e9ae10f | 1576 | CORE_ADDR address = extract_typed_address (&el->data[0], ptr_type); |
55aa24fb SDJ |
1577 | |
1578 | /* Link-time sh_addr of `.stapsdt.base' section. */ | |
0e9ae10f | 1579 | CORE_ADDR base_ref = extract_typed_address (&el->data[size], ptr_type); |
55aa24fb SDJ |
1580 | |
1581 | /* Semaphore address. */ | |
0e9ae10f | 1582 | CORE_ADDR sem_addr = extract_typed_address (&el->data[2 * size], ptr_type); |
55aa24fb | 1583 | |
0e9ae10f SDJ |
1584 | address += base - base_ref; |
1585 | if (sem_addr != 0) | |
1586 | sem_addr += base - base_ref; | |
55aa24fb SDJ |
1587 | |
1588 | /* Arguments. We can only extract the argument format if there is a valid | |
1589 | name for this probe. */ | |
0e9ae10f SDJ |
1590 | const char *probe_args = ((const char*) |
1591 | memchr (name, '\0', | |
1592 | (char *) el->data + el->size - name)); | |
55aa24fb SDJ |
1593 | |
1594 | if (probe_args != NULL) | |
1595 | ++probe_args; | |
1596 | ||
97c2dca0 | 1597 | if (probe_args == NULL |
0e9ae10f | 1598 | || (memchr (probe_args, '\0', (char *) el->data + el->size - name) |
97c2dca0 | 1599 | != el->data + el->size - 1)) |
55aa24fb | 1600 | { |
f3da9116 | 1601 | complaint (_("corrupt probe argument when reading `%s'"), |
4262abfb | 1602 | objfile_name (objfile)); |
55aa24fb | 1603 | /* If the argument string is NULL, it means some problem happened with |
f3da9116 | 1604 | it. So we return. */ |
55aa24fb SDJ |
1605 | return; |
1606 | } | |
1607 | ||
0e9ae10f SDJ |
1608 | stap_probe *ret = new stap_probe (std::string (name), std::string (provider), |
1609 | address, gdbarch, sem_addr, probe_args); | |
55aa24fb SDJ |
1610 | |
1611 | /* Successfully created probe. */ | |
814cf43a | 1612 | probesp->emplace_back (ret); |
55aa24fb SDJ |
1613 | } |
1614 | ||
55aa24fb SDJ |
1615 | /* Helper function which iterates over every section in the BFD file, |
1616 | trying to find the base address of the SystemTap base section. | |
1617 | Returns 1 if found (setting BASE to the proper value), zero otherwise. */ | |
1618 | ||
1619 | static int | |
1620 | get_stap_base_address (bfd *obfd, bfd_vma *base) | |
1621 | { | |
1622 | asection *ret = NULL; | |
1623 | ||
3cabfd26 TT |
1624 | for (asection *sect : gdb_bfd_sections (obfd)) |
1625 | if ((sect->flags & (SEC_DATA | SEC_ALLOC | SEC_HAS_CONTENTS)) | |
1626 | && sect->name && !strcmp (sect->name, STAP_BASE_SECTION_NAME)) | |
1627 | ret = sect; | |
55aa24fb | 1628 | |
97c2dca0 | 1629 | if (ret == NULL) |
55aa24fb | 1630 | { |
b98664d3 | 1631 | complaint (_("could not obtain base address for " |
55aa24fb | 1632 | "SystemTap section on objfile `%s'."), |
c7e97679 | 1633 | bfd_get_filename (obfd)); |
55aa24fb SDJ |
1634 | return 0; |
1635 | } | |
1636 | ||
97c2dca0 | 1637 | if (base != NULL) |
55aa24fb SDJ |
1638 | *base = ret->vma; |
1639 | ||
1640 | return 1; | |
1641 | } | |
1642 | ||
0e9ae10f | 1643 | /* Implementation of the 'is_linespec' method. */ |
55aa24fb | 1644 | |
0e9ae10f SDJ |
1645 | bool |
1646 | stap_static_probe_ops::is_linespec (const char **linespecp) const | |
1647 | { | |
1648 | static const char *const keywords[] = { "-pstap", "-probe-stap", NULL }; | |
1649 | ||
1650 | return probe_is_linespec_by_keyword (linespecp, keywords); | |
1651 | } | |
1652 | ||
1653 | /* Implementation of the 'get_probes' method. */ | |
1654 | ||
1655 | void | |
814cf43a TT |
1656 | stap_static_probe_ops::get_probes |
1657 | (std::vector<std::unique_ptr<probe>> *probesp, | |
1658 | struct objfile *objfile) const | |
55aa24fb SDJ |
1659 | { |
1660 | /* If we are here, then this is the first time we are parsing the | |
1661 | SystemTap probe's information. We basically have to count how many | |
1662 | probes the objfile has, and then fill in the necessary information | |
1663 | for each one. */ | |
1664 | bfd *obfd = objfile->obfd; | |
1665 | bfd_vma base; | |
1666 | struct sdt_note *iter; | |
aaa63a31 | 1667 | unsigned save_probesp_len = probesp->size (); |
55aa24fb | 1668 | |
d7333987 SDJ |
1669 | if (objfile->separate_debug_objfile_backlink != NULL) |
1670 | { | |
1671 | /* This is a .debug file, not the objfile itself. */ | |
1672 | return; | |
1673 | } | |
1674 | ||
97c2dca0 | 1675 | if (elf_tdata (obfd)->sdt_note_head == NULL) |
55aa24fb SDJ |
1676 | { |
1677 | /* There isn't any probe here. */ | |
1678 | return; | |
1679 | } | |
1680 | ||
1681 | if (!get_stap_base_address (obfd, &base)) | |
1682 | { | |
1683 | /* There was an error finding the base address for the section. | |
1684 | Just return NULL. */ | |
1685 | return; | |
1686 | } | |
1687 | ||
1688 | /* Parsing each probe's information. */ | |
97c2dca0 SDJ |
1689 | for (iter = elf_tdata (obfd)->sdt_note_head; |
1690 | iter != NULL; | |
1691 | iter = iter->next) | |
55aa24fb SDJ |
1692 | { |
1693 | /* We first have to handle all the information about the | |
1694 | probe which is present in the section. */ | |
1695 | handle_stap_probe (objfile, iter, probesp, base); | |
1696 | } | |
1697 | ||
aaa63a31 | 1698 | if (save_probesp_len == probesp->size ()) |
55aa24fb SDJ |
1699 | { |
1700 | /* If we are here, it means we have failed to parse every known | |
1701 | probe. */ | |
f3da9116 | 1702 | complaint (_("could not parse SystemTap probe(s) from inferior")); |
55aa24fb SDJ |
1703 | return; |
1704 | } | |
1705 | } | |
1706 | ||
6f9b8491 JM |
1707 | /* Implementation of the type_name method. */ |
1708 | ||
0e9ae10f SDJ |
1709 | const char * |
1710 | stap_static_probe_ops::type_name () const | |
6f9b8491 | 1711 | { |
6f9b8491 JM |
1712 | return "stap"; |
1713 | } | |
1714 | ||
0e9ae10f | 1715 | /* Implementation of the 'gen_info_probes_table_header' method. */ |
55aa24fb | 1716 | |
0e9ae10f SDJ |
1717 | std::vector<struct info_probe_column> |
1718 | stap_static_probe_ops::gen_info_probes_table_header () const | |
55aa24fb | 1719 | { |
0e9ae10f | 1720 | struct info_probe_column stap_probe_column; |
55aa24fb SDJ |
1721 | |
1722 | stap_probe_column.field_name = "semaphore"; | |
1723 | stap_probe_column.print_name = _("Semaphore"); | |
1724 | ||
0e9ae10f | 1725 | return std::vector<struct info_probe_column> { stap_probe_column }; |
55aa24fb SDJ |
1726 | } |
1727 | ||
55aa24fb SDJ |
1728 | /* Implementation of the `info probes stap' command. */ |
1729 | ||
1730 | static void | |
884beb0c | 1731 | info_probes_stap_command (const char *arg, int from_tty) |
55aa24fb | 1732 | { |
0e9ae10f | 1733 | info_probes_for_spops (arg, from_tty, &stap_static_probe_ops); |
55aa24fb SDJ |
1734 | } |
1735 | ||
6c265988 | 1736 | void _initialize_stap_probe (); |
55aa24fb | 1737 | void |
6c265988 | 1738 | _initialize_stap_probe () |
55aa24fb | 1739 | { |
0e9ae10f | 1740 | all_static_probe_ops.push_back (&stap_static_probe_ops); |
55aa24fb | 1741 | |
ccce17b0 YQ |
1742 | add_setshow_zuinteger_cmd ("stap-expression", class_maintenance, |
1743 | &stap_expression_debug, | |
1744 | _("Set SystemTap expression debugging."), | |
1745 | _("Show SystemTap expression debugging."), | |
1746 | _("When non-zero, the internal representation " | |
1747 | "of SystemTap expressions will be printed."), | |
1748 | NULL, | |
1749 | show_stapexpressiondebug, | |
1750 | &setdebuglist, &showdebuglist); | |
55aa24fb | 1751 | |
55aa24fb SDJ |
1752 | add_cmd ("stap", class_info, info_probes_stap_command, |
1753 | _("\ | |
1754 | Show information about SystemTap static probes.\n\ | |
1755 | Usage: info probes stap [PROVIDER [NAME [OBJECT]]]\n\ | |
1756 | Each argument is a regular expression, used to select probes.\n\ | |
1757 | PROVIDER matches probe provider names.\n\ | |
1758 | NAME matches the probe names.\n\ | |
1759 | OBJECT matches the executable or shared library name."), | |
1760 | info_probes_cmdlist_get ()); | |
1761 | ||
4c5e7a93 TT |
1762 | |
1763 | using namespace expr; | |
1764 | stap_maker_map[BINOP_ADD] = make_operation<add_operation>; | |
1765 | stap_maker_map[BINOP_BITWISE_AND] = make_operation<bitwise_and_operation>; | |
1766 | stap_maker_map[BINOP_BITWISE_IOR] = make_operation<bitwise_ior_operation>; | |
1767 | stap_maker_map[BINOP_BITWISE_XOR] = make_operation<bitwise_xor_operation>; | |
1768 | stap_maker_map[BINOP_DIV] = make_operation<div_operation>; | |
1769 | stap_maker_map[BINOP_EQUAL] = make_operation<equal_operation>; | |
1770 | stap_maker_map[BINOP_GEQ] = make_operation<geq_operation>; | |
1771 | stap_maker_map[BINOP_GTR] = make_operation<gtr_operation>; | |
1772 | stap_maker_map[BINOP_LEQ] = make_operation<leq_operation>; | |
1773 | stap_maker_map[BINOP_LESS] = make_operation<less_operation>; | |
1774 | stap_maker_map[BINOP_LOGICAL_AND] = make_operation<logical_and_operation>; | |
1775 | stap_maker_map[BINOP_LOGICAL_OR] = make_operation<logical_or_operation>; | |
1776 | stap_maker_map[BINOP_LSH] = make_operation<lsh_operation>; | |
1777 | stap_maker_map[BINOP_MUL] = make_operation<mul_operation>; | |
1778 | stap_maker_map[BINOP_NOTEQUAL] = make_operation<notequal_operation>; | |
1779 | stap_maker_map[BINOP_REM] = make_operation<rem_operation>; | |
1780 | stap_maker_map[BINOP_RSH] = make_operation<rsh_operation>; | |
1781 | stap_maker_map[BINOP_SUB] = make_operation<sub_operation>; | |
55aa24fb | 1782 | } |