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