]>
Commit | Line | Data |
---|---|---|
55aa24fb SDJ |
1 | /* SystemTap probe support for GDB. |
2 | ||
213516ef | 3 | Copyright (C) 2012-2023 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> | |
675da9a5 | 41 | #include "gdbsupport/hash_enum.h" |
55aa24fb SDJ |
42 | |
43 | #include <ctype.h> | |
44 | ||
45 | /* The name of the SystemTap section where we will find information about | |
46 | the probes. */ | |
47 | ||
48 | #define STAP_BASE_SECTION_NAME ".stapsdt.base" | |
49 | ||
55aa24fb SDJ |
50 | /* Should we display debug information for the probe's argument expression |
51 | parsing? */ | |
52 | ||
ccce17b0 | 53 | static unsigned int stap_expression_debug = 0; |
55aa24fb SDJ |
54 | |
55 | /* The various possibilities of bitness defined for a probe's argument. | |
56 | ||
57 | The relationship is: | |
58 | ||
59 | - STAP_ARG_BITNESS_UNDEFINED: The user hasn't specified the bitness. | |
30a1e6cc SDJ |
60 | - STAP_ARG_BITNESS_8BIT_UNSIGNED: argument string starts with `1@'. |
61 | - STAP_ARG_BITNESS_8BIT_SIGNED: argument string starts with `-1@'. | |
62 | - STAP_ARG_BITNESS_16BIT_UNSIGNED: argument string starts with `2@'. | |
63 | - STAP_ARG_BITNESS_16BIT_SIGNED: argument string starts with `-2@'. | |
55aa24fb SDJ |
64 | - STAP_ARG_BITNESS_32BIT_UNSIGNED: argument string starts with `4@'. |
65 | - STAP_ARG_BITNESS_32BIT_SIGNED: argument string starts with `-4@'. | |
66 | - STAP_ARG_BITNESS_64BIT_UNSIGNED: argument string starts with `8@'. | |
67 | - STAP_ARG_BITNESS_64BIT_SIGNED: argument string starts with `-8@'. */ | |
68 | ||
69 | enum stap_arg_bitness | |
70 | { | |
71 | STAP_ARG_BITNESS_UNDEFINED, | |
30a1e6cc SDJ |
72 | STAP_ARG_BITNESS_8BIT_UNSIGNED, |
73 | STAP_ARG_BITNESS_8BIT_SIGNED, | |
74 | STAP_ARG_BITNESS_16BIT_UNSIGNED, | |
75 | STAP_ARG_BITNESS_16BIT_SIGNED, | |
55aa24fb SDJ |
76 | STAP_ARG_BITNESS_32BIT_UNSIGNED, |
77 | STAP_ARG_BITNESS_32BIT_SIGNED, | |
78 | STAP_ARG_BITNESS_64BIT_UNSIGNED, | |
79 | STAP_ARG_BITNESS_64BIT_SIGNED, | |
80 | }; | |
81 | ||
82 | /* The following structure represents a single argument for the probe. */ | |
83 | ||
84 | struct stap_probe_arg | |
85 | { | |
0e9ae10f SDJ |
86 | /* Constructor for stap_probe_arg. */ |
87 | stap_probe_arg (enum stap_arg_bitness bitness_, struct type *atype_, | |
88 | expression_up &&aexpr_) | |
89 | : bitness (bitness_), atype (atype_), aexpr (std::move (aexpr_)) | |
90 | {} | |
91 | ||
55aa24fb SDJ |
92 | /* The bitness of this argument. */ |
93 | enum stap_arg_bitness bitness; | |
94 | ||
95 | /* The corresponding `struct type *' to the bitness. */ | |
96 | struct type *atype; | |
97 | ||
98 | /* The argument converted to an internal GDB expression. */ | |
0e9ae10f | 99 | expression_up aexpr; |
55aa24fb SDJ |
100 | }; |
101 | ||
0e9ae10f | 102 | /* Class that implements the static probe methods for "stap" probes. */ |
55aa24fb | 103 | |
0e9ae10f | 104 | class stap_static_probe_ops : public static_probe_ops |
55aa24fb | 105 | { |
0e9ae10f | 106 | public: |
4212d509 TT |
107 | /* We need a user-provided constructor to placate some compilers. |
108 | See PR build/24937. */ | |
109 | stap_static_probe_ops () | |
110 | { | |
111 | } | |
112 | ||
0e9ae10f SDJ |
113 | /* See probe.h. */ |
114 | bool is_linespec (const char **linespecp) const override; | |
55aa24fb | 115 | |
0e9ae10f | 116 | /* See probe.h. */ |
814cf43a | 117 | void get_probes (std::vector<std::unique_ptr<probe>> *probesp, |
0e9ae10f SDJ |
118 | struct objfile *objfile) const override; |
119 | ||
120 | /* See probe.h. */ | |
121 | const char *type_name () const override; | |
122 | ||
123 | /* See probe.h. */ | |
124 | std::vector<struct info_probe_column> gen_info_probes_table_header | |
125 | () const override; | |
126 | }; | |
127 | ||
128 | /* SystemTap static_probe_ops. */ | |
129 | ||
3dcfdc58 | 130 | const stap_static_probe_ops stap_static_probe_ops {}; |
0e9ae10f SDJ |
131 | |
132 | class stap_probe : public probe | |
133 | { | |
134 | public: | |
135 | /* Constructor for stap_probe. */ | |
136 | stap_probe (std::string &&name_, std::string &&provider_, CORE_ADDR address_, | |
137 | struct gdbarch *arch_, CORE_ADDR sem_addr, const char *args_text) | |
138 | : probe (std::move (name_), std::move (provider_), address_, arch_), | |
139 | m_sem_addr (sem_addr), | |
140 | m_have_parsed_args (false), m_unparsed_args_text (args_text) | |
141 | {} | |
142 | ||
143 | /* See probe.h. */ | |
144 | CORE_ADDR get_relocated_address (struct objfile *objfile) override; | |
145 | ||
146 | /* See probe.h. */ | |
fe01123e | 147 | unsigned get_argument_count (struct gdbarch *gdbarch) override; |
0e9ae10f SDJ |
148 | |
149 | /* See probe.h. */ | |
150 | bool can_evaluate_arguments () const override; | |
151 | ||
152 | /* See probe.h. */ | |
153 | struct value *evaluate_argument (unsigned n, | |
bd2b40ac | 154 | frame_info_ptr frame) override; |
0e9ae10f SDJ |
155 | |
156 | /* See probe.h. */ | |
157 | void compile_to_ax (struct agent_expr *aexpr, | |
158 | struct axs_value *axs_value, | |
159 | unsigned n) override; | |
160 | ||
161 | /* See probe.h. */ | |
162 | void set_semaphore (struct objfile *objfile, | |
163 | struct gdbarch *gdbarch) override; | |
164 | ||
165 | /* See probe.h. */ | |
166 | void clear_semaphore (struct objfile *objfile, | |
167 | struct gdbarch *gdbarch) override; | |
168 | ||
169 | /* See probe.h. */ | |
170 | const static_probe_ops *get_static_ops () const override; | |
171 | ||
172 | /* See probe.h. */ | |
173 | std::vector<const char *> gen_info_probes_table_values () const override; | |
174 | ||
175 | /* Return argument N of probe. | |
176 | ||
177 | If the probe's arguments have not been parsed yet, parse them. If | |
178 | there are no arguments, throw an exception (error). Otherwise, | |
179 | return the requested argument. */ | |
180 | struct stap_probe_arg *get_arg_by_number (unsigned n, | |
181 | struct gdbarch *gdbarch) | |
182 | { | |
183 | if (!m_have_parsed_args) | |
184 | this->parse_arguments (gdbarch); | |
185 | ||
186 | gdb_assert (m_have_parsed_args); | |
187 | if (m_parsed_args.empty ()) | |
f34652de | 188 | internal_error (_("Probe '%s' apparently does not have arguments, but \n" |
0e9ae10f SDJ |
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 ()) | |
f34652de | 194 | internal_error (_("Probe '%s' has %d arguments, but GDB is requesting\n" |
0e9ae10f SDJ |
195 | "argument %u. This should not happen. Please\n" |
196 | "report this bug."), | |
197 | this->get_name ().c_str (), | |
198 | (int) m_parsed_args.size (), n); | |
199 | ||
200 | return &m_parsed_args[n]; | |
201 | } | |
202 | ||
203 | /* Function which parses an argument string from the probe, | |
204 | correctly splitting the arguments and storing their information | |
205 | in properly ways. | |
206 | ||
207 | Consider the following argument string (x86 syntax): | |
208 | ||
209 | `4@%eax 4@$10' | |
210 | ||
211 | We have two arguments, `%eax' and `$10', both with 32-bit | |
212 | unsigned bitness. This function basically handles them, properly | |
213 | filling some structures with this information. */ | |
214 | void parse_arguments (struct gdbarch *gdbarch); | |
215 | ||
216 | private: | |
55aa24fb | 217 | /* If the probe has a semaphore associated, then this is the value of |
729662a5 | 218 | it, relative to SECT_OFF_DATA. */ |
0e9ae10f | 219 | CORE_ADDR m_sem_addr; |
55aa24fb | 220 | |
0e9ae10f SDJ |
221 | /* True if the arguments have been parsed. */ |
222 | bool m_have_parsed_args; | |
97c2dca0 | 223 | |
0e9ae10f SDJ |
224 | /* The text version of the probe's arguments, unparsed. */ |
225 | const char *m_unparsed_args_text; | |
55aa24fb | 226 | |
0e9ae10f SDJ |
227 | /* Information about each argument. This is an array of `stap_probe_arg', |
228 | with each entry representing one argument. This is only valid if | |
229 | M_ARGS_PARSED is true. */ | |
230 | std::vector<struct stap_probe_arg> m_parsed_args; | |
55aa24fb SDJ |
231 | }; |
232 | ||
233 | /* When parsing the arguments, we have to establish different precedences | |
234 | for the various kinds of asm operators. This enumeration represents those | |
235 | precedences. | |
236 | ||
237 | This logic behind this is available at | |
238 | <http://sourceware.org/binutils/docs/as/Infix-Ops.html#Infix-Ops>, or using | |
239 | the command "info '(as)Infix Ops'". */ | |
240 | ||
241 | enum stap_operand_prec | |
242 | { | |
243 | /* Lowest precedence, used for non-recognized operands or for the beginning | |
244 | of the parsing process. */ | |
245 | STAP_OPERAND_PREC_NONE = 0, | |
246 | ||
247 | /* Precedence of logical OR. */ | |
248 | STAP_OPERAND_PREC_LOGICAL_OR, | |
249 | ||
250 | /* Precedence of logical AND. */ | |
251 | STAP_OPERAND_PREC_LOGICAL_AND, | |
252 | ||
253 | /* Precedence of additive (plus, minus) and comparative (equal, less, | |
254 | greater-than, etc) operands. */ | |
255 | STAP_OPERAND_PREC_ADD_CMP, | |
256 | ||
257 | /* Precedence of bitwise operands (bitwise OR, XOR, bitwise AND, | |
258 | logical NOT). */ | |
259 | STAP_OPERAND_PREC_BITWISE, | |
260 | ||
261 | /* Precedence of multiplicative operands (multiplication, division, | |
262 | remainder, left shift and right shift). */ | |
263 | STAP_OPERAND_PREC_MUL | |
264 | }; | |
265 | ||
4c5e7a93 TT |
266 | static expr::operation_up stap_parse_argument_1 (struct stap_parse_info *p, |
267 | expr::operation_up &&lhs, | |
268 | enum stap_operand_prec prec) | |
269 | ATTRIBUTE_UNUSED_RESULT; | |
55aa24fb | 270 | |
4c5e7a93 TT |
271 | static expr::operation_up stap_parse_argument_conditionally |
272 | (struct stap_parse_info *p) ATTRIBUTE_UNUSED_RESULT; | |
55aa24fb | 273 | |
af2d9bee | 274 | /* Returns true if *S is an operator, false otherwise. */ |
55aa24fb | 275 | |
af2d9bee | 276 | static bool stap_is_operator (const char *op); |
55aa24fb SDJ |
277 | |
278 | static void | |
279 | show_stapexpressiondebug (struct ui_file *file, int from_tty, | |
280 | struct cmd_list_element *c, const char *value) | |
281 | { | |
6cb06a8c TT |
282 | gdb_printf (file, _("SystemTap Probe expression debugging is %s.\n"), |
283 | value); | |
55aa24fb SDJ |
284 | } |
285 | ||
286 | /* Returns the operator precedence level of OP, or STAP_OPERAND_PREC_NONE | |
287 | if the operator code was not recognized. */ | |
288 | ||
289 | static enum stap_operand_prec | |
290 | stap_get_operator_prec (enum exp_opcode op) | |
291 | { | |
292 | switch (op) | |
293 | { | |
294 | case BINOP_LOGICAL_OR: | |
295 | return STAP_OPERAND_PREC_LOGICAL_OR; | |
296 | ||
297 | case BINOP_LOGICAL_AND: | |
298 | return STAP_OPERAND_PREC_LOGICAL_AND; | |
299 | ||
300 | case BINOP_ADD: | |
301 | case BINOP_SUB: | |
302 | case BINOP_EQUAL: | |
303 | case BINOP_NOTEQUAL: | |
304 | case BINOP_LESS: | |
305 | case BINOP_LEQ: | |
306 | case BINOP_GTR: | |
307 | case BINOP_GEQ: | |
308 | return STAP_OPERAND_PREC_ADD_CMP; | |
309 | ||
310 | case BINOP_BITWISE_IOR: | |
311 | case BINOP_BITWISE_AND: | |
312 | case BINOP_BITWISE_XOR: | |
313 | case UNOP_LOGICAL_NOT: | |
314 | return STAP_OPERAND_PREC_BITWISE; | |
315 | ||
316 | case BINOP_MUL: | |
317 | case BINOP_DIV: | |
318 | case BINOP_REM: | |
319 | case BINOP_LSH: | |
320 | case BINOP_RSH: | |
321 | return STAP_OPERAND_PREC_MUL; | |
322 | ||
323 | default: | |
324 | return STAP_OPERAND_PREC_NONE; | |
325 | } | |
326 | } | |
327 | ||
3ca58cde SDJ |
328 | /* Given S, read the operator in it. Return the EXP_OPCODE which |
329 | represents the operator detected, or throw an error if no operator | |
330 | was found. */ | |
55aa24fb | 331 | |
fcf57f19 SDJ |
332 | static enum exp_opcode |
333 | stap_get_opcode (const char **s) | |
55aa24fb SDJ |
334 | { |
335 | const char c = **s; | |
fcf57f19 | 336 | enum exp_opcode op; |
55aa24fb SDJ |
337 | |
338 | *s += 1; | |
339 | ||
340 | switch (c) | |
341 | { | |
342 | case '*': | |
fcf57f19 | 343 | op = BINOP_MUL; |
55aa24fb SDJ |
344 | break; |
345 | ||
346 | case '/': | |
fcf57f19 | 347 | op = BINOP_DIV; |
55aa24fb SDJ |
348 | break; |
349 | ||
350 | case '%': | |
fcf57f19 | 351 | op = BINOP_REM; |
55aa24fb SDJ |
352 | break; |
353 | ||
354 | case '<': | |
fcf57f19 | 355 | op = BINOP_LESS; |
55aa24fb SDJ |
356 | if (**s == '<') |
357 | { | |
358 | *s += 1; | |
fcf57f19 | 359 | op = BINOP_LSH; |
55aa24fb SDJ |
360 | } |
361 | else if (**s == '=') | |
362 | { | |
363 | *s += 1; | |
fcf57f19 | 364 | op = BINOP_LEQ; |
55aa24fb SDJ |
365 | } |
366 | else if (**s == '>') | |
367 | { | |
368 | *s += 1; | |
fcf57f19 | 369 | op = BINOP_NOTEQUAL; |
55aa24fb SDJ |
370 | } |
371 | break; | |
372 | ||
373 | case '>': | |
fcf57f19 | 374 | op = BINOP_GTR; |
55aa24fb SDJ |
375 | if (**s == '>') |
376 | { | |
377 | *s += 1; | |
fcf57f19 | 378 | op = BINOP_RSH; |
55aa24fb SDJ |
379 | } |
380 | else if (**s == '=') | |
381 | { | |
382 | *s += 1; | |
fcf57f19 | 383 | op = BINOP_GEQ; |
55aa24fb SDJ |
384 | } |
385 | break; | |
386 | ||
387 | case '|': | |
fcf57f19 | 388 | op = BINOP_BITWISE_IOR; |
55aa24fb SDJ |
389 | if (**s == '|') |
390 | { | |
391 | *s += 1; | |
fcf57f19 | 392 | op = BINOP_LOGICAL_OR; |
55aa24fb SDJ |
393 | } |
394 | break; | |
395 | ||
396 | case '&': | |
fcf57f19 | 397 | op = BINOP_BITWISE_AND; |
55aa24fb SDJ |
398 | if (**s == '&') |
399 | { | |
400 | *s += 1; | |
fcf57f19 | 401 | op = BINOP_LOGICAL_AND; |
55aa24fb SDJ |
402 | } |
403 | break; | |
404 | ||
405 | case '^': | |
fcf57f19 | 406 | op = BINOP_BITWISE_XOR; |
55aa24fb SDJ |
407 | break; |
408 | ||
409 | case '!': | |
fcf57f19 | 410 | op = UNOP_LOGICAL_NOT; |
55aa24fb SDJ |
411 | break; |
412 | ||
413 | case '+': | |
fcf57f19 | 414 | op = BINOP_ADD; |
55aa24fb SDJ |
415 | break; |
416 | ||
417 | case '-': | |
fcf57f19 | 418 | op = BINOP_SUB; |
55aa24fb SDJ |
419 | break; |
420 | ||
421 | case '=': | |
fcf57f19 SDJ |
422 | gdb_assert (**s == '='); |
423 | op = BINOP_EQUAL; | |
55aa24fb SDJ |
424 | break; |
425 | ||
426 | default: | |
f469e8ce SDJ |
427 | error (_("Invalid opcode in expression `%s' for SystemTap" |
428 | "probe"), *s); | |
55aa24fb SDJ |
429 | } |
430 | ||
fcf57f19 | 431 | return op; |
55aa24fb SDJ |
432 | } |
433 | ||
4c5e7a93 TT |
434 | typedef expr::operation_up binop_maker_ftype (expr::operation_up &&, |
435 | expr::operation_up &&); | |
436 | /* Map from an expression opcode to a function that can create a | |
437 | binary operation of that type. */ | |
675da9a5 TT |
438 | static std::unordered_map<exp_opcode, binop_maker_ftype *, |
439 | gdb::hash_enum<exp_opcode>> stap_maker_map; | |
4c5e7a93 TT |
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) | |
f34652de | 812 | internal_error (_("Invalid register name '%s' after replacing it" |
7d7571f0 | 813 | " (previous name was '%s')"), |
6b78c3f8 AB |
814 | newregname.c_str (), regname.c_str ()); |
815 | ||
4c5e7a93 | 816 | regname = std::move (newregname); |
7d7571f0 SDJ |
817 | } |
818 | } | |
819 | ||
4c5e7a93 | 820 | operation_up reg = make_operation<register_operation> (std::move (regname)); |
55aa24fb | 821 | |
6f0dabd4 AB |
822 | /* If the argument has been placed into a vector register then (for most |
823 | architectures), the type of this register will be a union of arrays. | |
824 | As a result, attempting to cast from the register type to the scalar | |
825 | argument type will not be possible (GDB will throw an error during | |
826 | expression evaluation). | |
827 | ||
828 | The solution is to extract the scalar type from the value contents of | |
829 | the entire register value. */ | |
830 | if (!is_scalar_type (gdbarch_register_type (gdbarch, regnum))) | |
831 | { | |
832 | gdb_assert (is_scalar_type (p->arg_type)); | |
833 | reg = make_operation<unop_extract_operation> (std::move (reg), | |
834 | p->arg_type); | |
835 | } | |
836 | ||
55aa24fb SDJ |
837 | if (indirect_p) |
838 | { | |
4c5e7a93 TT |
839 | if (disp_op != nullptr) |
840 | reg = make_operation<add_operation> (std::move (disp_op), | |
841 | std::move (reg)); | |
55aa24fb SDJ |
842 | |
843 | /* Casting to the expected type. */ | |
4c5e7a93 TT |
844 | struct type *arg_ptr_type = lookup_pointer_type (p->arg_type); |
845 | reg = make_operation<unop_cast_operation> (std::move (reg), | |
846 | arg_ptr_type); | |
847 | reg = make_operation<unop_ind_operation> (std::move (reg)); | |
55aa24fb SDJ |
848 | } |
849 | ||
850 | /* Getting rid of the register name suffix. */ | |
05c0465e SDJ |
851 | if (stap_check_register_suffix (gdbarch, p->arg, ®_suffix)) |
852 | p->arg += strlen (reg_suffix); | |
853 | else | |
854 | error (_("Missing register name suffix on expression `%s'."), | |
855 | p->saved_arg); | |
55aa24fb SDJ |
856 | |
857 | /* Getting rid of the register indirection suffix. */ | |
05c0465e | 858 | if (indirect_p) |
55aa24fb | 859 | { |
05c0465e SDJ |
860 | if (stap_check_register_indirection_suffix (gdbarch, p->arg, |
861 | ®_ind_suffix)) | |
862 | p->arg += strlen (reg_ind_suffix); | |
863 | else | |
864 | error (_("Missing indirection suffix on expression `%s'."), | |
865 | p->saved_arg); | |
55aa24fb | 866 | } |
4c5e7a93 TT |
867 | |
868 | return reg; | |
55aa24fb SDJ |
869 | } |
870 | ||
871 | /* This function is responsible for parsing a single operand. | |
872 | ||
873 | A single operand can be: | |
874 | ||
875 | - an unary operation (e.g., `-5', `~2', or even with subexpressions | |
dda83cd7 | 876 | like `-(2 + 1)') |
55aa24fb | 877 | - a register displacement, which will be treated as a register |
dda83cd7 | 878 | operand (e.g., `-4(%eax)' on x86) |
55aa24fb SDJ |
879 | - a numeric constant, or |
880 | - a register operand (see function `stap_parse_register_operand') | |
881 | ||
882 | The function also calls special-handling functions to deal with | |
883 | unrecognized operands, allowing arch-specific parsers to be | |
884 | created. */ | |
885 | ||
4c5e7a93 | 886 | static expr::operation_up |
55aa24fb SDJ |
887 | stap_parse_single_operand (struct stap_parse_info *p) |
888 | { | |
889 | struct gdbarch *gdbarch = p->gdbarch; | |
05c0465e | 890 | const char *int_prefix = NULL; |
55aa24fb | 891 | |
4c5e7a93 TT |
892 | using namespace expr; |
893 | ||
55aa24fb | 894 | /* We first try to parse this token as a "special token". */ |
4c5e7a93 | 895 | if (gdbarch_stap_parse_special_token_p (gdbarch)) |
f1bb75ab | 896 | { |
4c5e7a93 TT |
897 | operation_up token = gdbarch_stap_parse_special_token (gdbarch, p); |
898 | if (token != nullptr) | |
899 | return token; | |
f1bb75ab | 900 | } |
55aa24fb | 901 | |
4c5e7a93 TT |
902 | struct type *long_type = builtin_type (gdbarch)->builtin_long; |
903 | operation_up result; | |
6f52fdf4 | 904 | if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' || *p->arg == '!') |
55aa24fb SDJ |
905 | { |
906 | char c = *p->arg; | |
55aa24fb SDJ |
907 | /* We use this variable to do a lookahead. */ |
908 | const char *tmp = p->arg; | |
af2d9bee | 909 | bool has_digit = false; |
55aa24fb | 910 | |
97c2dca0 | 911 | /* Skipping signal. */ |
55aa24fb SDJ |
912 | ++tmp; |
913 | ||
914 | /* This is an unary operation. Here is a list of allowed tokens | |
915 | here: | |
916 | ||
917 | - numeric literal; | |
918 | - number (from register displacement) | |
919 | - subexpression (beginning with `(') | |
920 | ||
921 | We handle the register displacement here, and the other cases | |
922 | recursively. */ | |
923 | if (p->inside_paren_p) | |
f1735a53 | 924 | tmp = skip_spaces (tmp); |
55aa24fb | 925 | |
474ca4f6 | 926 | while (isdigit (*tmp)) |
a0bcdaa7 | 927 | { |
474ca4f6 SDJ |
928 | /* We skip the digit here because we are only interested in |
929 | knowing what kind of unary operation this is. The digit | |
930 | will be handled by one of the functions that will be | |
931 | called below ('stap_parse_argument_conditionally' or | |
932 | 'stap_parse_register_operand'). */ | |
933 | ++tmp; | |
af2d9bee | 934 | has_digit = true; |
a0bcdaa7 | 935 | } |
55aa24fb | 936 | |
474ca4f6 SDJ |
937 | if (has_digit && stap_is_register_indirection_prefix (gdbarch, tmp, |
938 | NULL)) | |
55aa24fb SDJ |
939 | { |
940 | /* If we are here, it means it is a displacement. The only | |
941 | operations allowed here are `-' and `+'. */ | |
f1bb75ab | 942 | if (c != '-' && c != '+') |
55aa24fb SDJ |
943 | error (_("Invalid operator `%c' for register displacement " |
944 | "on expression `%s'."), c, p->saved_arg); | |
945 | ||
4c5e7a93 | 946 | result = stap_parse_register_operand (p); |
55aa24fb | 947 | } |
474ca4f6 SDJ |
948 | else |
949 | { | |
950 | /* This is not a displacement. We skip the operator, and | |
951 | deal with it when the recursion returns. */ | |
952 | ++p->arg; | |
4c5e7a93 | 953 | result = stap_parse_argument_conditionally (p); |
474ca4f6 | 954 | if (c == '-') |
4c5e7a93 | 955 | result = make_operation<unary_neg_operation> (std::move (result)); |
474ca4f6 | 956 | else if (c == '~') |
4c5e7a93 TT |
957 | result = (make_operation<unary_complement_operation> |
958 | (std::move (result))); | |
6f52fdf4 | 959 | else if (c == '!') |
4c5e7a93 TT |
960 | result = (make_operation<unary_logical_not_operation> |
961 | (std::move (result))); | |
474ca4f6 | 962 | } |
55aa24fb SDJ |
963 | } |
964 | else if (isdigit (*p->arg)) | |
965 | { | |
966 | /* A temporary variable, needed for lookahead. */ | |
967 | const char *tmp = p->arg; | |
a0bcdaa7 | 968 | char *endp; |
55aa24fb SDJ |
969 | long number; |
970 | ||
05c0465e SDJ |
971 | /* We can be dealing with a numeric constant, or with a register |
972 | displacement. */ | |
a0bcdaa7 PA |
973 | number = strtol (tmp, &endp, 10); |
974 | tmp = endp; | |
55aa24fb SDJ |
975 | |
976 | if (p->inside_paren_p) | |
f1735a53 | 977 | tmp = skip_spaces (tmp); |
05c0465e SDJ |
978 | |
979 | /* If "stap_is_integer_prefix" returns true, it means we can | |
980 | accept integers without a prefix here. But we also need to | |
981 | check whether the next token (i.e., "tmp") is not a register | |
982 | indirection prefix. */ | |
983 | if (stap_is_integer_prefix (gdbarch, p->arg, NULL) | |
984 | && !stap_is_register_indirection_prefix (gdbarch, tmp, NULL)) | |
55aa24fb | 985 | { |
05c0465e SDJ |
986 | const char *int_suffix; |
987 | ||
55aa24fb | 988 | /* We are dealing with a numeric constant. */ |
4c5e7a93 | 989 | result = make_operation<long_const_operation> (long_type, number); |
55aa24fb SDJ |
990 | |
991 | p->arg = tmp; | |
992 | ||
05c0465e SDJ |
993 | if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix)) |
994 | p->arg += strlen (int_suffix); | |
995 | else | |
996 | error (_("Invalid constant suffix on expression `%s'."), | |
997 | p->saved_arg); | |
55aa24fb | 998 | } |
05c0465e | 999 | else if (stap_is_register_indirection_prefix (gdbarch, tmp, NULL)) |
4c5e7a93 | 1000 | result = stap_parse_register_operand (p); |
55aa24fb SDJ |
1001 | else |
1002 | error (_("Unknown numeric token on expression `%s'."), | |
1003 | p->saved_arg); | |
1004 | } | |
05c0465e | 1005 | else if (stap_is_integer_prefix (gdbarch, p->arg, &int_prefix)) |
55aa24fb SDJ |
1006 | { |
1007 | /* We are dealing with a numeric constant. */ | |
1008 | long number; | |
a0bcdaa7 | 1009 | char *endp; |
05c0465e | 1010 | const char *int_suffix; |
55aa24fb | 1011 | |
05c0465e | 1012 | p->arg += strlen (int_prefix); |
a0bcdaa7 PA |
1013 | number = strtol (p->arg, &endp, 10); |
1014 | p->arg = endp; | |
55aa24fb | 1015 | |
4c5e7a93 | 1016 | result = make_operation<long_const_operation> (long_type, number); |
55aa24fb | 1017 | |
05c0465e SDJ |
1018 | if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix)) |
1019 | p->arg += strlen (int_suffix); | |
1020 | else | |
1021 | error (_("Invalid constant suffix on expression `%s'."), | |
1022 | p->saved_arg); | |
55aa24fb | 1023 | } |
05c0465e SDJ |
1024 | else if (stap_is_register_prefix (gdbarch, p->arg, NULL) |
1025 | || stap_is_register_indirection_prefix (gdbarch, p->arg, NULL)) | |
4c5e7a93 | 1026 | result = stap_parse_register_operand (p); |
55aa24fb SDJ |
1027 | else |
1028 | error (_("Operator `%c' not recognized on expression `%s'."), | |
1029 | *p->arg, p->saved_arg); | |
4c5e7a93 TT |
1030 | |
1031 | return result; | |
55aa24fb SDJ |
1032 | } |
1033 | ||
1034 | /* This function parses an argument conditionally, based on single or | |
1035 | non-single operands. A non-single operand would be a parenthesized | |
1036 | expression (e.g., `(2 + 1)'), and a single operand is anything that | |
1037 | starts with `-', `~', `+' (i.e., unary operators), a digit, or | |
1038 | something recognized by `gdbarch_stap_is_single_operand'. */ | |
1039 | ||
4c5e7a93 | 1040 | static expr::operation_up |
55aa24fb SDJ |
1041 | stap_parse_argument_conditionally (struct stap_parse_info *p) |
1042 | { | |
97c2dca0 SDJ |
1043 | gdb_assert (gdbarch_stap_is_single_operand_p (p->gdbarch)); |
1044 | ||
4c5e7a93 | 1045 | expr::operation_up result; |
6f52fdf4 | 1046 | if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' || *p->arg == '!' |
55aa24fb SDJ |
1047 | || isdigit (*p->arg) |
1048 | || gdbarch_stap_is_single_operand (p->gdbarch, p->arg)) | |
4c5e7a93 | 1049 | result = stap_parse_single_operand (p); |
55aa24fb SDJ |
1050 | else if (*p->arg == '(') |
1051 | { | |
1052 | /* We are dealing with a parenthesized operand. It means we | |
1053 | have to parse it as it was a separate expression, without | |
1054 | left-side or precedence. */ | |
1055 | ++p->arg; | |
f1735a53 | 1056 | p->arg = skip_spaces (p->arg); |
55aa24fb SDJ |
1057 | ++p->inside_paren_p; |
1058 | ||
4c5e7a93 | 1059 | result = stap_parse_argument_1 (p, {}, STAP_OPERAND_PREC_NONE); |
55aa24fb | 1060 | |
6f52fdf4 | 1061 | p->arg = skip_spaces (p->arg); |
55aa24fb | 1062 | if (*p->arg != ')') |
9bb305b3 | 1063 | error (_("Missing close-parenthesis on expression `%s'."), |
55aa24fb SDJ |
1064 | p->saved_arg); |
1065 | ||
6f52fdf4 | 1066 | --p->inside_paren_p; |
55aa24fb SDJ |
1067 | ++p->arg; |
1068 | if (p->inside_paren_p) | |
f1735a53 | 1069 | p->arg = skip_spaces (p->arg); |
55aa24fb SDJ |
1070 | } |
1071 | else | |
1072 | error (_("Cannot parse expression `%s'."), p->saved_arg); | |
4c5e7a93 TT |
1073 | |
1074 | return result; | |
55aa24fb SDJ |
1075 | } |
1076 | ||
1077 | /* Helper function for `stap_parse_argument'. Please, see its comments to | |
1078 | better understand what this function does. */ | |
1079 | ||
4c5e7a93 TT |
1080 | static expr::operation_up ATTRIBUTE_UNUSED_RESULT |
1081 | stap_parse_argument_1 (struct stap_parse_info *p, | |
1082 | expr::operation_up &&lhs_in, | |
55aa24fb SDJ |
1083 | enum stap_operand_prec prec) |
1084 | { | |
1085 | /* This is an operator-precedence parser. | |
1086 | ||
1087 | We work with left- and right-sides of expressions, and | |
1088 | parse them depending on the precedence of the operators | |
1089 | we find. */ | |
1090 | ||
97c2dca0 SDJ |
1091 | gdb_assert (p->arg != NULL); |
1092 | ||
55aa24fb | 1093 | if (p->inside_paren_p) |
f1735a53 | 1094 | p->arg = skip_spaces (p->arg); |
55aa24fb | 1095 | |
4c5e7a93 TT |
1096 | using namespace expr; |
1097 | operation_up lhs = std::move (lhs_in); | |
1098 | if (lhs == nullptr) | |
55aa24fb SDJ |
1099 | { |
1100 | /* We were called without a left-side, either because this is the | |
1101 | first call, or because we were called to parse a parenthesized | |
1102 | expression. It doesn't really matter; we have to parse the | |
1103 | left-side in order to continue the process. */ | |
4c5e7a93 | 1104 | lhs = stap_parse_argument_conditionally (p); |
55aa24fb SDJ |
1105 | } |
1106 | ||
6f52fdf4 SDJ |
1107 | if (p->inside_paren_p) |
1108 | p->arg = skip_spaces (p->arg); | |
1109 | ||
55aa24fb SDJ |
1110 | /* Start to parse the right-side, and to "join" left and right sides |
1111 | depending on the operation specified. | |
1112 | ||
1113 | This loop shall continue until we run out of characters in the input, | |
1114 | or until we find a close-parenthesis, which means that we've reached | |
1115 | the end of a sub-expression. */ | |
97c2dca0 | 1116 | while (*p->arg != '\0' && *p->arg != ')' && !isspace (*p->arg)) |
55aa24fb SDJ |
1117 | { |
1118 | const char *tmp_exp_buf; | |
1119 | enum exp_opcode opcode; | |
1120 | enum stap_operand_prec cur_prec; | |
1121 | ||
fcf57f19 | 1122 | if (!stap_is_operator (p->arg)) |
55aa24fb SDJ |
1123 | error (_("Invalid operator `%c' on expression `%s'."), *p->arg, |
1124 | p->saved_arg); | |
1125 | ||
1126 | /* We have to save the current value of the expression buffer because | |
1127 | the `stap_get_opcode' modifies it in order to get the current | |
1128 | operator. If this operator's precedence is lower than PREC, we | |
1129 | should return and not advance the expression buffer pointer. */ | |
1130 | tmp_exp_buf = p->arg; | |
fcf57f19 | 1131 | opcode = stap_get_opcode (&tmp_exp_buf); |
55aa24fb SDJ |
1132 | |
1133 | cur_prec = stap_get_operator_prec (opcode); | |
1134 | if (cur_prec < prec) | |
1135 | { | |
1136 | /* If the precedence of the operator that we are seeing now is | |
1137 | lower than the precedence of the first operator seen before | |
1138 | this parsing process began, it means we should stop parsing | |
1139 | and return. */ | |
1140 | break; | |
1141 | } | |
1142 | ||
1143 | p->arg = tmp_exp_buf; | |
1144 | if (p->inside_paren_p) | |
f1735a53 | 1145 | p->arg = skip_spaces (p->arg); |
55aa24fb | 1146 | |
6f52fdf4 SDJ |
1147 | /* Parse the right-side of the expression. |
1148 | ||
1149 | We save whether the right-side is a parenthesized | |
1150 | subexpression because, if it is, we will have to finish | |
1151 | processing this part of the expression before continuing. */ | |
1152 | bool paren_subexp = *p->arg == '('; | |
1153 | ||
4c5e7a93 | 1154 | operation_up rhs = stap_parse_argument_conditionally (p); |
6f52fdf4 SDJ |
1155 | if (p->inside_paren_p) |
1156 | p->arg = skip_spaces (p->arg); | |
1157 | if (paren_subexp) | |
1158 | { | |
4c5e7a93 | 1159 | lhs = stap_make_binop (opcode, std::move (lhs), std::move (rhs)); |
6f52fdf4 SDJ |
1160 | continue; |
1161 | } | |
55aa24fb SDJ |
1162 | |
1163 | /* While we still have operators, try to parse another | |
1164 | right-side, but using the current right-side as a left-side. */ | |
97c2dca0 | 1165 | while (*p->arg != '\0' && stap_is_operator (p->arg)) |
55aa24fb SDJ |
1166 | { |
1167 | enum exp_opcode lookahead_opcode; | |
1168 | enum stap_operand_prec lookahead_prec; | |
1169 | ||
1170 | /* Saving the current expression buffer position. The explanation | |
1171 | is the same as above. */ | |
1172 | tmp_exp_buf = p->arg; | |
fcf57f19 | 1173 | lookahead_opcode = stap_get_opcode (&tmp_exp_buf); |
55aa24fb SDJ |
1174 | lookahead_prec = stap_get_operator_prec (lookahead_opcode); |
1175 | ||
1176 | if (lookahead_prec <= prec) | |
1177 | { | |
1178 | /* If we are dealing with an operator whose precedence is lower | |
1179 | than the first one, just abandon the attempt. */ | |
1180 | break; | |
1181 | } | |
1182 | ||
4c5e7a93 TT |
1183 | /* Parse the right-side of the expression, using the current |
1184 | right-hand-side as the left-hand-side of the new | |
1185 | subexpression. */ | |
1186 | rhs = stap_parse_argument_1 (p, std::move (rhs), lookahead_prec); | |
6f52fdf4 SDJ |
1187 | if (p->inside_paren_p) |
1188 | p->arg = skip_spaces (p->arg); | |
55aa24fb SDJ |
1189 | } |
1190 | ||
4c5e7a93 | 1191 | lhs = stap_make_binop (opcode, std::move (lhs), std::move (rhs)); |
55aa24fb | 1192 | } |
4c5e7a93 TT |
1193 | |
1194 | return lhs; | |
55aa24fb SDJ |
1195 | } |
1196 | ||
1197 | /* Parse a probe's argument. | |
1198 | ||
1199 | Assuming that: | |
1200 | ||
1201 | LP = literal integer prefix | |
1202 | LS = literal integer suffix | |
1203 | ||
1204 | RP = register prefix | |
1205 | RS = register suffix | |
1206 | ||
1207 | RIP = register indirection prefix | |
1208 | RIS = register indirection suffix | |
1209 | ||
1210 | This routine assumes that arguments' tokens are of the form: | |
1211 | ||
1212 | - [LP] NUMBER [LS] | |
1213 | - [RP] REGISTER [RS] | |
1214 | - [RIP] [RP] REGISTER [RS] [RIS] | |
1215 | - If we find a number without LP, we try to parse it as a literal integer | |
1216 | constant (if LP == NULL), or as a register displacement. | |
1217 | - We count parenthesis, and only skip whitespaces if we are inside them. | |
1218 | - If we find an operator, we skip it. | |
1219 | ||
1220 | This function can also call a special function that will try to match | |
0e9ae10f SDJ |
1221 | unknown tokens. It will return the expression_up generated from |
1222 | parsing the argument. */ | |
55aa24fb | 1223 | |
0e9ae10f | 1224 | static expression_up |
55aa24fb SDJ |
1225 | stap_parse_argument (const char **arg, struct type *atype, |
1226 | struct gdbarch *gdbarch) | |
1227 | { | |
55aa24fb | 1228 | /* We need to initialize the expression buffer, in order to begin |
f7088df3 SDJ |
1229 | our parsing efforts. We use language_c here because we may need |
1230 | to do pointer arithmetics. */ | |
1201a264 | 1231 | struct stap_parse_info p (*arg, atype, language_def (language_c), |
e9d9f57e | 1232 | gdbarch); |
55aa24fb | 1233 | |
4c5e7a93 TT |
1234 | using namespace expr; |
1235 | operation_up result = stap_parse_argument_1 (&p, {}, STAP_OPERAND_PREC_NONE); | |
55aa24fb | 1236 | |
55aa24fb SDJ |
1237 | gdb_assert (p.inside_paren_p == 0); |
1238 | ||
1239 | /* Casting the final expression to the appropriate type. */ | |
4c5e7a93 TT |
1240 | result = make_operation<unop_cast_operation> (std::move (result), atype); |
1241 | p.pstate.set_operation (std::move (result)); | |
55aa24fb | 1242 | |
f1735a53 | 1243 | p.arg = skip_spaces (p.arg); |
55aa24fb SDJ |
1244 | *arg = p.arg; |
1245 | ||
e9d9f57e | 1246 | return p.pstate.release (); |
55aa24fb SDJ |
1247 | } |
1248 | ||
0e9ae10f | 1249 | /* Implementation of 'parse_arguments' method. */ |
55aa24fb | 1250 | |
0e9ae10f SDJ |
1251 | void |
1252 | stap_probe::parse_arguments (struct gdbarch *gdbarch) | |
55aa24fb SDJ |
1253 | { |
1254 | const char *cur; | |
55aa24fb | 1255 | |
0e9ae10f SDJ |
1256 | gdb_assert (!m_have_parsed_args); |
1257 | cur = m_unparsed_args_text; | |
1258 | m_have_parsed_args = true; | |
55aa24fb | 1259 | |
97c2dca0 | 1260 | if (cur == NULL || *cur == '\0' || *cur == ':') |
55aa24fb SDJ |
1261 | return; |
1262 | ||
97c2dca0 | 1263 | while (*cur != '\0') |
55aa24fb | 1264 | { |
0e9ae10f SDJ |
1265 | enum stap_arg_bitness bitness; |
1266 | bool got_minus = false; | |
55aa24fb SDJ |
1267 | |
1268 | /* We expect to find something like: | |
1269 | ||
1270 | N@OP | |
1271 | ||
30a1e6cc | 1272 | Where `N' can be [+,-][1,2,4,8]. This is not mandatory, so |
55aa24fb SDJ |
1273 | we check it here. If we don't find it, go to the next |
1274 | state. */ | |
f33da99a SDJ |
1275 | if ((cur[0] == '-' && isdigit (cur[1]) && cur[2] == '@') |
1276 | || (isdigit (cur[0]) && cur[1] == '@')) | |
55aa24fb SDJ |
1277 | { |
1278 | if (*cur == '-') | |
1279 | { | |
1280 | /* Discard the `-'. */ | |
1281 | ++cur; | |
0e9ae10f | 1282 | got_minus = true; |
55aa24fb SDJ |
1283 | } |
1284 | ||
30a1e6cc SDJ |
1285 | /* Defining the bitness. */ |
1286 | switch (*cur) | |
55aa24fb | 1287 | { |
30a1e6cc | 1288 | case '1': |
0e9ae10f SDJ |
1289 | bitness = (got_minus ? STAP_ARG_BITNESS_8BIT_SIGNED |
1290 | : STAP_ARG_BITNESS_8BIT_UNSIGNED); | |
30a1e6cc SDJ |
1291 | break; |
1292 | ||
1293 | case '2': | |
0e9ae10f SDJ |
1294 | bitness = (got_minus ? STAP_ARG_BITNESS_16BIT_SIGNED |
1295 | : STAP_ARG_BITNESS_16BIT_UNSIGNED); | |
30a1e6cc SDJ |
1296 | break; |
1297 | ||
1298 | case '4': | |
0e9ae10f SDJ |
1299 | bitness = (got_minus ? STAP_ARG_BITNESS_32BIT_SIGNED |
1300 | : STAP_ARG_BITNESS_32BIT_UNSIGNED); | |
30a1e6cc SDJ |
1301 | break; |
1302 | ||
1303 | case '8': | |
0e9ae10f SDJ |
1304 | bitness = (got_minus ? STAP_ARG_BITNESS_64BIT_SIGNED |
1305 | : STAP_ARG_BITNESS_64BIT_UNSIGNED); | |
30a1e6cc SDJ |
1306 | break; |
1307 | ||
1308 | default: | |
1309 | { | |
1310 | /* We have an error, because we don't expect anything | |
1311 | except 1, 2, 4 and 8. */ | |
1312 | warning (_("unrecognized bitness %s%c' for probe `%s'"), | |
0e9ae10f SDJ |
1313 | got_minus ? "`-" : "`", *cur, |
1314 | this->get_name ().c_str ()); | |
30a1e6cc SDJ |
1315 | return; |
1316 | } | |
55aa24fb | 1317 | } |
55aa24fb SDJ |
1318 | /* Discard the number and the `@' sign. */ |
1319 | cur += 2; | |
1320 | } | |
f33da99a | 1321 | else |
0e9ae10f | 1322 | bitness = STAP_ARG_BITNESS_UNDEFINED; |
f33da99a | 1323 | |
0e9ae10f SDJ |
1324 | struct type *atype |
1325 | = stap_get_expected_argument_type (gdbarch, bitness, | |
1326 | this->get_name ().c_str ()); | |
55aa24fb | 1327 | |
0e9ae10f | 1328 | expression_up expr = stap_parse_argument (&cur, atype, gdbarch); |
55aa24fb | 1329 | |
55aa24fb | 1330 | if (stap_expression_debug) |
19707310 | 1331 | expr->dump (gdb_stdlog); |
55aa24fb | 1332 | |
0e9ae10f | 1333 | m_parsed_args.emplace_back (bitness, atype, std::move (expr)); |
55aa24fb SDJ |
1334 | |
1335 | /* Start it over again. */ | |
f1735a53 | 1336 | cur = skip_spaces (cur); |
55aa24fb SDJ |
1337 | } |
1338 | } | |
1339 | ||
685de8c2 SDJ |
1340 | /* Helper function to relocate an address. */ |
1341 | ||
1342 | static CORE_ADDR | |
1343 | relocate_address (CORE_ADDR address, struct objfile *objfile) | |
1344 | { | |
3ceda722 | 1345 | return address + objfile->text_section_offset (); |
685de8c2 SDJ |
1346 | } |
1347 | ||
0e9ae10f | 1348 | /* Implementation of the get_relocated_address method. */ |
729662a5 | 1349 | |
0e9ae10f SDJ |
1350 | CORE_ADDR |
1351 | stap_probe::get_relocated_address (struct objfile *objfile) | |
729662a5 | 1352 | { |
685de8c2 | 1353 | return relocate_address (this->get_address (), objfile); |
729662a5 TT |
1354 | } |
1355 | ||
55aa24fb SDJ |
1356 | /* Given PROBE, returns the number of arguments present in that probe's |
1357 | argument string. */ | |
1358 | ||
0e9ae10f | 1359 | unsigned |
fe01123e | 1360 | stap_probe::get_argument_count (struct gdbarch *gdbarch) |
55aa24fb | 1361 | { |
0e9ae10f | 1362 | if (!m_have_parsed_args) |
25f9533e | 1363 | { |
0e9ae10f SDJ |
1364 | if (this->can_evaluate_arguments ()) |
1365 | this->parse_arguments (gdbarch); | |
25f9533e SDJ |
1366 | else |
1367 | { | |
af2d9bee | 1368 | static bool have_warned_stap_incomplete = false; |
25f9533e SDJ |
1369 | |
1370 | if (!have_warned_stap_incomplete) | |
1371 | { | |
1372 | warning (_( | |
1373 | "The SystemTap SDT probe support is not fully implemented on this target;\n" | |
1374 | "you will not be able to inspect the arguments of the probes.\n" | |
1375 | "Please report a bug against GDB requesting a port to this target.")); | |
af2d9bee | 1376 | have_warned_stap_incomplete = true; |
25f9533e SDJ |
1377 | } |
1378 | ||
1379 | /* Marking the arguments as "already parsed". */ | |
0e9ae10f | 1380 | m_have_parsed_args = true; |
25f9533e SDJ |
1381 | } |
1382 | } | |
55aa24fb | 1383 | |
0e9ae10f SDJ |
1384 | gdb_assert (m_have_parsed_args); |
1385 | return m_parsed_args.size (); | |
55aa24fb SDJ |
1386 | } |
1387 | ||
af2d9bee SDJ |
1388 | /* Return true if OP is a valid operator inside a probe argument, or |
1389 | false otherwise. */ | |
55aa24fb | 1390 | |
af2d9bee | 1391 | static bool |
fcf57f19 | 1392 | stap_is_operator (const char *op) |
55aa24fb | 1393 | { |
af2d9bee | 1394 | bool ret = true; |
fcf57f19 SDJ |
1395 | |
1396 | switch (*op) | |
1397 | { | |
1398 | case '*': | |
1399 | case '/': | |
1400 | case '%': | |
1401 | case '^': | |
1402 | case '!': | |
1403 | case '+': | |
1404 | case '-': | |
1405 | case '<': | |
1406 | case '>': | |
1407 | case '|': | |
1408 | case '&': | |
1409 | break; | |
1410 | ||
1411 | case '=': | |
1412 | if (op[1] != '=') | |
af2d9bee | 1413 | ret = false; |
fcf57f19 SDJ |
1414 | break; |
1415 | ||
1416 | default: | |
1417 | /* We didn't find any operator. */ | |
af2d9bee | 1418 | ret = false; |
fcf57f19 SDJ |
1419 | } |
1420 | ||
1421 | return ret; | |
55aa24fb SDJ |
1422 | } |
1423 | ||
0e9ae10f | 1424 | /* Implement the `can_evaluate_arguments' method. */ |
f469e8ce | 1425 | |
0e9ae10f SDJ |
1426 | bool |
1427 | stap_probe::can_evaluate_arguments () const | |
25f9533e | 1428 | { |
0e9ae10f | 1429 | struct gdbarch *gdbarch = this->get_gdbarch (); |
25f9533e SDJ |
1430 | |
1431 | /* For SystemTap probes, we have to guarantee that the method | |
1432 | stap_is_single_operand is defined on gdbarch. If it is not, then it | |
1433 | means that argument evaluation is not implemented on this target. */ | |
1434 | return gdbarch_stap_is_single_operand_p (gdbarch); | |
1435 | } | |
1436 | ||
55aa24fb SDJ |
1437 | /* Evaluate the probe's argument N (indexed from 0), returning a value |
1438 | corresponding to it. Assertion is thrown if N does not exist. */ | |
1439 | ||
0e9ae10f | 1440 | struct value * |
bd2b40ac | 1441 | stap_probe::evaluate_argument (unsigned n, frame_info_ptr frame) |
55aa24fb | 1442 | { |
55aa24fb | 1443 | struct stap_probe_arg *arg; |
0e9ae10f | 1444 | struct gdbarch *gdbarch = get_frame_arch (frame); |
55aa24fb | 1445 | |
0e9ae10f | 1446 | arg = this->get_arg_by_number (n, gdbarch); |
43048e46 | 1447 | return arg->aexpr->evaluate (arg->atype); |
55aa24fb SDJ |
1448 | } |
1449 | ||
1450 | /* Compile the probe's argument N (indexed from 0) to agent expression. | |
1451 | Assertion is thrown if N does not exist. */ | |
1452 | ||
0e9ae10f SDJ |
1453 | void |
1454 | stap_probe::compile_to_ax (struct agent_expr *expr, struct axs_value *value, | |
1455 | unsigned n) | |
55aa24fb | 1456 | { |
55aa24fb | 1457 | struct stap_probe_arg *arg; |
55aa24fb | 1458 | |
0e9ae10f | 1459 | arg = this->get_arg_by_number (n, expr->gdbarch); |
55aa24fb | 1460 | |
1eaebe02 | 1461 | arg->aexpr->op->generate_ax (arg->aexpr.get (), expr, value); |
55aa24fb SDJ |
1462 | |
1463 | require_rvalue (expr, value); | |
1464 | value->type = arg->atype; | |
1465 | } | |
55aa24fb SDJ |
1466 | \f |
1467 | ||
55aa24fb | 1468 | /* Set or clear a SystemTap semaphore. ADDRESS is the semaphore's |
0e9ae10f SDJ |
1469 | address. SET is zero if the semaphore should be cleared, or one if |
1470 | it should be set. This is a helper function for | |
1471 | 'stap_probe::set_semaphore' and 'stap_probe::clear_semaphore'. */ | |
55aa24fb SDJ |
1472 | |
1473 | static void | |
1474 | stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch) | |
1475 | { | |
1476 | gdb_byte bytes[sizeof (LONGEST)]; | |
1477 | /* The ABI specifies "unsigned short". */ | |
1478 | struct type *type = builtin_type (gdbarch)->builtin_unsigned_short; | |
1479 | ULONGEST value; | |
1480 | ||
55aa24fb | 1481 | /* Swallow errors. */ |
df86565b | 1482 | if (target_read_memory (address, bytes, type->length ()) != 0) |
55aa24fb SDJ |
1483 | { |
1484 | warning (_("Could not read the value of a SystemTap semaphore.")); | |
1485 | return; | |
1486 | } | |
1487 | ||
34877895 | 1488 | enum bfd_endian byte_order = type_byte_order (type); |
df86565b | 1489 | value = extract_unsigned_integer (bytes, type->length (), byte_order); |
55aa24fb SDJ |
1490 | /* Note that we explicitly don't worry about overflow or |
1491 | underflow. */ | |
1492 | if (set) | |
1493 | ++value; | |
1494 | else | |
1495 | --value; | |
1496 | ||
df86565b | 1497 | store_unsigned_integer (bytes, type->length (), byte_order, value); |
55aa24fb | 1498 | |
df86565b | 1499 | if (target_write_memory (address, bytes, type->length ()) != 0) |
55aa24fb SDJ |
1500 | warning (_("Could not write the value of a SystemTap semaphore.")); |
1501 | } | |
1502 | ||
0e9ae10f | 1503 | /* Implementation of the 'set_semaphore' method. |
55aa24fb | 1504 | |
0e9ae10f SDJ |
1505 | SystemTap semaphores act as reference counters, so calls to this |
1506 | function must be paired with calls to 'clear_semaphore'. | |
55aa24fb | 1507 | |
0e9ae10f SDJ |
1508 | This function and 'clear_semaphore' race with another tool |
1509 | changing the probes, but that is too rare to care. */ | |
1510 | ||
1511 | void | |
1512 | stap_probe::set_semaphore (struct objfile *objfile, struct gdbarch *gdbarch) | |
55aa24fb | 1513 | { |
7f0ae84c GB |
1514 | if (m_sem_addr == 0) |
1515 | return; | |
685de8c2 | 1516 | stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 1, gdbarch); |
0e9ae10f | 1517 | } |
55aa24fb | 1518 | |
0e9ae10f | 1519 | /* Implementation of the 'clear_semaphore' method. */ |
55aa24fb | 1520 | |
0e9ae10f SDJ |
1521 | void |
1522 | stap_probe::clear_semaphore (struct objfile *objfile, struct gdbarch *gdbarch) | |
1523 | { | |
7f0ae84c GB |
1524 | if (m_sem_addr == 0) |
1525 | return; | |
685de8c2 | 1526 | stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 0, gdbarch); |
55aa24fb SDJ |
1527 | } |
1528 | ||
0e9ae10f | 1529 | /* Implementation of the 'get_static_ops' method. */ |
55aa24fb | 1530 | |
0e9ae10f SDJ |
1531 | const static_probe_ops * |
1532 | stap_probe::get_static_ops () const | |
1533 | { | |
1534 | return &stap_static_probe_ops; | |
1535 | } | |
1536 | ||
1537 | /* Implementation of the 'gen_info_probes_table_values' method. */ | |
1538 | ||
1539 | std::vector<const char *> | |
1540 | stap_probe::gen_info_probes_table_values () const | |
55aa24fb | 1541 | { |
0e9ae10f | 1542 | const char *val = NULL; |
55aa24fb | 1543 | |
0e9ae10f SDJ |
1544 | if (m_sem_addr != 0) |
1545 | val = print_core_address (this->get_gdbarch (), m_sem_addr); | |
55aa24fb | 1546 | |
0e9ae10f | 1547 | return std::vector<const char *> { val }; |
55aa24fb SDJ |
1548 | } |
1549 | ||
55aa24fb SDJ |
1550 | /* Helper function that parses the information contained in a |
1551 | SystemTap's probe. Basically, the information consists in: | |
1552 | ||
1553 | - Probe's PC address; | |
1554 | - Link-time section address of `.stapsdt.base' section; | |
1555 | - Link-time address of the semaphore variable, or ZERO if the | |
1556 | probe doesn't have an associated semaphore; | |
1557 | - Probe's provider name; | |
1558 | - Probe's name; | |
3ca58cde | 1559 | - Probe's argument format. */ |
55aa24fb SDJ |
1560 | |
1561 | static void | |
1562 | handle_stap_probe (struct objfile *objfile, struct sdt_note *el, | |
814cf43a TT |
1563 | std::vector<std::unique_ptr<probe>> *probesp, |
1564 | CORE_ADDR base) | |
55aa24fb | 1565 | { |
98badbfd | 1566 | bfd *abfd = objfile->obfd.get (); |
55aa24fb | 1567 | int size = bfd_get_arch_size (abfd) / 8; |
08feed99 | 1568 | struct gdbarch *gdbarch = objfile->arch (); |
55aa24fb | 1569 | struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr; |
55aa24fb SDJ |
1570 | |
1571 | /* Provider and the name of the probe. */ | |
0e9ae10f SDJ |
1572 | const char *provider = (const char *) &el->data[3 * size]; |
1573 | const char *name = ((const char *) | |
1574 | memchr (provider, '\0', | |
1575 | (char *) el->data + el->size - provider)); | |
55aa24fb | 1576 | /* Making sure there is a name. */ |
0e9ae10f | 1577 | if (name == NULL) |
55aa24fb | 1578 | { |
f3da9116 | 1579 | complaint (_("corrupt probe name when reading `%s'"), |
4262abfb | 1580 | objfile_name (objfile)); |
55aa24fb SDJ |
1581 | |
1582 | /* There is no way to use a probe without a name or a provider, so | |
f3da9116 | 1583 | returning here makes sense. */ |
55aa24fb SDJ |
1584 | return; |
1585 | } | |
1586 | else | |
0e9ae10f | 1587 | ++name; |
55aa24fb SDJ |
1588 | |
1589 | /* Retrieving the probe's address. */ | |
0e9ae10f | 1590 | CORE_ADDR address = extract_typed_address (&el->data[0], ptr_type); |
55aa24fb SDJ |
1591 | |
1592 | /* Link-time sh_addr of `.stapsdt.base' section. */ | |
0e9ae10f | 1593 | CORE_ADDR base_ref = extract_typed_address (&el->data[size], ptr_type); |
55aa24fb SDJ |
1594 | |
1595 | /* Semaphore address. */ | |
0e9ae10f | 1596 | CORE_ADDR sem_addr = extract_typed_address (&el->data[2 * size], ptr_type); |
55aa24fb | 1597 | |
0e9ae10f SDJ |
1598 | address += base - base_ref; |
1599 | if (sem_addr != 0) | |
1600 | sem_addr += base - base_ref; | |
55aa24fb SDJ |
1601 | |
1602 | /* Arguments. We can only extract the argument format if there is a valid | |
1603 | name for this probe. */ | |
0e9ae10f SDJ |
1604 | const char *probe_args = ((const char*) |
1605 | memchr (name, '\0', | |
1606 | (char *) el->data + el->size - name)); | |
55aa24fb SDJ |
1607 | |
1608 | if (probe_args != NULL) | |
1609 | ++probe_args; | |
1610 | ||
97c2dca0 | 1611 | if (probe_args == NULL |
0e9ae10f | 1612 | || (memchr (probe_args, '\0', (char *) el->data + el->size - name) |
97c2dca0 | 1613 | != el->data + el->size - 1)) |
55aa24fb | 1614 | { |
f3da9116 | 1615 | complaint (_("corrupt probe argument when reading `%s'"), |
4262abfb | 1616 | objfile_name (objfile)); |
55aa24fb | 1617 | /* If the argument string is NULL, it means some problem happened with |
f3da9116 | 1618 | it. So we return. */ |
55aa24fb SDJ |
1619 | return; |
1620 | } | |
1621 | ||
08c59458 TV |
1622 | if (ignore_probe_p (provider, name, objfile_name (objfile), "SystemTap")) |
1623 | return; | |
1624 | ||
0e9ae10f SDJ |
1625 | stap_probe *ret = new stap_probe (std::string (name), std::string (provider), |
1626 | address, gdbarch, sem_addr, probe_args); | |
55aa24fb SDJ |
1627 | |
1628 | /* Successfully created probe. */ | |
814cf43a | 1629 | probesp->emplace_back (ret); |
55aa24fb SDJ |
1630 | } |
1631 | ||
55aa24fb SDJ |
1632 | /* Helper function which iterates over every section in the BFD file, |
1633 | trying to find the base address of the SystemTap base section. | |
1634 | Returns 1 if found (setting BASE to the proper value), zero otherwise. */ | |
1635 | ||
1636 | static int | |
1637 | get_stap_base_address (bfd *obfd, bfd_vma *base) | |
1638 | { | |
1639 | asection *ret = NULL; | |
1640 | ||
3cabfd26 TT |
1641 | for (asection *sect : gdb_bfd_sections (obfd)) |
1642 | if ((sect->flags & (SEC_DATA | SEC_ALLOC | SEC_HAS_CONTENTS)) | |
1643 | && sect->name && !strcmp (sect->name, STAP_BASE_SECTION_NAME)) | |
1644 | ret = sect; | |
55aa24fb | 1645 | |
97c2dca0 | 1646 | if (ret == NULL) |
55aa24fb | 1647 | { |
b98664d3 | 1648 | complaint (_("could not obtain base address for " |
55aa24fb | 1649 | "SystemTap section on objfile `%s'."), |
c7e97679 | 1650 | bfd_get_filename (obfd)); |
55aa24fb SDJ |
1651 | return 0; |
1652 | } | |
1653 | ||
97c2dca0 | 1654 | if (base != NULL) |
55aa24fb SDJ |
1655 | *base = ret->vma; |
1656 | ||
1657 | return 1; | |
1658 | } | |
1659 | ||
0e9ae10f | 1660 | /* Implementation of the 'is_linespec' method. */ |
55aa24fb | 1661 | |
0e9ae10f SDJ |
1662 | bool |
1663 | stap_static_probe_ops::is_linespec (const char **linespecp) const | |
1664 | { | |
1665 | static const char *const keywords[] = { "-pstap", "-probe-stap", NULL }; | |
1666 | ||
1667 | return probe_is_linespec_by_keyword (linespecp, keywords); | |
1668 | } | |
1669 | ||
1670 | /* Implementation of the 'get_probes' method. */ | |
1671 | ||
1672 | void | |
814cf43a TT |
1673 | stap_static_probe_ops::get_probes |
1674 | (std::vector<std::unique_ptr<probe>> *probesp, | |
1675 | struct objfile *objfile) const | |
55aa24fb SDJ |
1676 | { |
1677 | /* If we are here, then this is the first time we are parsing the | |
1678 | SystemTap probe's information. We basically have to count how many | |
1679 | probes the objfile has, and then fill in the necessary information | |
1680 | for each one. */ | |
98badbfd | 1681 | bfd *obfd = objfile->obfd.get (); |
55aa24fb SDJ |
1682 | bfd_vma base; |
1683 | struct sdt_note *iter; | |
aaa63a31 | 1684 | unsigned save_probesp_len = probesp->size (); |
55aa24fb | 1685 | |
d7333987 SDJ |
1686 | if (objfile->separate_debug_objfile_backlink != NULL) |
1687 | { | |
1688 | /* This is a .debug file, not the objfile itself. */ | |
1689 | return; | |
1690 | } | |
1691 | ||
97c2dca0 | 1692 | if (elf_tdata (obfd)->sdt_note_head == NULL) |
55aa24fb SDJ |
1693 | { |
1694 | /* There isn't any probe here. */ | |
1695 | return; | |
1696 | } | |
1697 | ||
1698 | if (!get_stap_base_address (obfd, &base)) | |
1699 | { | |
1700 | /* There was an error finding the base address for the section. | |
1701 | Just return NULL. */ | |
1702 | return; | |
1703 | } | |
1704 | ||
1705 | /* Parsing each probe's information. */ | |
97c2dca0 SDJ |
1706 | for (iter = elf_tdata (obfd)->sdt_note_head; |
1707 | iter != NULL; | |
1708 | iter = iter->next) | |
55aa24fb SDJ |
1709 | { |
1710 | /* We first have to handle all the information about the | |
1711 | probe which is present in the section. */ | |
1712 | handle_stap_probe (objfile, iter, probesp, base); | |
1713 | } | |
1714 | ||
aaa63a31 | 1715 | if (save_probesp_len == probesp->size ()) |
55aa24fb SDJ |
1716 | { |
1717 | /* If we are here, it means we have failed to parse every known | |
1718 | probe. */ | |
f3da9116 | 1719 | complaint (_("could not parse SystemTap probe(s) from inferior")); |
55aa24fb SDJ |
1720 | return; |
1721 | } | |
1722 | } | |
1723 | ||
6f9b8491 JM |
1724 | /* Implementation of the type_name method. */ |
1725 | ||
0e9ae10f SDJ |
1726 | const char * |
1727 | stap_static_probe_ops::type_name () const | |
6f9b8491 | 1728 | { |
6f9b8491 JM |
1729 | return "stap"; |
1730 | } | |
1731 | ||
0e9ae10f | 1732 | /* Implementation of the 'gen_info_probes_table_header' method. */ |
55aa24fb | 1733 | |
0e9ae10f SDJ |
1734 | std::vector<struct info_probe_column> |
1735 | stap_static_probe_ops::gen_info_probes_table_header () const | |
55aa24fb | 1736 | { |
0e9ae10f | 1737 | struct info_probe_column stap_probe_column; |
55aa24fb SDJ |
1738 | |
1739 | stap_probe_column.field_name = "semaphore"; | |
1740 | stap_probe_column.print_name = _("Semaphore"); | |
1741 | ||
0e9ae10f | 1742 | return std::vector<struct info_probe_column> { stap_probe_column }; |
55aa24fb SDJ |
1743 | } |
1744 | ||
55aa24fb SDJ |
1745 | /* Implementation of the `info probes stap' command. */ |
1746 | ||
1747 | static void | |
884beb0c | 1748 | info_probes_stap_command (const char *arg, int from_tty) |
55aa24fb | 1749 | { |
0e9ae10f | 1750 | info_probes_for_spops (arg, from_tty, &stap_static_probe_ops); |
55aa24fb SDJ |
1751 | } |
1752 | ||
6c265988 | 1753 | void _initialize_stap_probe (); |
55aa24fb | 1754 | void |
6c265988 | 1755 | _initialize_stap_probe () |
55aa24fb | 1756 | { |
0e9ae10f | 1757 | all_static_probe_ops.push_back (&stap_static_probe_ops); |
55aa24fb | 1758 | |
ccce17b0 YQ |
1759 | add_setshow_zuinteger_cmd ("stap-expression", class_maintenance, |
1760 | &stap_expression_debug, | |
1761 | _("Set SystemTap expression debugging."), | |
1762 | _("Show SystemTap expression debugging."), | |
1763 | _("When non-zero, the internal representation " | |
1764 | "of SystemTap expressions will be printed."), | |
1765 | NULL, | |
1766 | show_stapexpressiondebug, | |
1767 | &setdebuglist, &showdebuglist); | |
55aa24fb | 1768 | |
55aa24fb SDJ |
1769 | add_cmd ("stap", class_info, info_probes_stap_command, |
1770 | _("\ | |
1771 | Show information about SystemTap static probes.\n\ | |
1772 | Usage: info probes stap [PROVIDER [NAME [OBJECT]]]\n\ | |
1773 | Each argument is a regular expression, used to select probes.\n\ | |
1774 | PROVIDER matches probe provider names.\n\ | |
1775 | NAME matches the probe names.\n\ | |
1776 | OBJECT matches the executable or shared library name."), | |
1777 | info_probes_cmdlist_get ()); | |
1778 | ||
4c5e7a93 TT |
1779 | |
1780 | using namespace expr; | |
1781 | stap_maker_map[BINOP_ADD] = make_operation<add_operation>; | |
1782 | stap_maker_map[BINOP_BITWISE_AND] = make_operation<bitwise_and_operation>; | |
1783 | stap_maker_map[BINOP_BITWISE_IOR] = make_operation<bitwise_ior_operation>; | |
1784 | stap_maker_map[BINOP_BITWISE_XOR] = make_operation<bitwise_xor_operation>; | |
1785 | stap_maker_map[BINOP_DIV] = make_operation<div_operation>; | |
1786 | stap_maker_map[BINOP_EQUAL] = make_operation<equal_operation>; | |
1787 | stap_maker_map[BINOP_GEQ] = make_operation<geq_operation>; | |
1788 | stap_maker_map[BINOP_GTR] = make_operation<gtr_operation>; | |
1789 | stap_maker_map[BINOP_LEQ] = make_operation<leq_operation>; | |
1790 | stap_maker_map[BINOP_LESS] = make_operation<less_operation>; | |
1791 | stap_maker_map[BINOP_LOGICAL_AND] = make_operation<logical_and_operation>; | |
1792 | stap_maker_map[BINOP_LOGICAL_OR] = make_operation<logical_or_operation>; | |
1793 | stap_maker_map[BINOP_LSH] = make_operation<lsh_operation>; | |
1794 | stap_maker_map[BINOP_MUL] = make_operation<mul_operation>; | |
1795 | stap_maker_map[BINOP_NOTEQUAL] = make_operation<notequal_operation>; | |
1796 | stap_maker_map[BINOP_REM] = make_operation<rem_operation>; | |
1797 | stap_maker_map[BINOP_RSH] = make_operation<rsh_operation>; | |
1798 | stap_maker_map[BINOP_SUB] = make_operation<sub_operation>; | |
55aa24fb | 1799 | } |