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