* solist.h (target_so_ops.find_and_open_solib): Clarify usage of
[external/binutils.git] / gdb / probe.c
1 /* Generic static probe support for GDB.
2
3    Copyright (C) 2012-2013 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 "exceptions.h"
30 #include "linespec.h"
31 #include "gdb_regex.h"
32 #include "frame.h"
33 #include "arch-utils.h"
34 #include <ctype.h>
35
36 \f
37
38 /* See definition in probe.h.  */
39
40 struct symtabs_and_lines
41 parse_probes (char **argptr, struct linespec_result *canonical)
42 {
43   char *arg_start, *arg_end, *arg;
44   char *objfile_name = NULL, *provider = NULL, *name, *p;
45   struct cleanup *cleanup;
46   struct symtabs_and_lines result;
47   struct objfile *objfile;
48   struct program_space *pspace;
49   const struct probe_ops *probe_ops;
50   const char *cs;
51
52   result.sals = NULL;
53   result.nelts = 0;
54
55   arg_start = *argptr;
56
57   cs = *argptr;
58   probe_ops = probe_linespec_to_ops (&cs);
59   gdb_assert (probe_ops != NULL);
60
61   arg = (char *) cs;
62   arg = skip_spaces (arg);
63   if (!*arg)
64     error (_("argument to `%s' missing"), arg_start);
65
66   arg_end = skip_to_space (arg);
67
68   /* We make a copy here so we can write over parts with impunity.  */
69   arg = savestring (arg, arg_end - arg);
70   cleanup = make_cleanup (xfree, arg);
71
72   /* Extract each word from the argument, separated by ":"s.  */
73   p = strchr (arg, ':');
74   if (p == NULL)
75     {
76       /* This is `-p name'.  */
77       name = arg;
78     }
79   else
80     {
81       char *hold = p + 1;
82
83       *p = '\0';
84       p = strchr (hold, ':');
85       if (p == NULL)
86         {
87           /* This is `-p provider:name'.  */
88           provider = arg;
89           name = hold;
90         }
91       else
92         {
93           /* This is `-p objfile:provider:name'.  */
94           *p = '\0';
95           objfile_name = arg;
96           provider = hold;
97           name = p + 1;
98         }
99     }
100
101   if (*name == '\0')
102     error (_("no probe name specified"));
103   if (provider && *provider == '\0')
104     error (_("invalid provider name"));
105   if (objfile_name && *objfile_name == '\0')
106     error (_("invalid objfile name"));
107
108   ALL_PSPACES (pspace)
109     ALL_PSPACE_OBJFILES (pspace, objfile)
110       {
111         VEC (probe_p) *probes;
112         struct probe *probe;
113         int ix;
114
115         if (!objfile->sf || !objfile->sf->sym_probe_fns)
116           continue;
117
118         if (objfile_name
119             && FILENAME_CMP (objfile->name, objfile_name) != 0
120             && FILENAME_CMP (lbasename (objfile->name), objfile_name) != 0)
121           continue;
122
123         probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
124
125         for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
126           {
127             struct symtab_and_line *sal;
128
129             if (probe_ops != &probe_ops_any && probe->pops != probe_ops)
130               continue;
131
132             if (provider && strcmp (probe->provider, provider) != 0)
133               continue;
134
135             if (strcmp (probe->name, name) != 0)
136               continue;
137
138             ++result.nelts;
139             result.sals = xrealloc (result.sals,
140                                     result.nelts
141                                     * sizeof (struct symtab_and_line));
142             sal = &result.sals[result.nelts - 1];
143
144             init_sal (sal);
145
146             sal->pc = probe->address;
147             sal->explicit_pc = 1;
148             sal->section = find_pc_overlay (sal->pc);
149             sal->pspace = pspace;
150             sal->probe = probe;
151           }
152       }
153
154   if (result.nelts == 0)
155     {
156       throw_error (NOT_FOUND_ERROR,
157                    _("No probe matching objfile=`%s', provider=`%s', name=`%s'"),
158                    objfile_name ? objfile_name : _("<any>"),
159                    provider ? provider : _("<any>"),
160                    name);
161     }
162
163   if (canonical)
164     {
165       canonical->special_display = 1;
166       canonical->pre_expanded = 1;
167       canonical->addr_string = savestring (*argptr, arg_end - *argptr);
168     }
169
170   *argptr = arg_end;
171   do_cleanups (cleanup);
172
173   return result;
174 }
175
176 /* See definition in probe.h.  */
177
178 VEC (probe_p) *
179 find_probes_in_objfile (struct objfile *objfile, const char *provider,
180                         const char *name)
181 {
182   VEC (probe_p) *probes, *result = NULL;
183   int ix;
184   struct probe *probe;
185
186   if (!objfile->sf || !objfile->sf->sym_probe_fns)
187     return NULL;
188
189   probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
190   for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
191     {
192       if (strcmp (probe->provider, provider) != 0)
193         continue;
194
195       if (strcmp (probe->name, name) != 0)
196         continue;
197
198       VEC_safe_push (probe_p, result, probe);
199     }
200
201   return result;
202 }
203
204 /* See definition in probe.h.  */
205
206 struct probe *
207 find_probe_by_pc (CORE_ADDR pc)
208 {
209   struct objfile *objfile;
210
211   ALL_OBJFILES (objfile)
212   {
213     VEC (probe_p) *probes;
214     int ix;
215     struct probe *probe;
216
217     if (!objfile->sf || !objfile->sf->sym_probe_fns)
218       continue;
219
220     /* If this proves too inefficient, we can replace with a hash.  */
221     probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
222     for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
223       if (probe->address == pc)
224         return probe;
225   }
226
227   return NULL;
228 }
229
230 \f
231
232 /* Make a vector of probes matching OBJNAME, PROVIDER, and PROBE_NAME.
233    If POPS is not NULL, only probes of this certain probe_ops will match.
234    Each argument is a regexp, or NULL, which matches anything.  */
235
236 static VEC (probe_p) *
237 collect_probes (char *objname, char *provider, char *probe_name,
238                 const struct probe_ops *pops)
239 {
240   struct objfile *objfile;
241   VEC (probe_p) *result = NULL;
242   struct cleanup *cleanup, *cleanup_temps;
243   regex_t obj_pat, prov_pat, probe_pat;
244
245   cleanup = make_cleanup (VEC_cleanup (probe_p), &result);
246
247   cleanup_temps = make_cleanup (null_cleanup, NULL);
248   if (provider != NULL)
249     compile_rx_or_error (&prov_pat, provider, _("Invalid provider regexp"));
250   if (probe_name != NULL)
251     compile_rx_or_error (&probe_pat, probe_name, _("Invalid probe regexp"));
252   if (objname != NULL)
253     compile_rx_or_error (&obj_pat, objname, _("Invalid object file regexp"));
254
255   ALL_OBJFILES (objfile)
256     {
257       VEC (probe_p) *probes;
258       struct probe *probe;
259       int ix;
260
261       if (! objfile->sf || ! objfile->sf->sym_probe_fns)
262         continue;
263
264       if (objname)
265         {
266           if (regexec (&obj_pat, objfile->name, 0, NULL, 0) != 0)
267             continue;
268         }
269
270       probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
271
272       for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
273         {
274           if (pops != NULL && probe->pops != pops)
275             continue;
276
277           if (provider
278               && regexec (&prov_pat, probe->provider, 0, NULL, 0) != 0)
279             continue;
280
281           if (probe_name
282               && regexec (&probe_pat, probe->name, 0, NULL, 0) != 0)
283             continue;
284
285           VEC_safe_push (probe_p, result, probe);
286         }
287     }
288
289   do_cleanups (cleanup_temps);
290   discard_cleanups (cleanup);
291   return result;
292 }
293
294 /* A qsort comparison function for probe_p objects.  */
295
296 static int
297 compare_probes (const void *a, const void *b)
298 {
299   const struct probe *pa = *((const struct probe **) a);
300   const struct probe *pb = *((const struct probe **) b);
301   int v;
302
303   v = strcmp (pa->provider, pb->provider);
304   if (v)
305     return v;
306
307   v = strcmp (pa->name, pb->name);
308   if (v)
309     return v;
310
311   if (pa->address < pb->address)
312     return -1;
313   if (pa->address > pb->address)
314     return 1;
315
316   return strcmp (pa->objfile->name, pb->objfile->name);
317 }
318
319 /* Helper function that generate entries in the ui_out table being
320    crafted by `info_probes_for_ops'.  */
321
322 static void
323 gen_ui_out_table_header_info (VEC (probe_p) *probes,
324                               const struct probe_ops *p)
325 {
326   /* `headings' refers to the names of the columns when printing `info
327      probes'.  */
328   VEC (info_probe_column_s) *headings = NULL;
329   struct cleanup *c;
330   info_probe_column_s *column;
331   size_t headings_size;
332   int ix;
333
334   gdb_assert (p != NULL);
335
336   if (p->gen_info_probes_table_header == NULL
337       && p->gen_info_probes_table_values == NULL)
338     return;
339
340   gdb_assert (p->gen_info_probes_table_header != NULL
341               && p->gen_info_probes_table_values != NULL);
342
343   c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
344   p->gen_info_probes_table_header (&headings);
345
346   headings_size = VEC_length (info_probe_column_s, headings);
347
348   for (ix = 0;
349        VEC_iterate (info_probe_column_s, headings, ix, column);
350        ++ix)
351     {
352       struct probe *probe;
353       int jx;
354       size_t size_max = strlen (column->print_name);
355
356       for (jx = 0; VEC_iterate (probe_p, probes, jx, probe); ++jx)
357         {
358           /* `probe_fields' refers to the values of each new field that this
359              probe will display.  */
360           VEC (const_char_ptr) *probe_fields = NULL;
361           struct cleanup *c2;
362           const char *val;
363           int kx;
364
365           if (probe->pops != p)
366             continue;
367
368           c2 = make_cleanup (VEC_cleanup (const_char_ptr), &probe_fields);
369           p->gen_info_probes_table_values (probe, &probe_fields);
370
371           gdb_assert (VEC_length (const_char_ptr, probe_fields)
372                       == headings_size);
373
374           for (kx = 0; VEC_iterate (const_char_ptr, probe_fields, kx, val);
375                ++kx)
376             {
377               /* It is valid to have a NULL value here, which means that the
378                  backend does not have something to write and this particular
379                  field should be skipped.  */
380               if (val == NULL)
381                 continue;
382
383               size_max = max (strlen (val), size_max);
384             }
385           do_cleanups (c2);
386         }
387
388       ui_out_table_header (current_uiout, size_max, ui_left,
389                            column->field_name, column->print_name);
390     }
391
392   do_cleanups (c);
393 }
394
395 /* Helper function to print extra information about a probe and an objfile
396    represented by PROBE.  */
397
398 static void
399 print_ui_out_info (struct probe *probe)
400 {
401   int ix;
402   int j = 0;
403   /* `values' refers to the actual values of each new field in the output
404      of `info probe'.  `headings' refers to the names of each new field.  */
405   VEC (const_char_ptr) *values = NULL;
406   VEC (info_probe_column_s) *headings = NULL;
407   info_probe_column_s *column;
408   struct cleanup *c;
409
410   gdb_assert (probe != NULL);
411   gdb_assert (probe->pops != NULL);
412
413   if (probe->pops->gen_info_probes_table_header == NULL
414       && probe->pops->gen_info_probes_table_values == NULL)
415     return;
416
417   gdb_assert (probe->pops->gen_info_probes_table_header != NULL
418               && probe->pops->gen_info_probes_table_values != NULL);
419
420   c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
421   make_cleanup (VEC_cleanup (const_char_ptr), &values);
422
423   probe->pops->gen_info_probes_table_header (&headings);
424   probe->pops->gen_info_probes_table_values (probe, &values);
425
426   gdb_assert (VEC_length (info_probe_column_s, headings)
427               == VEC_length (const_char_ptr, values));
428
429   for (ix = 0;
430        VEC_iterate (info_probe_column_s, headings, ix, column);
431        ++ix)
432     {
433       const char *val = VEC_index (const_char_ptr, values, j++);
434
435       if (val == NULL)
436         ui_out_field_skip (current_uiout, column->field_name);
437       else
438         ui_out_field_string (current_uiout, column->field_name, val);
439     }
440
441   do_cleanups (c);
442 }
443
444 /* Helper function that returns the number of extra fields which POPS will
445    need.  */
446
447 static int
448 get_number_extra_fields (const struct probe_ops *pops)
449 {
450   VEC (info_probe_column_s) *headings = NULL;
451   struct cleanup *c;
452   int n;
453
454   if (pops->gen_info_probes_table_header == NULL)
455     return 0;
456
457   c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
458   pops->gen_info_probes_table_header (&headings);
459
460   n = VEC_length (info_probe_column_s, headings);
461
462   do_cleanups (c);
463
464   return n;
465 }
466
467 /* See comment in probe.h.  */
468
469 void
470 info_probes_for_ops (char *arg, int from_tty, const struct probe_ops *pops)
471 {
472   char *provider, *probe_name = NULL, *objname = NULL;
473   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
474   VEC (probe_p) *probes;
475   int i, any_found;
476   int ui_out_extra_fields = 0;
477   size_t size_addr;
478   size_t size_name = strlen ("Name");
479   size_t size_objname = strlen ("Object");
480   size_t size_provider = strlen ("Provider");
481   struct probe *probe;
482   struct gdbarch *gdbarch = get_current_arch ();
483
484   /* Do we have a `provider:probe:objfile' style of linespec?  */
485   provider = extract_arg (&arg);
486   if (provider)
487     {
488       make_cleanup (xfree, provider);
489
490       probe_name = extract_arg (&arg);
491       if (probe_name)
492         {
493           make_cleanup (xfree, probe_name);
494
495           objname = extract_arg (&arg);
496           if (objname)
497             make_cleanup (xfree, objname);
498         }
499     }
500
501   if (pops == NULL)
502     {
503       const struct probe_ops *po;
504       int ix;
505
506       /* If the probe_ops is NULL, it means the user has requested a "simple"
507          `info probes', i.e., she wants to print all information about all
508          probes.  For that, we have to identify how many extra fields we will
509          need to add in the ui_out table.
510
511          To do that, we iterate over all probe_ops, querying each one about
512          its extra fields, and incrementing `ui_out_extra_fields' to reflect
513          that number.  */
514
515       for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
516         ui_out_extra_fields += get_number_extra_fields (po);
517     }
518   else
519     ui_out_extra_fields = get_number_extra_fields (pops);
520
521   probes = collect_probes (objname, provider, probe_name, pops);
522   make_cleanup (VEC_cleanup (probe_p), &probes);
523   make_cleanup_ui_out_table_begin_end (current_uiout,
524                                        4 + ui_out_extra_fields,
525                                        VEC_length (probe_p, probes),
526                                        "StaticProbes");
527
528   if (!VEC_empty (probe_p, probes))
529     qsort (VEC_address (probe_p, probes), VEC_length (probe_p, probes),
530            sizeof (probe_p), compare_probes);
531
532   /* What's the size of an address in our architecture?  */
533   size_addr = gdbarch_addr_bit (gdbarch) == 64 ? 18 : 10;
534
535   /* Determining the maximum size of each field (`provider', `name' and
536      `objname').  */
537   for (i = 0; VEC_iterate (probe_p, probes, i, probe); ++i)
538     {
539       size_name = max (strlen (probe->name), size_name);
540       size_provider = max (strlen (probe->provider), size_provider);
541       size_objname = max (strlen (probe->objfile->name), size_objname);
542     }
543
544   ui_out_table_header (current_uiout, size_provider, ui_left, "provider",
545                        _("Provider"));
546   ui_out_table_header (current_uiout, size_name, ui_left, "name", _("Name"));
547   ui_out_table_header (current_uiout, size_addr, ui_left, "addr", _("Where"));
548
549   if (pops == NULL)
550     {
551       const struct probe_ops *po;
552       int ix;
553
554       /* We have to generate the table header for each new probe type that we
555          will print.  */
556       for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
557         gen_ui_out_table_header_info (probes, po);
558     }
559   else
560     gen_ui_out_table_header_info (probes, pops);
561
562   ui_out_table_header (current_uiout, size_objname, ui_left, "object",
563                        _("Object"));
564   ui_out_table_body (current_uiout);
565
566   for (i = 0; VEC_iterate (probe_p, probes, i, probe); ++i)
567     {
568       struct cleanup *inner;
569
570       inner = make_cleanup_ui_out_tuple_begin_end (current_uiout, "probe");
571
572       ui_out_field_string (current_uiout, "provider", probe->provider);
573       ui_out_field_string (current_uiout, "name", probe->name);
574       ui_out_field_core_addr (current_uiout, "addr",
575                               get_objfile_arch (probe->objfile),
576                               probe->address);
577
578       if (pops == NULL)
579         {
580           const struct probe_ops *po;
581           int ix;
582
583           for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po);
584                ++ix)
585             if (probe->pops == po)
586               print_ui_out_info (probe);
587         }
588       else
589         print_ui_out_info (probe);
590
591       ui_out_field_string (current_uiout, "object", probe->objfile->name);
592       ui_out_text (current_uiout, "\n");
593
594       do_cleanups (inner);
595     }
596
597   any_found = !VEC_empty (probe_p, probes);
598   do_cleanups (cleanup);
599
600   if (!any_found)
601     ui_out_message (current_uiout, 0, _("No probes matched.\n"));
602 }
603
604 /* Implementation of the `info probes' command.  */
605
606 static void
607 info_probes_command (char *arg, int from_tty)
608 {
609   info_probes_for_ops (arg, from_tty, NULL);
610 }
611
612 /* See comments in probe.h.  */
613
614 unsigned
615 get_probe_argument_count (struct probe *probe)
616 {
617   const struct sym_probe_fns *probe_fns;
618
619   gdb_assert (probe->objfile != NULL);
620   gdb_assert (probe->objfile->sf != NULL);
621
622   probe_fns = probe->objfile->sf->sym_probe_fns;
623
624   gdb_assert (probe_fns != NULL);
625
626   return probe_fns->sym_get_probe_argument_count (probe);
627 }
628
629 /* See comments in probe.h.  */
630
631 struct value *
632 evaluate_probe_argument (struct probe *probe, unsigned n)
633 {
634   const struct sym_probe_fns *probe_fns;
635
636   gdb_assert (probe->objfile != NULL);
637   gdb_assert (probe->objfile->sf != NULL);
638
639   probe_fns = probe->objfile->sf->sym_probe_fns;
640
641   gdb_assert (probe_fns != NULL);
642
643   return probe_fns->sym_evaluate_probe_argument (probe, n);
644 }
645
646 /* See comments in probe.h.  */
647
648 struct value *
649 probe_safe_evaluate_at_pc (struct frame_info *frame, unsigned n)
650 {
651   struct probe *probe;
652   unsigned n_args;
653
654   probe = find_probe_by_pc (get_frame_pc (frame));
655   if (!probe)
656     return NULL;
657
658   n_args = get_probe_argument_count (probe);
659   if (n >= n_args)
660     return NULL;
661
662   return evaluate_probe_argument (probe, n);
663 }
664
665 /* See comment in probe.h.  */
666
667 const struct probe_ops *
668 probe_linespec_to_ops (const char **linespecp)
669 {
670   int ix;
671   const struct probe_ops *probe_ops;
672
673   for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, probe_ops); ix++)
674     if (probe_ops->is_linespec (linespecp))
675       return probe_ops;
676
677   return NULL;
678 }
679
680 /* See comment in probe.h.  */
681
682 int
683 probe_is_linespec_by_keyword (const char **linespecp, const char *const *keywords)
684 {
685   const char *s = *linespecp;
686   const char *const *csp;
687
688   for (csp = keywords; *csp; csp++)
689     {
690       const char *keyword = *csp;
691       size_t len = strlen (keyword);
692
693       if (strncmp (s, keyword, len) == 0 && isspace (s[len]))
694         {
695           *linespecp += len + 1;
696           return 1;
697         }
698     }
699
700   return 0;
701 }
702
703 /* Implementation of `is_linespec' method for `struct probe_ops'.  */
704
705 static int
706 probe_any_is_linespec (const char **linespecp)
707 {
708   static const char *const keywords[] = { "-p", "-probe", NULL };
709
710   return probe_is_linespec_by_keyword (linespecp, keywords);
711 }
712
713 /* Dummy method used for `probe_ops_any'.  */
714
715 static void
716 probe_any_get_probes (VEC (probe_p) **probesp, struct objfile *objfile)
717 {
718   /* No probes can be provided by this dummy backend.  */
719 }
720
721 /* Operations associated with a generic probe.  */
722
723 const struct probe_ops probe_ops_any =
724 {
725   probe_any_is_linespec,
726   probe_any_get_probes,
727 };
728
729 /* See comments in probe.h.  */
730
731 struct cmd_list_element **
732 info_probes_cmdlist_get (void)
733 {
734   static struct cmd_list_element *info_probes_cmdlist;
735
736   if (info_probes_cmdlist == NULL)
737     add_prefix_cmd ("probes", class_info, info_probes_command,
738                     _("\
739 Show available static probes.\n\
740 Usage: info probes [all|TYPE [ARGS]]\n\
741 TYPE specifies the type of the probe, and can be one of the following:\n\
742   - stap\n\
743 If you specify TYPE, there may be additional arguments needed by the\n\
744 subcommand.\n\
745 If you do not specify any argument, or specify `all', then the command\n\
746 will show information about all types of probes."),
747                     &info_probes_cmdlist, "info probes ",
748                     0/*allow-unknown*/, &infolist);
749
750   return &info_probes_cmdlist;
751 }
752
753 VEC (probe_ops_cp) *all_probe_ops;
754
755 void _initialize_probe (void);
756
757 void
758 _initialize_probe (void)
759 {
760   VEC_safe_push (probe_ops_cp, all_probe_ops, &probe_ops_any);
761
762   add_cmd ("all", class_info, info_probes_command,
763            _("\
764 Show information about all type of probes."),
765            info_probes_cmdlist_get ());
766 }