stap-probe.c 51 KB

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