probe.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* Generic SDT 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. #if !defined (PROBE_H)
  15. #define PROBE_H 1
  16. #include "symtab.h"
  17. struct event_location;
  18. struct linespec_result;
  19. /* Structure useful for passing the header names in the method
  20. `gen_ui_out_table_header'. */
  21. struct info_probe_column
  22. {
  23. /* The internal name of the field. This string cannot be capitalized nor
  24. localized, e.g., "extra_field". */
  25. const char *field_name;
  26. /* The field name to be printed in the `info probes' command. This
  27. string can be capitalized and localized, e.g., _("Extra Field"). */
  28. const char *print_name;
  29. };
  30. /* Operations that act on probes, but are specific to each backend.
  31. These methods do not go into the 'class probe' because they do not
  32. act on a single probe; instead, they are used to operate on many
  33. probes at once, or to provide information about the probe backend
  34. itself, instead of a single probe.
  35. Each probe backend needs to inherit this class and implement all of
  36. the virtual functions specified here. Then, an object shall be
  37. instantiated and added (or "registered") to the
  38. ALL_STATIC_PROBE_OPS vector so that the frontend probe interface
  39. can use it in the generic probe functions. */
  40. class static_probe_ops
  41. {
  42. public:
  43. /* Method responsible for verifying if LINESPECP is a valid linespec
  44. for a probe breakpoint. It should return true if it is, or false
  45. if it is not. It also should update LINESPECP in order to
  46. discard the breakpoint option associated with this linespec. For
  47. example, if the option is `-probe', and the LINESPECP is `-probe
  48. abc', the function should return 1 and set LINESPECP to
  49. `abc'. */
  50. virtual bool is_linespec (const char **linespecp) const = 0;
  51. /* Function that should fill PROBES with known probes from OBJFILE. */
  52. virtual void get_probes (std::vector<std::unique_ptr<probe>> *probes,
  53. struct objfile *objfile) const = 0;
  54. /* Return a pointer to a name identifying the probe type. This is
  55. the string that will be displayed in the "Type" column of the
  56. `info probes' command. */
  57. virtual const char *type_name () const = 0;
  58. /* Return true if the probe can be enabled; false otherwise. */
  59. virtual bool can_enable () const
  60. {
  61. return false;
  62. }
  63. /* Function responsible for providing the extra fields that will be
  64. printed in the `info probes' command. It should fill HEADS
  65. with whatever extra fields it needs. If no extra fields are
  66. required by the probe backend, the method EMIT_INFO_PROBES_FIELDS
  67. should return false. */
  68. virtual std::vector<struct info_probe_column>
  69. gen_info_probes_table_header () const = 0;
  70. };
  71. /* Definition of a vector of static_probe_ops. */
  72. extern std::vector<const static_probe_ops *> all_static_probe_ops;
  73. /* Helper function that, given KEYWORDS, iterate over it trying to match
  74. each keyword with LINESPECP. If it succeeds, it updates the LINESPECP
  75. pointer and returns 1. Otherwise, nothing is done to LINESPECP and zero
  76. is returned. */
  77. extern int probe_is_linespec_by_keyword (const char **linespecp,
  78. const char *const *keywords);
  79. /* Return specific STATIC_PROBE_OPS * matching *LINESPECP and possibly
  80. updating LINESPECP to skip its "-probe-type " prefix. Return
  81. &static_probe_ops_any if LINESPECP matches "-probe ", that is any
  82. unspecific probe. Return NULL if LINESPECP is not identified as
  83. any known probe type, *LINESPECP is not modified in such case. */
  84. extern const static_probe_ops *
  85. probe_linespec_to_static_ops (const char **linespecp);
  86. /* The probe itself. The class contains generic information about the
  87. probe. */
  88. class probe
  89. {
  90. public:
  91. /* Default constructor for a probe. */
  92. probe (std::string &&name_, std::string &&provider_, CORE_ADDR address_,
  93. struct gdbarch *arch_)
  94. : m_name (std::move (name_)), m_provider (std::move (provider_)),
  95. m_address (address_), m_arch (arch_)
  96. {}
  97. /* Virtual destructor. */
  98. virtual ~probe ()
  99. {}
  100. /* Compute the probe's relocated address. OBJFILE is the objfile
  101. in which the probe originated. */
  102. virtual CORE_ADDR get_relocated_address (struct objfile *objfile) = 0;
  103. /* Return the number of arguments of the probe. This function can
  104. throw an exception. */
  105. virtual unsigned get_argument_count (struct gdbarch *gdbarch) = 0;
  106. /* Return 1 if the probe interface can evaluate the arguments of
  107. probe, zero otherwise. See the comments on
  108. sym_probe_fns:can_evaluate_probe_arguments for more
  109. details. */
  110. virtual bool can_evaluate_arguments () const = 0;
  111. /* Evaluate the Nth argument from the probe, returning a value
  112. corresponding to it. The argument number is represented N.
  113. This function can throw an exception. */
  114. virtual struct value *evaluate_argument (unsigned n,
  115. struct frame_info *frame) = 0;
  116. /* Compile the Nth argument of the probe to an agent expression.
  117. The argument number is represented by N. */
  118. virtual void compile_to_ax (struct agent_expr *aexpr,
  119. struct axs_value *axs_value,
  120. unsigned n) = 0;
  121. /* Set the semaphore associated with the probe. This function only
  122. makes sense if the probe has a concept of semaphore associated to
  123. a probe. */
  124. virtual void set_semaphore (struct objfile *objfile,
  125. struct gdbarch *gdbarch)
  126. {}
  127. /* Clear the semaphore associated with the probe. This function
  128. only makes sense if the probe has a concept of semaphore
  129. associated to a probe. */
  130. virtual void clear_semaphore (struct objfile *objfile,
  131. struct gdbarch *gdbarch)
  132. {}
  133. /* Return the pointer to the static_probe_ops instance related to
  134. the probe type. */
  135. virtual const static_probe_ops *get_static_ops () const = 0;
  136. /* Function that will fill VALUES with the values of the extra
  137. fields to be printed for the probe.
  138. If the backend implements the `gen_ui_out_table_header' method,
  139. then it should implement this method as well. The backend should
  140. also guarantee that the order and the number of values in the
  141. vector is exactly the same as the order of the extra fields
  142. provided in the method `gen_ui_out_table_header'. If a certain
  143. field is to be skipped when printing the information, you can
  144. push a NULL value in that position in the vector. */
  145. virtual std::vector<const char *> gen_info_probes_table_values () const
  146. {
  147. return std::vector<const char *> ();
  148. }
  149. /* Enable the probe. The semantics of "enabling" a probe depend on
  150. the specific backend. This function can throw an exception. */
  151. virtual void enable ()
  152. {}
  153. /* Disable the probe. The semantics of "disabling" a probe depend
  154. on the specific backend. This function can throw an
  155. exception. */
  156. virtual void disable ()
  157. {}
  158. /* Getter for M_NAME. */
  159. const std::string &get_name () const
  160. {
  161. return m_name;
  162. }
  163. /* Getter for M_PROVIDER. */
  164. const std::string &get_provider () const
  165. {
  166. return m_provider;
  167. }
  168. /* Getter for M_ADDRESS. */
  169. CORE_ADDR get_address () const
  170. {
  171. return m_address;
  172. }
  173. /* Getter for M_ARCH. */
  174. struct gdbarch *get_gdbarch () const
  175. {
  176. return m_arch;
  177. }
  178. private:
  179. /* The name of the probe. */
  180. std::string m_name;
  181. /* The provider of the probe. It generally defaults to the name of
  182. the objfile which contains the probe. */
  183. std::string m_provider;
  184. /* The address where the probe is inserted, relative to
  185. SECT_OFF_TEXT. */
  186. CORE_ADDR m_address;
  187. /* The probe's architecture. */
  188. struct gdbarch *m_arch;
  189. };
  190. /* A bound probe holds a pointer to a probe and a pointer to the
  191. probe's defining objfile. This is needed because probes are
  192. independent of the program space and thus require relocation at
  193. their point of use. */
  194. struct bound_probe
  195. {
  196. /* Create an empty bound_probe object. */
  197. bound_probe ()
  198. {}
  199. /* Create and initialize a bound_probe object using PROBE and OBJFILE. */
  200. bound_probe (probe *probe_, struct objfile *objfile_)
  201. : prob (probe_), objfile (objfile_)
  202. {}
  203. /* The probe. */
  204. probe *prob = NULL;
  205. /* The objfile in which the probe originated. */
  206. struct objfile *objfile = NULL;
  207. };
  208. /* A helper for linespec that decodes a probe specification. It
  209. returns a std::vector<symtab_and_line> object and updates LOC or
  210. throws an error. */
  211. extern std::vector<symtab_and_line> parse_probes
  212. (const struct event_location *loc,
  213. struct program_space *pspace,
  214. struct linespec_result *canon);
  215. /* Given a PC, find an associated probe. If a probe is found, return
  216. it. If no probe is found, return a bound probe whose fields are
  217. both NULL. */
  218. extern struct bound_probe find_probe_by_pc (CORE_ADDR pc);
  219. /* Search OBJFILE for a probe with the given PROVIDER, NAME. Return a
  220. vector of all probes that were found. If no matching probe is found,
  221. return an empty vector. */
  222. extern std::vector<probe *> find_probes_in_objfile (struct objfile *objfile,
  223. const char *provider,
  224. const char *name);
  225. /* Generate a `info probes' command output for probes associated with
  226. SPOPS. If SPOPS is related to the "any probe" type, then all probe
  227. types are considered. It is a helper function that can be used by
  228. the probe backends to print their `info probe TYPE'. */
  229. extern void info_probes_for_spops (const char *arg, int from_tty,
  230. const static_probe_ops *spops);
  231. /* Return the `cmd_list_element' associated with the `info probes' command,
  232. or create a new one if it doesn't exist. Helper function that serves the
  233. purpose of avoiding the case of a backend using the `cmd_list_element'
  234. associated with `info probes', without having it registered yet. */
  235. extern struct cmd_list_element **info_probes_cmdlist_get (void);
  236. /* A convenience function that finds a probe at the PC in FRAME and
  237. evaluates argument N, with 0 <= N < number_of_args. If there is no
  238. probe at that location, or if the probe does not have enough arguments,
  239. this returns NULL. */
  240. extern struct value *probe_safe_evaluate_at_pc (struct frame_info *frame,
  241. unsigned n);
  242. #endif /* !defined (PROBE_H) */