Explicit locations: introduce probe locations
[external/binutils.git] / gdb / probe.c
1 /* Generic static probe support for GDB.
2
3    Copyright (C) 2012-2015 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "probe.h"
22 #include "command.h"
23 #include "cli/cli-cmds.h"
24 #include "cli/cli-utils.h"
25 #include "objfiles.h"
26 #include "symtab.h"
27 #include "progspace.h"
28 #include "filenames.h"
29 #include "linespec.h"
30 #include "gdb_regex.h"
31 #include "frame.h"
32 #include "arch-utils.h"
33 #include "value.h"
34 #include "ax.h"
35 #include "ax-gdb.h"
36 #include "location.h"
37 #include <ctype.h>
38
39 typedef struct bound_probe bound_probe_s;
40 DEF_VEC_O (bound_probe_s);
41
42 \f
43
44 /* See definition in probe.h.  */
45
46 struct symtabs_and_lines
47 parse_probes (const struct event_location *location,
48               struct linespec_result *canonical)
49 {
50   char *arg_end, *arg;
51   char *objfile_namestr = NULL, *provider = NULL, *name, *p;
52   struct cleanup *cleanup;
53   struct symtabs_and_lines result;
54   struct objfile *objfile;
55   struct program_space *pspace;
56   const struct probe_ops *probe_ops;
57   const char *arg_start, *cs;
58
59   result.sals = NULL;
60   result.nelts = 0;
61
62   gdb_assert (event_location_type (location) == PROBE_LOCATION);
63   arg_start = get_probe_location (location);
64
65   cs = arg_start;
66   probe_ops = probe_linespec_to_ops (&cs);
67   if (probe_ops == NULL)
68     error (_("'%s' is not a probe linespec"), arg_start);
69
70   arg = (char *) cs;
71   arg = skip_spaces (arg);
72   if (!*arg)
73     error (_("argument to `%s' missing"), arg_start);
74
75   arg_end = skip_to_space (arg);
76
77   /* We make a copy here so we can write over parts with impunity.  */
78   arg = savestring (arg, arg_end - arg);
79   cleanup = make_cleanup (xfree, arg);
80
81   /* Extract each word from the argument, separated by ":"s.  */
82   p = strchr (arg, ':');
83   if (p == NULL)
84     {
85       /* This is `-p name'.  */
86       name = arg;
87     }
88   else
89     {
90       char *hold = p + 1;
91
92       *p = '\0';
93       p = strchr (hold, ':');
94       if (p == NULL)
95         {
96           /* This is `-p provider:name'.  */
97           provider = arg;
98           name = hold;
99         }
100       else
101         {
102           /* This is `-p objfile:provider:name'.  */
103           *p = '\0';
104           objfile_namestr = arg;
105           provider = hold;
106           name = p + 1;
107         }
108     }
109
110   if (*name == '\0')
111     error (_("no probe name specified"));
112   if (provider && *provider == '\0')
113     error (_("invalid provider name"));
114   if (objfile_namestr && *objfile_namestr == '\0')
115     error (_("invalid objfile name"));
116
117   ALL_PSPACES (pspace)
118     ALL_PSPACE_OBJFILES (pspace, objfile)
119       {
120         VEC (probe_p) *probes;
121         struct probe *probe;
122         int ix;
123
124         if (!objfile->sf || !objfile->sf->sym_probe_fns)
125           continue;
126
127         if (objfile_namestr
128             && FILENAME_CMP (objfile_name (objfile), objfile_namestr) != 0
129             && FILENAME_CMP (lbasename (objfile_name (objfile)),
130                              objfile_namestr) != 0)
131           continue;
132
133         probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
134
135         for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
136           {
137             struct symtab_and_line *sal;
138
139             if (probe_ops != &probe_ops_any && probe->pops != probe_ops)
140               continue;
141
142             if (provider && strcmp (probe->provider, provider) != 0)
143               continue;
144
145             if (strcmp (probe->name, name) != 0)
146               continue;
147
148             ++result.nelts;
149             result.sals = xrealloc (result.sals,
150                                     result.nelts
151                                     * sizeof (struct symtab_and_line));
152             sal = &result.sals[result.nelts - 1];
153
154             init_sal (sal);
155
156             sal->pc = get_probe_address (probe, objfile);
157             sal->explicit_pc = 1;
158             sal->section = find_pc_overlay (sal->pc);
159             sal->pspace = pspace;
160             sal->probe = probe;
161             sal->objfile = objfile;
162           }
163       }
164
165   if (result.nelts == 0)
166     {
167       throw_error (NOT_FOUND_ERROR,
168                    _("No probe matching objfile=`%s', provider=`%s', name=`%s'"),
169                    objfile_namestr ? objfile_namestr : _("<any>"),
170                    provider ? provider : _("<any>"),
171                    name);
172     }
173
174   if (canonical)
175     {
176       char *canon;
177
178       canon = savestring (arg_start, arg_end - arg_start);
179       make_cleanup (xfree, canon);
180       canonical->special_display = 1;
181       canonical->pre_expanded = 1;
182       canonical->location = new_probe_location (canon);
183     }
184
185   do_cleanups (cleanup);
186
187   return result;
188 }
189
190 /* See definition in probe.h.  */
191
192 VEC (probe_p) *
193 find_probes_in_objfile (struct objfile *objfile, const char *provider,
194                         const char *name)
195 {
196   VEC (probe_p) *probes, *result = NULL;
197   int ix;
198   struct probe *probe;
199
200   if (!objfile->sf || !objfile->sf->sym_probe_fns)
201     return NULL;
202
203   probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
204   for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
205     {
206       if (strcmp (probe->provider, provider) != 0)
207         continue;
208
209       if (strcmp (probe->name, name) != 0)
210         continue;
211
212       VEC_safe_push (probe_p, result, probe);
213     }
214
215   return result;
216 }
217
218 /* See definition in probe.h.  */
219
220 struct bound_probe
221 find_probe_by_pc (CORE_ADDR pc)
222 {
223   struct objfile *objfile;
224   struct bound_probe result;
225
226   result.objfile = NULL;
227   result.probe = NULL;
228
229   ALL_OBJFILES (objfile)
230   {
231     VEC (probe_p) *probes;
232     int ix;
233     struct probe *probe;
234
235     if (!objfile->sf || !objfile->sf->sym_probe_fns
236         || objfile->sect_index_text == -1)
237       continue;
238
239     /* If this proves too inefficient, we can replace with a hash.  */
240     probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
241     for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
242       if (get_probe_address (probe, objfile) == pc)
243         {
244           result.objfile = objfile;
245           result.probe = probe;
246           return result;
247         }
248   }
249
250   return result;
251 }
252
253 \f
254
255 /* Make a vector of probes matching OBJNAME, PROVIDER, and PROBE_NAME.
256    If POPS is not NULL, only probes of this certain probe_ops will match.
257    Each argument is a regexp, or NULL, which matches anything.  */
258
259 static VEC (bound_probe_s) *
260 collect_probes (char *objname, char *provider, char *probe_name,
261                 const struct probe_ops *pops)
262 {
263   struct objfile *objfile;
264   VEC (bound_probe_s) *result = NULL;
265   struct cleanup *cleanup, *cleanup_temps;
266   regex_t obj_pat, prov_pat, probe_pat;
267
268   cleanup = make_cleanup (VEC_cleanup (bound_probe_s), &result);
269
270   cleanup_temps = make_cleanup (null_cleanup, NULL);
271   if (provider != NULL)
272     compile_rx_or_error (&prov_pat, provider, _("Invalid provider regexp"));
273   if (probe_name != NULL)
274     compile_rx_or_error (&probe_pat, probe_name, _("Invalid probe regexp"));
275   if (objname != NULL)
276     compile_rx_or_error (&obj_pat, objname, _("Invalid object file regexp"));
277
278   ALL_OBJFILES (objfile)
279     {
280       VEC (probe_p) *probes;
281       struct probe *probe;
282       int ix;
283
284       if (! objfile->sf || ! objfile->sf->sym_probe_fns)
285         continue;
286
287       if (objname)
288         {
289           if (regexec (&obj_pat, objfile_name (objfile), 0, NULL, 0) != 0)
290             continue;
291         }
292
293       probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
294
295       for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
296         {
297           struct bound_probe bound;
298
299           if (pops != NULL && probe->pops != pops)
300             continue;
301
302           if (provider
303               && regexec (&prov_pat, probe->provider, 0, NULL, 0) != 0)
304             continue;
305
306           if (probe_name
307               && regexec (&probe_pat, probe->name, 0, NULL, 0) != 0)
308             continue;
309
310           bound.objfile = objfile;
311           bound.probe = probe;
312           VEC_safe_push (bound_probe_s, result, &bound);
313         }
314     }
315
316   do_cleanups (cleanup_temps);
317   discard_cleanups (cleanup);
318   return result;
319 }
320
321 /* A qsort comparison function for bound_probe_s objects.  */
322
323 static int
324 compare_probes (const void *a, const void *b)
325 {
326   const struct bound_probe *pa = (const struct bound_probe *) a;
327   const struct bound_probe *pb = (const struct bound_probe *) b;
328   int v;
329
330   v = strcmp (pa->probe->provider, pb->probe->provider);
331   if (v)
332     return v;
333
334   v = strcmp (pa->probe->name, pb->probe->name);
335   if (v)
336     return v;
337
338   if (pa->probe->address < pb->probe->address)
339     return -1;
340   if (pa->probe->address > pb->probe->address)
341     return 1;
342
343   return strcmp (objfile_name (pa->objfile), objfile_name (pb->objfile));
344 }
345
346 /* Helper function that generate entries in the ui_out table being
347    crafted by `info_probes_for_ops'.  */
348
349 static void
350 gen_ui_out_table_header_info (VEC (bound_probe_s) *probes,
351                               const struct probe_ops *p)
352 {
353   /* `headings' refers to the names of the columns when printing `info
354      probes'.  */
355   VEC (info_probe_column_s) *headings = NULL;
356   struct cleanup *c;
357   info_probe_column_s *column;
358   size_t headings_size;
359   int ix;
360
361   gdb_assert (p != NULL);
362
363   if (p->gen_info_probes_table_header == NULL
364       && p->gen_info_probes_table_values == NULL)
365     return;
366
367   gdb_assert (p->gen_info_probes_table_header != NULL
368               && p->gen_info_probes_table_values != NULL);
369
370   c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
371   p->gen_info_probes_table_header (&headings);
372
373   headings_size = VEC_length (info_probe_column_s, headings);
374
375   for (ix = 0;
376        VEC_iterate (info_probe_column_s, headings, ix, column);
377        ++ix)
378     {
379       struct bound_probe *probe;
380       int jx;
381       size_t size_max = strlen (column->print_name);
382
383       for (jx = 0; VEC_iterate (bound_probe_s, probes, jx, probe); ++jx)
384         {
385           /* `probe_fields' refers to the values of each new field that this
386              probe will display.  */
387           VEC (const_char_ptr) *probe_fields = NULL;
388           struct cleanup *c2;
389           const char *val;
390           int kx;
391
392           if (probe->probe->pops != p)
393             continue;
394
395           c2 = make_cleanup (VEC_cleanup (const_char_ptr), &probe_fields);
396           p->gen_info_probes_table_values (probe->probe, &probe_fields);
397
398           gdb_assert (VEC_length (const_char_ptr, probe_fields)
399                       == headings_size);
400
401           for (kx = 0; VEC_iterate (const_char_ptr, probe_fields, kx, val);
402                ++kx)
403             {
404               /* It is valid to have a NULL value here, which means that the
405                  backend does not have something to write and this particular
406                  field should be skipped.  */
407               if (val == NULL)
408                 continue;
409
410               size_max = max (strlen (val), size_max);
411             }
412           do_cleanups (c2);
413         }
414
415       ui_out_table_header (current_uiout, size_max, ui_left,
416                            column->field_name, column->print_name);
417     }
418
419   do_cleanups (c);
420 }
421
422 /* Helper function to print not-applicable strings for all the extra
423    columns defined in a probe_ops.  */
424
425 static void
426 print_ui_out_not_applicables (const struct probe_ops *pops)
427 {
428   struct cleanup *c;
429   VEC (info_probe_column_s) *headings = NULL;
430   info_probe_column_s *column;
431   int ix;
432
433   if (pops->gen_info_probes_table_header == NULL)
434     return;
435
436   c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
437   pops->gen_info_probes_table_header (&headings);
438
439   for (ix = 0;
440        VEC_iterate (info_probe_column_s, headings, ix, column);
441        ++ix)
442     ui_out_field_string (current_uiout, column->field_name, _("n/a"));
443
444   do_cleanups (c);
445 }
446
447 /* Helper function to print extra information about a probe and an objfile
448    represented by PROBE.  */
449
450 static void
451 print_ui_out_info (struct probe *probe)
452 {
453   int ix;
454   int j = 0;
455   /* `values' refers to the actual values of each new field in the output
456      of `info probe'.  `headings' refers to the names of each new field.  */
457   VEC (const_char_ptr) *values = NULL;
458   VEC (info_probe_column_s) *headings = NULL;
459   info_probe_column_s *column;
460   struct cleanup *c;
461
462   gdb_assert (probe != NULL);
463   gdb_assert (probe->pops != NULL);
464
465   if (probe->pops->gen_info_probes_table_header == NULL
466       && probe->pops->gen_info_probes_table_values == NULL)
467     return;
468
469   gdb_assert (probe->pops->gen_info_probes_table_header != NULL
470               && probe->pops->gen_info_probes_table_values != NULL);
471
472   c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
473   make_cleanup (VEC_cleanup (const_char_ptr), &values);
474
475   probe->pops->gen_info_probes_table_header (&headings);
476   probe->pops->gen_info_probes_table_values (probe, &values);
477
478   gdb_assert (VEC_length (info_probe_column_s, headings)
479               == VEC_length (const_char_ptr, values));
480
481   for (ix = 0;
482        VEC_iterate (info_probe_column_s, headings, ix, column);
483        ++ix)
484     {
485       const char *val = VEC_index (const_char_ptr, values, j++);
486
487       if (val == NULL)
488         ui_out_field_skip (current_uiout, column->field_name);
489       else
490         ui_out_field_string (current_uiout, column->field_name, val);
491     }
492
493   do_cleanups (c);
494 }
495
496 /* Helper function that returns the number of extra fields which POPS will
497    need.  */
498
499 static int
500 get_number_extra_fields (const struct probe_ops *pops)
501 {
502   VEC (info_probe_column_s) *headings = NULL;
503   struct cleanup *c;
504   int n;
505
506   if (pops->gen_info_probes_table_header == NULL)
507     return 0;
508
509   c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
510   pops->gen_info_probes_table_header (&headings);
511
512   n = VEC_length (info_probe_column_s, headings);
513
514   do_cleanups (c);
515
516   return n;
517 }
518
519 /* Helper function that returns 1 if there is a probe in PROBES
520    featuring the given POPS.  It returns 0 otherwise.  */
521
522 static int
523 exists_probe_with_pops (VEC (bound_probe_s) *probes,
524                         const struct probe_ops *pops)
525 {
526   struct bound_probe *probe;
527   int ix;
528
529   for (ix = 0; VEC_iterate (bound_probe_s, probes, ix, probe); ++ix)
530     if (probe->probe->pops == pops)
531       return 1;
532
533   return 0;
534 }
535
536 /* Helper function that parses a probe linespec of the form [PROVIDER
537    [PROBE [OBJNAME]]] from the provided string STR.  */
538
539 static void
540 parse_probe_linespec (const char *str, char **provider,
541                       char **probe_name, char **objname)
542 {
543   *probe_name = *objname = NULL;
544
545   *provider = extract_arg_const (&str);
546   if (*provider != NULL)
547     {
548       *probe_name = extract_arg_const (&str);
549       if (*probe_name != NULL)
550         *objname = extract_arg_const (&str);
551     }
552 }
553
554 /* See comment in probe.h.  */
555
556 void
557 info_probes_for_ops (const char *arg, int from_tty,
558                      const struct probe_ops *pops)
559 {
560   char *provider, *probe_name = NULL, *objname = NULL;
561   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
562   VEC (bound_probe_s) *probes;
563   int i, any_found;
564   int ui_out_extra_fields = 0;
565   size_t size_addr;
566   size_t size_name = strlen ("Name");
567   size_t size_objname = strlen ("Object");
568   size_t size_provider = strlen ("Provider");
569   size_t size_type = strlen ("Type");
570   struct bound_probe *probe;
571   struct gdbarch *gdbarch = get_current_arch ();
572
573   parse_probe_linespec (arg, &provider, &probe_name, &objname);
574   make_cleanup (xfree, provider);
575   make_cleanup (xfree, probe_name);
576   make_cleanup (xfree, objname);
577
578   probes = collect_probes (objname, provider, probe_name, pops);
579   make_cleanup (VEC_cleanup (probe_p), &probes);
580
581   if (pops == NULL)
582     {
583       const struct probe_ops *po;
584       int ix;
585
586       /* If the probe_ops is NULL, it means the user has requested a "simple"
587          `info probes', i.e., she wants to print all information about all
588          probes.  For that, we have to identify how many extra fields we will
589          need to add in the ui_out table.
590
591          To do that, we iterate over all probe_ops, querying each one about
592          its extra fields, and incrementing `ui_out_extra_fields' to reflect
593          that number.  But note that we ignore the probe_ops for which no probes
594          are defined with the given search criteria.  */
595
596       for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
597         if (exists_probe_with_pops (probes, po))
598           ui_out_extra_fields += get_number_extra_fields (po);
599     }
600   else
601     ui_out_extra_fields = get_number_extra_fields (pops);
602
603   make_cleanup_ui_out_table_begin_end (current_uiout,
604                                        5 + ui_out_extra_fields,
605                                        VEC_length (bound_probe_s, probes),
606                                        "StaticProbes");
607
608   if (!VEC_empty (bound_probe_s, probes))
609     qsort (VEC_address (bound_probe_s, probes),
610            VEC_length (bound_probe_s, probes),
611            sizeof (bound_probe_s), compare_probes);
612
613   /* What's the size of an address in our architecture?  */
614   size_addr = gdbarch_addr_bit (gdbarch) == 64 ? 18 : 10;
615
616   /* Determining the maximum size of each field (`type', `provider',
617      `name' and `objname').  */
618   for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
619     {
620       const char *probe_type = probe->probe->pops->type_name (probe->probe);
621
622       size_type = max (strlen (probe_type), size_type);
623       size_name = max (strlen (probe->probe->name), size_name);
624       size_provider = max (strlen (probe->probe->provider), size_provider);
625       size_objname = max (strlen (objfile_name (probe->objfile)), size_objname);
626     }
627
628   ui_out_table_header (current_uiout, size_type, ui_left, "type", _("Type"));
629   ui_out_table_header (current_uiout, size_provider, ui_left, "provider",
630                        _("Provider"));
631   ui_out_table_header (current_uiout, size_name, ui_left, "name", _("Name"));
632   ui_out_table_header (current_uiout, size_addr, ui_left, "addr", _("Where"));
633
634   if (pops == NULL)
635     {
636       const struct probe_ops *po;
637       int ix;
638
639       /* We have to generate the table header for each new probe type
640          that we will print.  Note that this excludes probe types not
641          having any defined probe with the search criteria.  */
642       for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
643         if (exists_probe_with_pops (probes, po))
644           gen_ui_out_table_header_info (probes, po);
645     }
646   else
647     gen_ui_out_table_header_info (probes, pops);
648
649   ui_out_table_header (current_uiout, size_objname, ui_left, "object",
650                        _("Object"));
651   ui_out_table_body (current_uiout);
652
653   for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
654     {
655       struct cleanup *inner;
656       const char *probe_type = probe->probe->pops->type_name (probe->probe);
657
658       inner = make_cleanup_ui_out_tuple_begin_end (current_uiout, "probe");
659
660       ui_out_field_string (current_uiout, "type",probe_type);
661       ui_out_field_string (current_uiout, "provider", probe->probe->provider);
662       ui_out_field_string (current_uiout, "name", probe->probe->name);
663       ui_out_field_core_addr (current_uiout, "addr",
664                               probe->probe->arch,
665                               get_probe_address (probe->probe, probe->objfile));
666
667       if (pops == NULL)
668         {
669           const struct probe_ops *po;
670           int ix;
671
672           for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po);
673                ++ix)
674             if (probe->probe->pops == po)
675               print_ui_out_info (probe->probe);
676             else if (exists_probe_with_pops (probes, po))
677               print_ui_out_not_applicables (po);
678         }
679       else
680         print_ui_out_info (probe->probe);
681
682       ui_out_field_string (current_uiout, "object",
683                            objfile_name (probe->objfile));
684       ui_out_text (current_uiout, "\n");
685
686       do_cleanups (inner);
687     }
688
689   any_found = !VEC_empty (bound_probe_s, probes);
690   do_cleanups (cleanup);
691
692   if (!any_found)
693     ui_out_message (current_uiout, 0, _("No probes matched.\n"));
694 }
695
696 /* Implementation of the `info probes' command.  */
697
698 static void
699 info_probes_command (char *arg, int from_tty)
700 {
701   info_probes_for_ops (arg, from_tty, NULL);
702 }
703
704 /* Implementation of the `enable probes' command.  */
705
706 static void
707 enable_probes_command (char *arg, int from_tty)
708 {
709   char *provider, *probe_name = NULL, *objname = NULL;
710   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
711   VEC (bound_probe_s) *probes;
712   struct bound_probe *probe;
713   int i;
714
715   parse_probe_linespec ((const char *) arg, &provider, &probe_name, &objname);
716   make_cleanup (xfree, provider);
717   make_cleanup (xfree, probe_name);
718   make_cleanup (xfree, objname);
719
720   probes = collect_probes (objname, provider, probe_name, NULL);
721   if (VEC_empty (bound_probe_s, probes))
722     {
723       ui_out_message (current_uiout, 0, _("No probes matched.\n"));
724       do_cleanups (cleanup);
725       return;
726     }
727
728   /* Enable the selected probes, provided their backends support the
729      notion of enabling a probe.  */
730   for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
731     {
732       const struct probe_ops *pops = probe->probe->pops;
733
734       if (pops->enable_probe != NULL)
735         {
736           pops->enable_probe (probe->probe);
737           ui_out_message (current_uiout, 0,
738                           _("Probe %s:%s enabled.\n"),
739                           probe->probe->provider, probe->probe->name);
740         }
741       else
742         ui_out_message (current_uiout, 0,
743                         _("Probe %s:%s cannot be enabled.\n"),
744                         probe->probe->provider, probe->probe->name);
745     }
746
747   do_cleanups (cleanup);
748 }
749
750 /* Implementation of the `disable probes' command.  */
751
752 static void
753 disable_probes_command (char *arg, int from_tty)
754 {
755   char *provider, *probe_name = NULL, *objname = NULL;
756   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
757   VEC (bound_probe_s) *probes;
758   struct bound_probe *probe;
759   int i;
760
761   parse_probe_linespec ((const char *) arg, &provider, &probe_name, &objname);
762   make_cleanup (xfree, provider);
763   make_cleanup (xfree, probe_name);
764   make_cleanup (xfree, objname);
765
766   probes = collect_probes (objname, provider, probe_name, NULL /* pops */);
767   if (VEC_empty (bound_probe_s, probes))
768     {
769       ui_out_message (current_uiout, 0, _("No probes matched.\n"));
770       do_cleanups (cleanup);
771       return;
772     }
773
774   /* Disable the selected probes, provided their backends support the
775      notion of enabling a probe.  */
776   for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
777     {
778       const struct probe_ops *pops = probe->probe->pops;
779
780       if (pops->disable_probe != NULL)
781         {
782           pops->disable_probe (probe->probe);
783           ui_out_message (current_uiout, 0,
784                           _("Probe %s:%s disabled.\n"),
785                           probe->probe->provider, probe->probe->name);
786         }
787       else
788         ui_out_message (current_uiout, 0,
789                         _("Probe %s:%s cannot be disabled.\n"),
790                         probe->probe->provider, probe->probe->name);
791     }
792
793   do_cleanups (cleanup);
794 }
795
796 /* See comments in probe.h.  */
797
798 CORE_ADDR
799 get_probe_address (struct probe *probe, struct objfile *objfile)
800 {
801   return probe->pops->get_probe_address (probe, objfile);
802 }
803
804 /* See comments in probe.h.  */
805
806 unsigned
807 get_probe_argument_count (struct probe *probe, struct frame_info *frame)
808 {
809   return probe->pops->get_probe_argument_count (probe, frame);
810 }
811
812 /* See comments in probe.h.  */
813
814 int
815 can_evaluate_probe_arguments (struct probe *probe)
816 {
817   return probe->pops->can_evaluate_probe_arguments (probe);
818 }
819
820 /* See comments in probe.h.  */
821
822 struct value *
823 evaluate_probe_argument (struct probe *probe, unsigned n,
824                          struct frame_info *frame)
825 {
826   return probe->pops->evaluate_probe_argument (probe, n, frame);
827 }
828
829 /* See comments in probe.h.  */
830
831 struct value *
832 probe_safe_evaluate_at_pc (struct frame_info *frame, unsigned n)
833 {
834   struct bound_probe probe;
835   unsigned n_args;
836
837   probe = find_probe_by_pc (get_frame_pc (frame));
838   if (!probe.probe)
839     return NULL;
840
841   n_args = get_probe_argument_count (probe.probe, frame);
842   if (n >= n_args)
843     return NULL;
844
845   return evaluate_probe_argument (probe.probe, n, frame);
846 }
847
848 /* See comment in probe.h.  */
849
850 const struct probe_ops *
851 probe_linespec_to_ops (const char **linespecp)
852 {
853   int ix;
854   const struct probe_ops *probe_ops;
855
856   for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, probe_ops); ix++)
857     if (probe_ops->is_linespec (linespecp))
858       return probe_ops;
859
860   return NULL;
861 }
862
863 /* See comment in probe.h.  */
864
865 int
866 probe_is_linespec_by_keyword (const char **linespecp, const char *const *keywords)
867 {
868   const char *s = *linespecp;
869   const char *const *csp;
870
871   for (csp = keywords; *csp; csp++)
872     {
873       const char *keyword = *csp;
874       size_t len = strlen (keyword);
875
876       if (strncmp (s, keyword, len) == 0 && isspace (s[len]))
877         {
878           *linespecp += len + 1;
879           return 1;
880         }
881     }
882
883   return 0;
884 }
885
886 /* Implementation of `is_linespec' method for `struct probe_ops'.  */
887
888 static int
889 probe_any_is_linespec (const char **linespecp)
890 {
891   static const char *const keywords[] = { "-p", "-probe", NULL };
892
893   return probe_is_linespec_by_keyword (linespecp, keywords);
894 }
895
896 /* Dummy method used for `probe_ops_any'.  */
897
898 static void
899 probe_any_get_probes (VEC (probe_p) **probesp, struct objfile *objfile)
900 {
901   /* No probes can be provided by this dummy backend.  */
902 }
903
904 /* Operations associated with a generic probe.  */
905
906 const struct probe_ops probe_ops_any =
907 {
908   probe_any_is_linespec,
909   probe_any_get_probes,
910 };
911
912 /* See comments in probe.h.  */
913
914 struct cmd_list_element **
915 info_probes_cmdlist_get (void)
916 {
917   static struct cmd_list_element *info_probes_cmdlist;
918
919   if (info_probes_cmdlist == NULL)
920     add_prefix_cmd ("probes", class_info, info_probes_command,
921                     _("\
922 Show available static probes.\n\
923 Usage: info probes [all|TYPE [ARGS]]\n\
924 TYPE specifies the type of the probe, and can be one of the following:\n\
925   - stap\n\
926 If you specify TYPE, there may be additional arguments needed by the\n\
927 subcommand.\n\
928 If you do not specify any argument, or specify `all', then the command\n\
929 will show information about all types of probes."),
930                     &info_probes_cmdlist, "info probes ",
931                     0/*allow-unknown*/, &infolist);
932
933   return &info_probes_cmdlist;
934 }
935
936 \f
937
938 /* This is called to compute the value of one of the $_probe_arg*
939    convenience variables.  */
940
941 static struct value *
942 compute_probe_arg (struct gdbarch *arch, struct internalvar *ivar,
943                    void *data)
944 {
945   struct frame_info *frame = get_selected_frame (_("No frame selected"));
946   CORE_ADDR pc = get_frame_pc (frame);
947   int sel = (int) (uintptr_t) data;
948   struct bound_probe pc_probe;
949   const struct sym_probe_fns *pc_probe_fns;
950   unsigned n_args;
951
952   /* SEL == -1 means "_probe_argc".  */
953   gdb_assert (sel >= -1);
954
955   pc_probe = find_probe_by_pc (pc);
956   if (pc_probe.probe == NULL)
957     error (_("No probe at PC %s"), core_addr_to_string (pc));
958
959   n_args = get_probe_argument_count (pc_probe.probe, frame);
960   if (sel == -1)
961     return value_from_longest (builtin_type (arch)->builtin_int, n_args);
962
963   if (sel >= n_args)
964     error (_("Invalid probe argument %d -- probe has %u arguments available"),
965            sel, n_args);
966
967   return evaluate_probe_argument (pc_probe.probe, sel, frame);
968 }
969
970 /* This is called to compile one of the $_probe_arg* convenience
971    variables into an agent expression.  */
972
973 static void
974 compile_probe_arg (struct internalvar *ivar, struct agent_expr *expr,
975                    struct axs_value *value, void *data)
976 {
977   CORE_ADDR pc = expr->scope;
978   int sel = (int) (uintptr_t) data;
979   struct bound_probe pc_probe;
980   const struct sym_probe_fns *pc_probe_fns;
981   int n_args;
982   struct frame_info *frame = get_selected_frame (NULL);
983
984   /* SEL == -1 means "_probe_argc".  */
985   gdb_assert (sel >= -1);
986
987   pc_probe = find_probe_by_pc (pc);
988   if (pc_probe.probe == NULL)
989     error (_("No probe at PC %s"), core_addr_to_string (pc));
990
991   n_args = get_probe_argument_count (pc_probe.probe, frame);
992
993   if (sel == -1)
994     {
995       value->kind = axs_rvalue;
996       value->type = builtin_type (expr->gdbarch)->builtin_int;
997       ax_const_l (expr, n_args);
998       return;
999     }
1000
1001   gdb_assert (sel >= 0);
1002   if (sel >= n_args)
1003     error (_("Invalid probe argument %d -- probe has %d arguments available"),
1004            sel, n_args);
1005
1006   pc_probe.probe->pops->compile_to_ax (pc_probe.probe, expr, value, sel);
1007 }
1008
1009 static const struct internalvar_funcs probe_funcs =
1010 {
1011   compute_probe_arg,
1012   compile_probe_arg,
1013   NULL
1014 };
1015
1016
1017 VEC (probe_ops_cp) *all_probe_ops;
1018
1019 void _initialize_probe (void);
1020
1021 void
1022 _initialize_probe (void)
1023 {
1024   VEC_safe_push (probe_ops_cp, all_probe_ops, &probe_ops_any);
1025
1026   create_internalvar_type_lazy ("_probe_argc", &probe_funcs,
1027                                 (void *) (uintptr_t) -1);
1028   create_internalvar_type_lazy ("_probe_arg0", &probe_funcs,
1029                                 (void *) (uintptr_t) 0);
1030   create_internalvar_type_lazy ("_probe_arg1", &probe_funcs,
1031                                 (void *) (uintptr_t) 1);
1032   create_internalvar_type_lazy ("_probe_arg2", &probe_funcs,
1033                                 (void *) (uintptr_t) 2);
1034   create_internalvar_type_lazy ("_probe_arg3", &probe_funcs,
1035                                 (void *) (uintptr_t) 3);
1036   create_internalvar_type_lazy ("_probe_arg4", &probe_funcs,
1037                                 (void *) (uintptr_t) 4);
1038   create_internalvar_type_lazy ("_probe_arg5", &probe_funcs,
1039                                 (void *) (uintptr_t) 5);
1040   create_internalvar_type_lazy ("_probe_arg6", &probe_funcs,
1041                                 (void *) (uintptr_t) 6);
1042   create_internalvar_type_lazy ("_probe_arg7", &probe_funcs,
1043                                 (void *) (uintptr_t) 7);
1044   create_internalvar_type_lazy ("_probe_arg8", &probe_funcs,
1045                                 (void *) (uintptr_t) 8);
1046   create_internalvar_type_lazy ("_probe_arg9", &probe_funcs,
1047                                 (void *) (uintptr_t) 9);
1048   create_internalvar_type_lazy ("_probe_arg10", &probe_funcs,
1049                                 (void *) (uintptr_t) 10);
1050   create_internalvar_type_lazy ("_probe_arg11", &probe_funcs,
1051                                 (void *) (uintptr_t) 11);
1052
1053   add_cmd ("all", class_info, info_probes_command,
1054            _("\
1055 Show information about all type of probes."),
1056            info_probes_cmdlist_get ());
1057
1058   add_cmd ("probes", class_breakpoint, enable_probes_command, _("\
1059 Enable probes.\n\
1060 Usage: enable probes [PROVIDER [NAME [OBJECT]]]\n\
1061 Each argument is a regular expression, used to select probes.\n\
1062 PROVIDER matches probe provider names.\n\
1063 NAME matches the probe names.\n\
1064 OBJECT matches the executable or shared library name.\n\
1065 If you do not specify any argument then the command will enable\n\
1066 all defined probes."),
1067            &enablelist);
1068
1069   add_cmd ("probes", class_breakpoint, disable_probes_command, _("\
1070 Disable probes.\n\
1071 Usage: disable probes [PROVIDER [NAME [OBJECT]]]\n\
1072 Each argument is a regular expression, used to select probes.\n\
1073 PROVIDER matches probe provider names.\n\
1074 NAME matches the probe names.\n\
1075 OBJECT matches the executable or shared library name.\n\
1076 If you do not specify any argument then the command will disable\n\
1077 all defined probes."),
1078            &disablelist);
1079
1080 }