Automatic date update in version.in
[binutils-gdb.git] / gdb / mi / mi-cmd-file.c
blob756a50f504d0dcb2cfa78c3258241a96f6a9b1fe
1 /* MI Command Set - file commands.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
5 This file is part of GDB.
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.
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.
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/>. */
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 "solib.h"
29 #include "solist.h"
30 #include "gdbsupport/gdb_regex.h"
32 /* Return to the client the absolute path and line number of the
33 current file being executed. */
35 void
36 mi_cmd_file_list_exec_source_file (const char *command,
37 const char *const *argv, int argc)
39 struct symtab_and_line st;
40 struct ui_out *uiout = current_uiout;
42 if (!mi_valid_noargs ("-file-list-exec-source-file", argc, argv))
43 error (_("-file-list-exec-source-file: Usage: No args"));
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 ();
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"));
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));
59 uiout->field_string ("fullname", symtab_to_fullname (st.symtab));
61 uiout->field_signed ("macro-info",
62 st.symtab->compunit ()->macro_table () != NULL);
65 /* Implement -file-list-exec-source-files command. */
67 void
68 mi_cmd_file_list_exec_source_files (const char *command,
69 const char *const *argv, int argc)
71 enum opt
73 GROUP_BY_OBJFILE_OPT,
74 MATCH_BASENAME_OPT,
75 MATCH_DIRNAME_OPT
77 static const struct mi_opt opts[] =
79 {"-group-by-objfile", GROUP_BY_OBJFILE_OPT, 0},
80 {"-basename", MATCH_BASENAME_OPT, 0},
81 {"-dirname", MATCH_DIRNAME_OPT, 0},
82 { 0, 0, 0 }
85 /* Parse arguments. */
86 int oind = 0;
87 const char *oarg;
89 bool group_by_objfile = false;
90 bool match_on_basename = false;
91 bool match_on_dirname = false;
93 while (1)
95 int opt = mi_getopt ("-file-list-exec-source-files", argc, argv,
96 opts, &oind, &oarg);
97 if (opt < 0)
98 break;
99 switch ((enum opt) opt)
101 case GROUP_BY_OBJFILE_OPT:
102 group_by_objfile = true;
103 break;
104 case MATCH_BASENAME_OPT:
105 match_on_basename = true;
106 break;
107 case MATCH_DIRNAME_OPT:
108 match_on_dirname = true;
109 break;
113 if ((argc - oind > 1) || (match_on_basename && match_on_dirname))
114 error (_("-file-list-exec-source-files: Usage: [--group-by-objfile] [--basename | --dirname] [--] REGEXP"));
116 const char *regexp = nullptr;
117 if (argc - oind == 1)
118 regexp = argv[oind];
120 info_sources_filter::match_on match_type;
121 if (match_on_dirname)
122 match_type = info_sources_filter::match_on::DIRNAME;
123 else if (match_on_basename)
124 match_type = info_sources_filter::match_on::BASENAME;
125 else
126 match_type = info_sources_filter::match_on::FULLNAME;
128 info_sources_filter filter (match_type, regexp);
129 info_sources_worker (current_uiout, group_by_objfile, filter);
132 /* See mi-cmds.h. */
134 void
135 mi_cmd_file_list_shared_libraries (const char *command,
136 const char *const *argv, int argc)
138 struct ui_out *uiout = current_uiout;
139 const char *pattern;
141 switch (argc)
143 case 0:
144 pattern = NULL;
145 break;
146 case 1:
147 pattern = argv[0];
148 break;
149 default:
150 error (_("Usage: -file-list-shared-libraries [REGEXP]"));
153 if (pattern != NULL)
155 const char *re_err = re_comp (pattern);
157 if (re_err != NULL)
158 error (_("Invalid regexp: %s"), re_err);
161 update_solib_list (1);
163 /* Print the table header. */
164 ui_out_emit_list list_emitter (uiout, "shared-libraries");
166 for (const shobj &so : current_program_space->solibs ())
168 if (so.so_name.empty ())
169 continue;
171 if (pattern != nullptr && !re_exec (so.so_name.c_str ()))
172 continue;
174 ui_out_emit_tuple tuple_emitter (uiout, NULL);
175 mi_output_solib_attribs (uiout, so);