From: Matt Rice <ratmice@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: gdb-patches@sourceware.org
Subject: Re: [patch] PR 12999 info macros command.
Date: Thu, 21 Jul 2011 06:47:00 -0000 [thread overview]
Message-ID: <CACTLOFpFhXnxcWKLp2CHNn8mfp2_tNa=6GGdJ2Jmyzo8mS+d6w@mail.gmail.com> (raw)
In-Reply-To: <83oc0ovq7p.fsf@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 1671 bytes --]
On Wed, Jul 20, 2011 at 10:04 AM, Eli Zaretskii <eliz@gnu.org> wrote:
>> Date: Mon, 18 Jul 2011 23:57:25 -0700
>> From: Matt Rice <ratmice@gmail.com>
>>
>> this patch adds 2 commands,
>> info macros LINESPEC
>> info definitions MACRO
>
> Thanks. A few minor comments and a question.
>
>> +@cindex definition, showing a macro's
>
> That sounds awkward. Suggest "@cindex definition of a macro, showing"
> instead.
This actually came from the existing 'info macro'. command, I updated that.
>> +@item info macros @var{linespec}
>> +Show all macro definitions at the location of @var{linespec}, and describe the
>
> Suggest a slight rewording:
>
> Show all macro definitions that are in effect at the location
> specified by @var{linespec}, and describe ...
k.
>> +@cindex definition, showing a macro's
>
> It's not useful to have identical index entries so close to one
> another. I suggest to remove the second one.
ahh, I see.
>> +Show all definitions of the macro named @var{macro}, in the current scope,
>
> It might be better to say "... definitions of the named @var{macro} ...".
k.
> Also, what is the "scope" of a macro?
to answer your question scope of a macro is all lines between #define
and #undef, not extending beyond the boundry of a compilation unit.
Its the wrong word though, and not what 'info definitions' does,
neither was my second patch correct. because 'info definitions' looks
throughout a compilation unit.
Updated the docs, and the 'help' command text for 'info definitions'.
I added @cindex macros, from debug info
so it would be near the existing @cindex macros, user-defined
[-- Attachment #2: foo.diff --]
[-- Type: application/octet-stream, Size: 10093 bytes --]
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 3a3a9fb..49b467d 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -9588,11 +9588,24 @@ can be any string of tokens.
@kindex info macro
@cindex macro definition, showing
-@cindex definition, showing a macro's
+@cindex definition of a macro, showing
+@cindex macros, from debug info
@item info macro @var{macro}
-Show the definition of the macro named @var{macro}, and describe the
+Show the current definition of the named @var{macro}, and describe the
source location or compiler command-line where that definition was established.
+@kindex info macros
+@item info macros @var{linespec}
+Show all macro definitions that are in effect at the location specified
+by @var{linespec}, and describe the source location or compiler
+command-line where those definitions were established.
+
+@kindex info definitions
+@item info definitions @var{macro}
+Show all definitions of the named @var{macro} that are defined in the current
+compilation unit, and describe the source location or compiler command-line
+where those definitions were established.
+
@kindex macro define
@cindex user-defined macros
@cindex defining macros interactively
diff --git a/gdb/macrocmd.c b/gdb/macrocmd.c
index c77156a..b8f2746 100644
--- a/gdb/macrocmd.c
+++ b/gdb/macrocmd.c
@@ -117,7 +117,10 @@ macro_expand_once_command (char *exp, int from_tty)
return;
}
+/* Outputs the include path of a macro starting at FILE and LINE to STREAM.
+ Care should be taken that this function does not cause any lookups into
+ the splay tree so that it can be safely used while iterating. */
static void
show_pp_source_pos (struct ui_file *stream,
struct macro_source_file *file,
@@ -134,36 +137,27 @@ show_pp_source_pos (struct ui_file *stream,
}
}
+/* Outputs a macro for human consumption, detailing the include path
+ and macro definition. NAME is the name of the macro.
+ D the definition. FILE the start of the include path, and LINE the
+ line number in FILE.
+ Care should be taken that this function does not cause any lookups into
+ the splay tree so that it can be safely used while iterating. */
static void
-info_macro_command (char *name, int from_tty)
+print_macro_definition (const char *name,
+ const struct macro_definition *d,
+ struct macro_source_file *file,
+ int line)
{
- struct macro_scope *ms = NULL;
- struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
- struct macro_definition *d;
-
- if (! name || ! *name)
- error (_("You must follow the `info macro' command with the name"
- " of the macro\n"
- "whose definition you want to see."));
-
- ms = default_macro_scope ();
- if (! ms)
- error (_("GDB has no preprocessor macro information for that code."));
-
- d = macro_lookup_definition (ms->file, ms->line, name);
- if (d)
- {
- int line;
- struct macro_source_file *file
- = macro_definition_location (ms->file, ms->line, name, &line);
-
fprintf_filtered (gdb_stdout, "Defined at ");
show_pp_source_pos (gdb_stdout, file, line);
+
if (line != 0)
fprintf_filtered (gdb_stdout, "#define %s", name);
else
fprintf_filtered (gdb_stdout, "-D%s", name);
+
if (d->kind == macro_function_like)
{
int i;
@@ -177,10 +171,37 @@ info_macro_command (char *name, int from_tty)
}
fputs_filtered (")", gdb_stdout);
}
+
if (line != 0)
fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
else
fprintf_filtered (gdb_stdout, "=%s\n", d->replacement);
+}
+
+static void
+info_macro_command (char *name, int from_tty)
+{
+ struct macro_scope *ms = NULL;
+ struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
+ struct macro_definition *d;
+
+ if (! name || ! *name)
+ error (_("You must follow the `info macro' command with the name"
+ " of the macro\n"
+ "whose definition you want to see."));
+
+ ms = default_macro_scope ();
+ if (! ms)
+ error (_("GDB has no preprocessor macro information for that code."));
+
+ d = macro_lookup_definition (ms->file, ms->line, name);
+ if (d)
+ {
+ int line;
+ struct macro_source_file *file
+ = macro_definition_location (ms->file, ms->line, name, &line);
+
+ print_macro_definition (name, d, file, line);
}
else
{
@@ -194,6 +215,64 @@ info_macro_command (char *name, int from_tty)
do_cleanups (cleanup_chain);
}
+/* A callback function for usage with macro_for_each and friends.
+ If USER_DATA is null all macros will be printed.
+ Otherwise USER_DATA is considered to be a string, printing
+ only macros who's NAME matches USER_DATA. Other arguments are
+ routed to print_macro_definition. */
+static void
+print_macro_callback (const char *name, const struct macro_definition *macro,
+ struct macro_source_file *source, int line,
+ void *user_data)
+{
+ if (! user_data || strcmp (user_data, name) == 0)
+ print_macro_definition (name, macro, source, line);
+}
+
+/* Implementation of the "info definitions" command. */
+static void
+info_definitions_command (char *name, int from_tty)
+{
+ struct macro_scope *ms = NULL;
+ struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
+
+ if (! name || ! *name)
+ error (_("The `info definitions' command requires a macro name as an \
+argument.\n"
+ "Usage: info definitions MACRO\n"));
+
+ ms = default_macro_scope ();
+
+ if (! ms || ! ms->file || ! ms->file->table)
+ error (_("GDB has no preprocessor macro information for that code."));
+
+ macro_for_each (ms->file->table, print_macro_callback, name);
+ do_cleanups (cleanup_chain);
+}
+
+/* Implementation of the "info macros" command. */
+static void
+info_macros_command (char *args, int from_tty)
+{
+ struct macro_scope *ms = NULL;
+ struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
+
+ if (args == NULL)
+ ms = default_macro_scope ();
+ else
+ {
+ struct symtabs_and_lines sals = decode_line_spec (args, 0);
+
+ if (sals.nelts)
+ ms = sal_macro_scope (sals.sals[0]);
+ }
+
+ if (! ms || ! ms->file || ! ms->file->table)
+ error (_("GDB has no preprocessor macro information for that code."));
+
+ macro_for_each_in_scope (ms->file, ms->line, print_macro_callback, NULL);
+ do_cleanups (cleanup_chain);
+}
\f
/* User-defined macros. */
@@ -359,6 +438,7 @@ macro_undef_command (char *exp, int from_tty)
static void
print_one_macro (const char *name, const struct macro_definition *macro,
+ struct macro_source_file *source, int line,
void *ignore)
{
fprintf_filtered (gdb_stdout, "macro define %s", name);
@@ -382,7 +462,6 @@ macro_list_command (char *exp, int from_tty)
macro_for_each (macro_user_macros, print_one_macro, NULL);
}
-
\f
/* Initializing the `macrocmd' module. */
@@ -420,6 +499,18 @@ expression work together to yield a pre-processed expression."),
_("Show the definition of MACRO, and its source location."),
&infolist);
+ add_cmd ("macros", no_class, info_macros_command,
+ _("Show the definitions of all macros at LINESPEC, or the current \
+source location.\n\
+Usage: info source [LINESPEC]"),
+ &infolist);
+
+ add_cmd ("definitions", no_class, info_definitions_command,
+ _("Show all definitions of MACRO defined int the current \
+ compilation unit.\n\
+Usage: info definitions MACRO"),
+ &infolist);
+
add_cmd ("define", no_class, macro_define_command, _("\
Define a new C/C++ preprocessor macro.\n\
The GDB command `macro define DEFINITION' is equivalent to placing a\n\
diff --git a/gdb/macrotab.c b/gdb/macrotab.c
index cba285d..efcf835 100644
--- a/gdb/macrotab.c
+++ b/gdb/macrotab.c
@@ -913,7 +913,8 @@ foreach_macro (splay_tree_node node, void *arg)
struct macro_key *key = (struct macro_key *) node->key;
struct macro_definition *def = (struct macro_definition *) node->value;
- (*datum->fn) (key->name, def, datum->user_data);
+ (*datum->fn) (key->name, def, key->start_file, key->start_line,
+ datum->user_data);
return 0;
}
@@ -945,7 +946,8 @@ foreach_macro_in_scope (splay_tree_node node, void *info)
&& (!key->end_file
|| compare_locations (key->end_file, key->end_line,
datum->file, datum->line) >= 0))
- (*datum->fn) (key->name, def, datum->user_data);
+ (*datum->fn) (key->name, def, key->start_file, key->start_line,
+ datum->user_data);
return 0;
}
diff --git a/gdb/macrotab.h b/gdb/macrotab.h
index d2b2b63..a10351a 100644
--- a/gdb/macrotab.h
+++ b/gdb/macrotab.h
@@ -306,11 +306,14 @@ struct macro_source_file *(macro_definition_location
int *definition_line));
/* Callback function when walking a macro table. NAME is the name of
- the macro, and DEFINITION is the definition. USER_DATA is an
- arbitrary pointer which is passed by the caller to macro_for_each
- or macro_for_each_in_scope. */
+ the macro, and DEFINITION is the definition. SOURCE is the file at the
+ start of the include path, and LINE is the line number of the SOURCE file
+ where the macro was defined. USER_DATA is an arbitrary pointer which is
+ passed by the caller to macro_for_each or macro_for_each_in_scope. */
typedef void (*macro_callback_fn) (const char *name,
const struct macro_definition *definition,
+ struct macro_source_file *source,
+ int line,
void *user_data);
/* Call the function FN for each macro in the macro table TABLE.
diff --git a/gdb/symtab.c b/gdb/symtab.c
index b792081..9447bd9 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -3771,6 +3771,7 @@ struct add_name_data
This adds a macro's name to the current completion list. */
static void
add_macro_name (const char *name, const struct macro_definition *ignore,
+ struct macro_source_file *ignore2, int ignore3,
void *user_data)
{
struct add_name_data *datum = (struct add_name_data *) user_data;
next prev parent reply other threads:[~2011-07-20 22:01 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-07-19 9:57 Matt Rice
2011-07-19 17:29 ` Tom Tromey
2011-07-20 2:06 ` Matt Rice
2011-07-20 14:14 ` Tom Tromey
2011-07-20 17:24 ` Eli Zaretskii
2011-07-21 6:47 ` Matt Rice [this message]
2011-07-21 10:22 ` Eli Zaretskii
2011-07-21 14:07 ` Tom Tromey
2011-07-21 15:56 ` Matt Rice
2011-07-21 16:13 ` Pedro Alves
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CACTLOFpFhXnxcWKLp2CHNn8mfp2_tNa=6GGdJ2Jmyzo8mS+d6w@mail.gmail.com' \
--to=ratmice@gmail.com \
--cc=eliz@gnu.org \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox