Introduce compiled_regex, eliminate make_regfree_cleanup
[external/binutils.git] / gdb / probe.c
1 /* Generic static probe support for GDB.
2
3    Copyright (C) 2012-2017 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 #include <algorithm>
39 #include "common/gdb_optional.h"
40
41 typedef struct bound_probe bound_probe_s;
42 DEF_VEC_O (bound_probe_s);
43
44 \f
45
46 /* A helper for parse_probes that decodes a probe specification in
47    SEARCH_PSPACE.  It appends matching SALs to RESULT.  */
48
49 static void
50 parse_probes_in_pspace (const struct probe_ops *probe_ops,
51                         struct program_space *search_pspace,
52                         const char *objfile_namestr,
53                         const char *provider,
54                         const char *name,
55                         struct symtabs_and_lines *result)
56 {
57   struct objfile *objfile;
58
59   ALL_PSPACE_OBJFILES (search_pspace, objfile)
60     {
61       VEC (probe_p) *probes;
62       struct probe *probe;
63       int ix;
64
65       if (!objfile->sf || !objfile->sf->sym_probe_fns)
66         continue;
67
68       if (objfile_namestr
69           && FILENAME_CMP (objfile_name (objfile), objfile_namestr) != 0
70           && FILENAME_CMP (lbasename (objfile_name (objfile)),
71                            objfile_namestr) != 0)
72         continue;
73
74       probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
75
76       for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
77         {
78           struct symtab_and_line *sal;
79
80           if (probe_ops != &probe_ops_any && probe->pops != probe_ops)
81             continue;
82
83           if (provider && strcmp (probe->provider, provider) != 0)
84             continue;
85
86           if (strcmp (probe->name, name) != 0)
87             continue;
88
89           ++result->nelts;
90           result->sals = XRESIZEVEC (struct symtab_and_line, result->sals,
91                                      result->nelts);
92           sal = &result->sals[result->nelts - 1];
93
94           init_sal (sal);
95
96           sal->pc = get_probe_address (probe, objfile);
97           sal->explicit_pc = 1;
98           sal->section = find_pc_overlay (sal->pc);
99           sal->pspace = search_pspace;
100           sal->probe = probe;
101           sal->objfile = objfile;
102         }
103     }
104 }
105
106 /* See definition in probe.h.  */
107
108 struct symtabs_and_lines
109 parse_probes (const struct event_location *location,
110               struct program_space *search_pspace,
111               struct linespec_result *canonical)
112 {
113   char *arg_end, *arg;
114   char *objfile_namestr = NULL, *provider = NULL, *name, *p;
115   struct cleanup *cleanup;
116   struct symtabs_and_lines result;
117   const struct probe_ops *probe_ops;
118   const char *arg_start, *cs;
119
120   result.sals = NULL;
121   result.nelts = 0;
122
123   gdb_assert (event_location_type (location) == PROBE_LOCATION);
124   arg_start = get_probe_location (location);
125
126   cs = arg_start;
127   probe_ops = probe_linespec_to_ops (&cs);
128   if (probe_ops == NULL)
129     error (_("'%s' is not a probe linespec"), arg_start);
130
131   arg = (char *) cs;
132   arg = skip_spaces (arg);
133   if (!*arg)
134     error (_("argument to `%s' missing"), arg_start);
135
136   arg_end = skip_to_space (arg);
137
138   /* We make a copy here so we can write over parts with impunity.  */
139   arg = savestring (arg, arg_end - arg);
140   cleanup = make_cleanup (xfree, arg);
141
142   /* Extract each word from the argument, separated by ":"s.  */
143   p = strchr (arg, ':');
144   if (p == NULL)
145     {
146       /* This is `-p name'.  */
147       name = arg;
148     }
149   else
150     {
151       char *hold = p + 1;
152
153       *p = '\0';
154       p = strchr (hold, ':');
155       if (p == NULL)
156         {
157           /* This is `-p provider:name'.  */
158           provider = arg;
159           name = hold;
160         }
161       else
162         {
163           /* This is `-p objfile:provider:name'.  */
164           *p = '\0';
165           objfile_namestr = arg;
166           provider = hold;
167           name = p + 1;
168         }
169     }
170
171   if (*name == '\0')
172     error (_("no probe name specified"));
173   if (provider && *provider == '\0')
174     error (_("invalid provider name"));
175   if (objfile_namestr && *objfile_namestr == '\0')
176     error (_("invalid objfile name"));
177
178   if (search_pspace != NULL)
179     {
180       parse_probes_in_pspace (probe_ops, search_pspace, objfile_namestr,
181                               provider, name, &result);
182     }
183   else
184     {
185       struct program_space *pspace;
186
187       ALL_PSPACES (pspace)
188         parse_probes_in_pspace (probe_ops, pspace, objfile_namestr,
189                                 provider, name, &result);
190     }
191
192   if (result.nelts == 0)
193     {
194       throw_error (NOT_FOUND_ERROR,
195                    _("No probe matching objfile=`%s', provider=`%s', name=`%s'"),
196                    objfile_namestr ? objfile_namestr : _("<any>"),
197                    provider ? provider : _("<any>"),
198                    name);
199     }
200
201   if (canonical)
202     {
203       char *canon;
204
205       canon = savestring (arg_start, arg_end - arg_start);
206       make_cleanup (xfree, canon);
207       canonical->special_display = 1;
208       canonical->pre_expanded = 1;
209       canonical->location = new_probe_location (canon);
210     }
211
212   do_cleanups (cleanup);
213
214   return result;
215 }
216
217 /* See definition in probe.h.  */
218
219 VEC (probe_p) *
220 find_probes_in_objfile (struct objfile *objfile, const char *provider,
221                         const char *name)
222 {
223   VEC (probe_p) *probes, *result = NULL;
224   int ix;
225   struct probe *probe;
226
227   if (!objfile->sf || !objfile->sf->sym_probe_fns)
228     return NULL;
229
230   probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
231   for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
232     {
233       if (strcmp (probe->provider, provider) != 0)
234         continue;
235
236       if (strcmp (probe->name, name) != 0)
237         continue;
238
239       VEC_safe_push (probe_p, result, probe);
240     }
241
242   return result;
243 }
244
245 /* See definition in probe.h.  */
246
247 struct bound_probe
248 find_probe_by_pc (CORE_ADDR pc)
249 {
250   struct objfile *objfile;
251   struct bound_probe result;
252
253   result.objfile = NULL;
254   result.probe = NULL;
255
256   ALL_OBJFILES (objfile)
257   {
258     VEC (probe_p) *probes;
259     int ix;
260     struct probe *probe;
261
262     if (!objfile->sf || !objfile->sf->sym_probe_fns
263         || objfile->sect_index_text == -1)
264       continue;
265
266     /* If this proves too inefficient, we can replace with a hash.  */
267     probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
268     for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
269       if (get_probe_address (probe, objfile) == pc)
270         {
271           result.objfile = objfile;
272           result.probe = probe;
273           return result;
274         }
275   }
276
277   return result;
278 }
279
280 \f
281
282 /* Make a vector of probes matching OBJNAME, PROVIDER, and PROBE_NAME.
283    If POPS is not NULL, only probes of this certain probe_ops will match.
284    Each argument is a regexp, or NULL, which matches anything.  */
285
286 static VEC (bound_probe_s) *
287 collect_probes (char *objname, char *provider, char *probe_name,
288                 const struct probe_ops *pops)
289 {
290   struct objfile *objfile;
291   VEC (bound_probe_s) *result = NULL;
292   struct cleanup *cleanup;
293   gdb::optional<compiled_regex> obj_pat, prov_pat, probe_pat;
294
295   cleanup = make_cleanup (VEC_cleanup (bound_probe_s), &result);
296
297   if (provider != NULL)
298     prov_pat.emplace (provider, REG_NOSUB, _("Invalid provider regexp"));
299   if (probe_name != NULL)
300     probe_pat.emplace (probe_name, REG_NOSUB, _("Invalid probe regexp"));
301   if (objname != NULL)
302     obj_pat.emplace (objname, REG_NOSUB, _("Invalid object file regexp"));
303
304   ALL_OBJFILES (objfile)
305     {
306       VEC (probe_p) *probes;
307       struct probe *probe;
308       int ix;
309
310       if (! objfile->sf || ! objfile->sf->sym_probe_fns)
311         continue;
312
313       if (objname)
314         {
315           if (obj_pat->exec (objfile_name (objfile), 0, NULL, 0) != 0)
316             continue;
317         }
318
319       probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
320
321       for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
322         {
323           struct bound_probe bound;
324
325           if (pops != NULL && probe->pops != pops)
326             continue;
327
328           if (provider
329               && prov_pat->exec (probe->provider, 0, NULL, 0) != 0)
330             continue;
331
332           if (probe_name
333               && probe_pat->exec (probe->name, 0, NULL, 0) != 0)
334             continue;
335
336           bound.objfile = objfile;
337           bound.probe = probe;
338           VEC_safe_push (bound_probe_s, result, &bound);
339         }
340     }
341
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 = std::max (strlen (val), size_max);
436             }
437           do_cleanups (c2);
438         }
439
440       current_uiout->table_header (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     current_uiout->field_string (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         current_uiout->field_skip (column->field_name);
514       else
515         current_uiout->field_string (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 = std::max (strlen (probe_type), size_type);
648       size_name = std::max (strlen (probe->probe->name), size_name);
649       size_provider = std::max (strlen (probe->probe->provider), size_provider);
650       size_objname = std::max (strlen (objfile_name (probe->objfile)),
651                                size_objname);
652     }
653
654   current_uiout->table_header (size_type, ui_left, "type", _("Type"));
655   current_uiout->table_header (size_provider, ui_left, "provider",
656                                _("Provider"));
657   current_uiout->table_header (size_name, ui_left, "name", _("Name"));
658   current_uiout->table_header (size_addr, ui_left, "addr", _("Where"));
659
660   if (pops == NULL)
661     {
662       const struct probe_ops *po;
663       int ix;
664
665       /* We have to generate the table header for each new probe type
666          that we will print.  Note that this excludes probe types not
667          having any defined probe with the search criteria.  */
668       for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
669         if (exists_probe_with_pops (probes, po))
670           gen_ui_out_table_header_info (probes, po);
671     }
672   else
673     gen_ui_out_table_header_info (probes, pops);
674
675   current_uiout->table_header (size_objname, ui_left, "object", _("Object"));
676   current_uiout->table_body ();
677
678   for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
679     {
680       const char *probe_type = probe->probe->pops->type_name (probe->probe);
681
682       ui_out_emit_tuple tuple_emitter (current_uiout, "probe");
683
684       current_uiout->field_string ("type",probe_type);
685       current_uiout->field_string ("provider", probe->probe->provider);
686       current_uiout->field_string ("name", probe->probe->name);
687       current_uiout->field_core_addr (
688         "addr", probe->probe->arch,
689         get_probe_address (probe->probe, probe->objfile));
690
691       if (pops == NULL)
692         {
693           const struct probe_ops *po;
694           int ix;
695
696           for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po);
697                ++ix)
698             if (probe->probe->pops == po)
699               print_ui_out_info (probe->probe);
700             else if (exists_probe_with_pops (probes, po))
701               print_ui_out_not_applicables (po);
702         }
703       else
704         print_ui_out_info (probe->probe);
705
706       current_uiout->field_string ("object",
707                            objfile_name (probe->objfile));
708       current_uiout->text ("\n");
709     }
710
711   any_found = !VEC_empty (bound_probe_s, probes);
712   do_cleanups (cleanup);
713
714   if (!any_found)
715     current_uiout->message (_("No probes matched.\n"));
716 }
717
718 /* Implementation of the `info probes' command.  */
719
720 static void
721 info_probes_command (char *arg, int from_tty)
722 {
723   info_probes_for_ops (arg, from_tty, NULL);
724 }
725
726 /* Implementation of the `enable probes' command.  */
727
728 static void
729 enable_probes_command (char *arg, int from_tty)
730 {
731   char *provider, *probe_name = NULL, *objname = NULL;
732   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
733   VEC (bound_probe_s) *probes;
734   struct bound_probe *probe;
735   int i;
736
737   parse_probe_linespec ((const char *) arg, &provider, &probe_name, &objname);
738   make_cleanup (xfree, provider);
739   make_cleanup (xfree, probe_name);
740   make_cleanup (xfree, objname);
741
742   probes = collect_probes (objname, provider, probe_name, NULL);
743   if (VEC_empty (bound_probe_s, probes))
744     {
745       current_uiout->message (_("No probes matched.\n"));
746       do_cleanups (cleanup);
747       return;
748     }
749
750   /* Enable the selected probes, provided their backends support the
751      notion of enabling a probe.  */
752   for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
753     {
754       const struct probe_ops *pops = probe->probe->pops;
755
756       if (pops->enable_probe != NULL)
757         {
758           pops->enable_probe (probe->probe);
759           current_uiout->message (_("Probe %s:%s enabled.\n"),
760                                   probe->probe->provider, probe->probe->name);
761         }
762       else
763         current_uiout->message (_("Probe %s:%s cannot be enabled.\n"),
764                                 probe->probe->provider, probe->probe->name);
765     }
766
767   do_cleanups (cleanup);
768 }
769
770 /* Implementation of the `disable probes' command.  */
771
772 static void
773 disable_probes_command (char *arg, int from_tty)
774 {
775   char *provider, *probe_name = NULL, *objname = NULL;
776   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
777   VEC (bound_probe_s) *probes;
778   struct bound_probe *probe;
779   int i;
780
781   parse_probe_linespec ((const char *) arg, &provider, &probe_name, &objname);
782   make_cleanup (xfree, provider);
783   make_cleanup (xfree, probe_name);
784   make_cleanup (xfree, objname);
785
786   probes = collect_probes (objname, provider, probe_name, NULL /* pops */);
787   if (VEC_empty (bound_probe_s, probes))
788     {
789       current_uiout->message (_("No probes matched.\n"));
790       do_cleanups (cleanup);
791       return;
792     }
793
794   /* Disable the selected probes, provided their backends support the
795      notion of enabling a probe.  */
796   for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
797     {
798       const struct probe_ops *pops = probe->probe->pops;
799
800       if (pops->disable_probe != NULL)
801         {
802           pops->disable_probe (probe->probe);
803           current_uiout->message (_("Probe %s:%s disabled.\n"),
804                                   probe->probe->provider, probe->probe->name);
805         }
806       else
807         current_uiout->message (_("Probe %s:%s cannot be disabled.\n"),
808                                 probe->probe->provider, probe->probe->name);
809     }
810
811   do_cleanups (cleanup);
812 }
813
814 /* See comments in probe.h.  */
815
816 CORE_ADDR
817 get_probe_address (struct probe *probe, struct objfile *objfile)
818 {
819   return probe->pops->get_probe_address (probe, objfile);
820 }
821
822 /* See comments in probe.h.  */
823
824 unsigned
825 get_probe_argument_count (struct probe *probe, struct frame_info *frame)
826 {
827   return probe->pops->get_probe_argument_count (probe, frame);
828 }
829
830 /* See comments in probe.h.  */
831
832 int
833 can_evaluate_probe_arguments (struct probe *probe)
834 {
835   return probe->pops->can_evaluate_probe_arguments (probe);
836 }
837
838 /* See comments in probe.h.  */
839
840 struct value *
841 evaluate_probe_argument (struct probe *probe, unsigned n,
842                          struct frame_info *frame)
843 {
844   return probe->pops->evaluate_probe_argument (probe, n, frame);
845 }
846
847 /* See comments in probe.h.  */
848
849 struct value *
850 probe_safe_evaluate_at_pc (struct frame_info *frame, unsigned n)
851 {
852   struct bound_probe probe;
853   unsigned n_args;
854
855   probe = find_probe_by_pc (get_frame_pc (frame));
856   if (!probe.probe)
857     return NULL;
858
859   n_args = get_probe_argument_count (probe.probe, frame);
860   if (n >= n_args)
861     return NULL;
862
863   return evaluate_probe_argument (probe.probe, n, frame);
864 }
865
866 /* See comment in probe.h.  */
867
868 const struct probe_ops *
869 probe_linespec_to_ops (const char **linespecp)
870 {
871   int ix;
872   const struct probe_ops *probe_ops;
873
874   for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, probe_ops); ix++)
875     if (probe_ops->is_linespec (linespecp))
876       return probe_ops;
877
878   return NULL;
879 }
880
881 /* See comment in probe.h.  */
882
883 int
884 probe_is_linespec_by_keyword (const char **linespecp, const char *const *keywords)
885 {
886   const char *s = *linespecp;
887   const char *const *csp;
888
889   for (csp = keywords; *csp; csp++)
890     {
891       const char *keyword = *csp;
892       size_t len = strlen (keyword);
893
894       if (strncmp (s, keyword, len) == 0 && isspace (s[len]))
895         {
896           *linespecp += len + 1;
897           return 1;
898         }
899     }
900
901   return 0;
902 }
903
904 /* Implementation of `is_linespec' method for `struct probe_ops'.  */
905
906 static int
907 probe_any_is_linespec (const char **linespecp)
908 {
909   static const char *const keywords[] = { "-p", "-probe", NULL };
910
911   return probe_is_linespec_by_keyword (linespecp, keywords);
912 }
913
914 /* Dummy method used for `probe_ops_any'.  */
915
916 static void
917 probe_any_get_probes (VEC (probe_p) **probesp, struct objfile *objfile)
918 {
919   /* No probes can be provided by this dummy backend.  */
920 }
921
922 /* Operations associated with a generic probe.  */
923
924 const struct probe_ops probe_ops_any =
925 {
926   probe_any_is_linespec,
927   probe_any_get_probes,
928 };
929
930 /* See comments in probe.h.  */
931
932 struct cmd_list_element **
933 info_probes_cmdlist_get (void)
934 {
935   static struct cmd_list_element *info_probes_cmdlist;
936
937   if (info_probes_cmdlist == NULL)
938     add_prefix_cmd ("probes", class_info, info_probes_command,
939                     _("\
940 Show available static probes.\n\
941 Usage: info probes [all|TYPE [ARGS]]\n\
942 TYPE specifies the type of the probe, and can be one of the following:\n\
943   - stap\n\
944 If you specify TYPE, there may be additional arguments needed by the\n\
945 subcommand.\n\
946 If you do not specify any argument, or specify `all', then the command\n\
947 will show information about all types of probes."),
948                     &info_probes_cmdlist, "info probes ",
949                     0/*allow-unknown*/, &infolist);
950
951   return &info_probes_cmdlist;
952 }
953
954 \f
955
956 /* This is called to compute the value of one of the $_probe_arg*
957    convenience variables.  */
958
959 static struct value *
960 compute_probe_arg (struct gdbarch *arch, struct internalvar *ivar,
961                    void *data)
962 {
963   struct frame_info *frame = get_selected_frame (_("No frame selected"));
964   CORE_ADDR pc = get_frame_pc (frame);
965   int sel = (int) (uintptr_t) data;
966   struct bound_probe pc_probe;
967   const struct sym_probe_fns *pc_probe_fns;
968   unsigned n_args;
969
970   /* SEL == -1 means "_probe_argc".  */
971   gdb_assert (sel >= -1);
972
973   pc_probe = find_probe_by_pc (pc);
974   if (pc_probe.probe == NULL)
975     error (_("No probe at PC %s"), core_addr_to_string (pc));
976
977   n_args = get_probe_argument_count (pc_probe.probe, frame);
978   if (sel == -1)
979     return value_from_longest (builtin_type (arch)->builtin_int, n_args);
980
981   if (sel >= n_args)
982     error (_("Invalid probe argument %d -- probe has %u arguments available"),
983            sel, n_args);
984
985   return evaluate_probe_argument (pc_probe.probe, sel, frame);
986 }
987
988 /* This is called to compile one of the $_probe_arg* convenience
989    variables into an agent expression.  */
990
991 static void
992 compile_probe_arg (struct internalvar *ivar, struct agent_expr *expr,
993                    struct axs_value *value, void *data)
994 {
995   CORE_ADDR pc = expr->scope;
996   int sel = (int) (uintptr_t) data;
997   struct bound_probe pc_probe;
998   const struct sym_probe_fns *pc_probe_fns;
999   int n_args;
1000   struct frame_info *frame = get_selected_frame (NULL);
1001
1002   /* SEL == -1 means "_probe_argc".  */
1003   gdb_assert (sel >= -1);
1004
1005   pc_probe = find_probe_by_pc (pc);
1006   if (pc_probe.probe == NULL)
1007     error (_("No probe at PC %s"), core_addr_to_string (pc));
1008
1009   n_args = get_probe_argument_count (pc_probe.probe, frame);
1010
1011   if (sel == -1)
1012     {
1013       value->kind = axs_rvalue;
1014       value->type = builtin_type (expr->gdbarch)->builtin_int;
1015       ax_const_l (expr, n_args);
1016       return;
1017     }
1018
1019   gdb_assert (sel >= 0);
1020   if (sel >= n_args)
1021     error (_("Invalid probe argument %d -- probe has %d arguments available"),
1022            sel, n_args);
1023
1024   pc_probe.probe->pops->compile_to_ax (pc_probe.probe, expr, value, sel);
1025 }
1026
1027 static const struct internalvar_funcs probe_funcs =
1028 {
1029   compute_probe_arg,
1030   compile_probe_arg,
1031   NULL
1032 };
1033
1034
1035 VEC (probe_ops_cp) *all_probe_ops;
1036
1037 void _initialize_probe (void);
1038
1039 void
1040 _initialize_probe (void)
1041 {
1042   VEC_safe_push (probe_ops_cp, all_probe_ops, &probe_ops_any);
1043
1044   create_internalvar_type_lazy ("_probe_argc", &probe_funcs,
1045                                 (void *) (uintptr_t) -1);
1046   create_internalvar_type_lazy ("_probe_arg0", &probe_funcs,
1047                                 (void *) (uintptr_t) 0);
1048   create_internalvar_type_lazy ("_probe_arg1", &probe_funcs,
1049                                 (void *) (uintptr_t) 1);
1050   create_internalvar_type_lazy ("_probe_arg2", &probe_funcs,
1051                                 (void *) (uintptr_t) 2);
1052   create_internalvar_type_lazy ("_probe_arg3", &probe_funcs,
1053                                 (void *) (uintptr_t) 3);
1054   create_internalvar_type_lazy ("_probe_arg4", &probe_funcs,
1055                                 (void *) (uintptr_t) 4);
1056   create_internalvar_type_lazy ("_probe_arg5", &probe_funcs,
1057                                 (void *) (uintptr_t) 5);
1058   create_internalvar_type_lazy ("_probe_arg6", &probe_funcs,
1059                                 (void *) (uintptr_t) 6);
1060   create_internalvar_type_lazy ("_probe_arg7", &probe_funcs,
1061                                 (void *) (uintptr_t) 7);
1062   create_internalvar_type_lazy ("_probe_arg8", &probe_funcs,
1063                                 (void *) (uintptr_t) 8);
1064   create_internalvar_type_lazy ("_probe_arg9", &probe_funcs,
1065                                 (void *) (uintptr_t) 9);
1066   create_internalvar_type_lazy ("_probe_arg10", &probe_funcs,
1067                                 (void *) (uintptr_t) 10);
1068   create_internalvar_type_lazy ("_probe_arg11", &probe_funcs,
1069                                 (void *) (uintptr_t) 11);
1070
1071   add_cmd ("all", class_info, info_probes_command,
1072            _("\
1073 Show information about all type of probes."),
1074            info_probes_cmdlist_get ());
1075
1076   add_cmd ("probes", class_breakpoint, enable_probes_command, _("\
1077 Enable probes.\n\
1078 Usage: enable probes [PROVIDER [NAME [OBJECT]]]\n\
1079 Each argument is a regular expression, used to select probes.\n\
1080 PROVIDER matches probe provider names.\n\
1081 NAME matches the probe names.\n\
1082 OBJECT matches the executable or shared library name.\n\
1083 If you do not specify any argument then the command will enable\n\
1084 all defined probes."),
1085            &enablelist);
1086
1087   add_cmd ("probes", class_breakpoint, disable_probes_command, _("\
1088 Disable probes.\n\
1089 Usage: disable probes [PROVIDER [NAME [OBJECT]]]\n\
1090 Each argument is a regular expression, used to select probes.\n\
1091 PROVIDER matches probe provider names.\n\
1092 NAME matches the probe names.\n\
1093 OBJECT matches the executable or shared library name.\n\
1094 If you do not specify any argument then the command will disable\n\
1095 all defined probes."),
1096            &disablelist);
1097
1098 }