gdb/mi: add regexp filtering to -file-list-exec-source-files
[binutils-gdb.git] / gdb / mi / mi-cmd-file.c
1 /* MI Command Set - file commands.
2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
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 "mi-cmds.h"
22 #include "mi-getopt.h"
23 #include "mi-interp.h"
24 #include "ui-out.h"
25 #include "symtab.h"
26 #include "source.h"
27 #include "objfiles.h"
28 #include "psymtab.h"
29 #include "solib.h"
30 #include "solist.h"
31 #include "gdb_regex.h"
32
33 /* Return to the client the absolute path and line number of the
34 current file being executed. */
35
36 void
37 mi_cmd_file_list_exec_source_file (const char *command, char **argv, int argc)
38 {
39 struct symtab_and_line st;
40 struct ui_out *uiout = current_uiout;
41
42 if (!mi_valid_noargs ("-file-list-exec-source-file", argc, argv))
43 error (_("-file-list-exec-source-file: Usage: No args"));
44
45 /* Set the default file and line, also get them. */
46 set_default_source_symtab_and_line ();
47 st = get_current_source_symtab_and_line ();
48
49 /* We should always get a symtab. Apparently, filename does not
50 need to be tested for NULL. The documentation in symtab.h
51 suggests it will always be correct. */
52 if (!st.symtab)
53 error (_("-file-list-exec-source-file: No symtab"));
54
55 /* Print to the user the line, filename and fullname. */
56 uiout->field_signed ("line", st.line);
57 uiout->field_string ("file", symtab_to_filename_for_display (st.symtab));
58
59 uiout->field_string ("fullname", symtab_to_fullname (st.symtab));
60
61 uiout->field_signed ("macro-info",
62 COMPUNIT_MACRO_TABLE (SYMTAB_COMPUNIT (st.symtab)) != NULL);
63 }
64
65 /* Implement -file-list-exec-source-files command. */
66
67 void
68 mi_cmd_file_list_exec_source_files (const char *command, char **argv, int argc)
69 {
70 enum opt
71 {
72 MATCH_BASENAME_OPT,
73 MATCH_DIRNAME_OPT
74 };
75 static const struct mi_opt opts[] =
76 {
77 {"-basename", MATCH_BASENAME_OPT, 0},
78 {"-dirname", MATCH_DIRNAME_OPT, 0},
79 { 0, 0, 0 }
80 };
81
82 /* Parse arguments. */
83 int oind = 0;
84 char *oarg;
85
86 bool match_on_basename = false;
87 bool match_on_dirname = false;
88
89 while (1)
90 {
91 int opt = mi_getopt ("-file-list-exec-source-files", argc, argv,
92 opts, &oind, &oarg);
93 if (opt < 0)
94 break;
95 switch ((enum opt) opt)
96 {
97 case MATCH_BASENAME_OPT:
98 match_on_basename = true;
99 break;
100 case MATCH_DIRNAME_OPT:
101 match_on_dirname = true;
102 break;
103 }
104 }
105
106 if ((argc - oind > 1) || (match_on_basename && match_on_dirname))
107 error (_("-file-list-exec-source-files: Usage: [--basename | --dirname] [--] REGEXP"));
108
109 const char *regexp = nullptr;
110 if (argc - oind == 1)
111 regexp = argv[oind];
112
113 info_sources_filter::match_on match_type;
114 if (match_on_dirname)
115 match_type = info_sources_filter::match_on::DIRNAME;
116 else if (match_on_basename)
117 match_type = info_sources_filter::match_on::BASENAME;
118 else
119 match_type = info_sources_filter::match_on::FULLNAME;
120
121 info_sources_filter filter (match_type, regexp);
122 info_sources_worker (current_uiout, filter);
123 }
124
125 /* See mi-cmds.h. */
126
127 void
128 mi_cmd_file_list_shared_libraries (const char *command, char **argv, int argc)
129 {
130 struct ui_out *uiout = current_uiout;
131 const char *pattern;
132
133 switch (argc)
134 {
135 case 0:
136 pattern = NULL;
137 break;
138 case 1:
139 pattern = argv[0];
140 break;
141 default:
142 error (_("Usage: -file-list-shared-libraries [REGEXP]"));
143 }
144
145 if (pattern != NULL)
146 {
147 const char *re_err = re_comp (pattern);
148
149 if (re_err != NULL)
150 error (_("Invalid regexp: %s"), re_err);
151 }
152
153 update_solib_list (1);
154
155 /* Print the table header. */
156 ui_out_emit_list list_emitter (uiout, "shared-libraries");
157
158 for (struct so_list *so : current_program_space->solibs ())
159 {
160 if (so->so_name[0] == '\0')
161 continue;
162 if (pattern != NULL && !re_exec (so->so_name))
163 continue;
164
165 ui_out_emit_tuple tuple_emitter (uiout, NULL);
166 mi_output_solib_attribs (uiout, so);
167 }
168 }