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