[RFC] Re: Profiling gdb?
Daniel Jacobowitz
drow@mvista.com
Sun Jan 5 22:27:00 GMT 2003
On Sun, Jan 20, 2002 at 10:26:35PM -0500, Andrew Cagney wrote:
> >What was the final status of the profiling patch? It's a feature I'd
> >really like to see in (having it in means fewer local changes in my
> >tree when I'm testing the speed of something!).
>
>
> Interesting timing, I was just looking over the same thing.
And then neither of us did anything about it for a while. Cue music, a
year later.
> My take is:
>
>
> Part 1: ``(gdb) maint set moncontrol {on,off}''
>
> A home for the command doco (gdb.texinfo) has been created.
>
> The necessary framework (maint set) has been added along with the
> function to correctly implement a boolean command. Note:
> http://sources.redhat.com/gdb/ari/#var_boolean
>
> The command should always be present but the call should be wrapped in a
> check that the function exits. The function moncontrol() is in both the
> libc and libc_p libraries (at least on correctly implemented systems :-)
> so this is correct.
Check.
> Part 2: Add the --<what-was-that-profile-option>
>
> Which adds the compile/link -gp flag.
>
> Eli's comment that you don't need to link with -gp is correct.
>
> Perhaphs the <what-was-that-profile-option> should let the user control
> the compile and link flags.
>
> Don't remember where the doco for this goes.
Check. Without the extra bit of configurability for now.
> Part 3: (new) ``(gdb) maint set monstartup <lowpc> <highpc>
>
> Can anyone think why we shouldn't make this visible. Of course the user
> will need to somehow obtain the magic addresses from somewhere.
>
> Same as moncontrol().
Perhaps some other time. I do _use_ monstartup, however.
This patch should address all comments from the last three times this
came up. I chose to use monstartup/_mcleanup instead of using
moncontrol; when someone wants to use this on a system without those
functions, _then_ we can decide how to handle it. That's just my
opinion though. What I've done should work for at least GNU/Linux and
FreeBSD, and that's enough to be useful. It only comes in if you
configure it on, anyway.
It works like a charm; absolutely beautifully. Thoughts, all?
--
Daniel Jacobowitz
MontaVista Software Debian GNU/Linux Developer
2003-01-05 Daniel Jacobowitz <drow@mvista.com>
Original patch by Tom Tromey <tromey@cygnus.com>.
* Makefile.in (PROFILE_CFLAGS): Substitute from configure.
(INTERNAL_LDFLAGS): Don't include PROFILE_CFLAGS.
* NEWS: Mention profiling.
* configure.in (--enable-gdbtk): Fix typo.
(--enable-profiling): New. Set PROFILE_CFLAGS. Define
ENABLE_PROFILING.
* maint.c (maintenance_set_profile_cmd): Remove NOTYET.
Fill in function.
(profiling_state): New variable.
(mcleanup_wrapper): New function.
(_initialize_maint): Remove NOTYET, fix call to
add_setshow_boolean_cmd for "maint set profile".
2003-01-05 Daniel Jacobowitz <drow@mvista.com>
* gdb.texinfo (Maintenance Commands): Add "maint set profile"
and "maint show profile".
Index: Makefile.in
===================================================================
RCS file: /cvs/src/src/gdb/Makefile.in,v
retrieving revision 1.309
diff -u -p -r1.309 Makefile.in
--- Makefile.in 5 Jan 2003 01:39:54 -0000 1.309
+++ Makefile.in 5 Jan 2003 22:13:53 -0000
@@ -318,7 +318,8 @@ GDB_CFLAGS = -I. -I$(srcdir) -I$(srcdir)
# M{H,T}_CFLAGS, if defined, have host- and target-dependent CFLAGS
# from the config directory.
GLOBAL_CFLAGS = $(MT_CFLAGS) $(MH_CFLAGS)
-#PROFILE_CFLAGS = -pg
+
+PROFILE_CFLAGS = @PROFILE_CFLAGS@
# CFLAGS is specifically reserved for setting from the command line
# when running make. I.E. "make CFLAGS=-Wmissing-prototypes".
@@ -345,7 +346,8 @@ LDFLAGS = @LDFLAGS@
# Profiling options need to go here to work.
# I think it's perfectly reasonable for a user to set -pg in CFLAGS
# and have it work; that's why CFLAGS is here.
-INTERNAL_LDFLAGS = $(CFLAGS) $(GLOBAL_CFLAGS) $(PROFILE_CFLAGS) $(MH_LDFLAGS) $(LDFLAGS) $(CONFIG_LDFLAGS)
+# PROFILE_CFLAGS is _not_ included, however, because we use monstartup.
+INTERNAL_LDFLAGS = $(CFLAGS) $(GLOBAL_CFLAGS) $(MH_LDFLAGS) $(LDFLAGS) $(CONFIG_LDFLAGS)
# If your system is missing alloca(), or, more likely, it's there but
# it doesn't work, then refer to libiberty.
Index: NEWS
===================================================================
RCS file: /cvs/src/src/gdb/NEWS,v
retrieving revision 1.97
diff -u -p -r1.97 NEWS
--- NEWS 2 Jan 2003 14:27:26 -0000 1.97
+++ NEWS 5 Jan 2003 22:13:54 -0000
@@ -3,6 +3,13 @@
*** Changes since GDB 5.3:
+* Profiling for gdb
+
+If gdb is configured with `--enable-profiling', then gdb is built with
+`-pg' and the `maint set profile' command is enabled. This command can
+be used to enable or disable profiling, making it possible to profile a
+single command or set of commands.
+
* Default MI syntax changed to "mi2".
The default MI (machine interface) syntax, enabled by the command line
Index: configure.in
===================================================================
RCS file: /cvs/src/src/gdb/configure.in,v
retrieving revision 1.116
diff -u -p -r1.116 configure.in
--- configure.in 4 Jan 2003 23:47:12 -0000 1.116
+++ configure.in 5 Jan 2003 22:13:54 -0000
@@ -164,7 +164,7 @@ fi
# Enable gdbtk.
AC_ARG_ENABLE(gdbtk,
-[ --enable-gtk enable gdbtk graphical user interface (GUI)],
+[ --enable-gdbtk enable gdbtk graphical user interface (GUI)],
[case $enableval in
yes | no)
;;
@@ -183,6 +183,33 @@ case $host_os in
enable_gdbtk=no ;;
esac
+# Profiling support.
+AC_ARG_ENABLE(profiling,
+[ --enable-profiling enable profiling of GDB],
+ [case $enableval in
+ yes | no)
+ ;;
+ *)
+ AC_MSG_ERROR([bad value $enableval for --enable-profile]) ;;
+ esac],
+ [enable_profiling=no])
+
+if test "$enable_profiling" = yes ; then
+ PROFILE_CFLAGS=-pg
+ OLD_CFLAGS="$CFLAGS"
+ CFLAGS="$CFLAGS $PROFILE_CFLAGS"
+
+ AC_CHECK_FUNC(monstartup, [],
+ AC_MSG_ERROR(monstartup necessary to use --enable-profiling.))
+
+ AC_CHECK_FUNC(_mcleanup, [],
+ AC_MSG_ERROR(_mcleanup necessary to use --enable-profiling.))
+
+ CFLAGS="$OLD_CFLAGS"
+ AC_DEFINE_UNQUOTED(ENABLE_PROFILING, 1,
+ [Define if profiling support should be enabled.])
+fi
+
# --------------------- #
# Checks for programs. #
# --------------------- #
@@ -1132,6 +1162,7 @@ AC_SUBST(IGNORE_SIM)
AC_SUBST(IGNORE_SIM_OBS)
AC_SUBST(ENABLE_CFLAGS)
+AC_SUBST(PROFILE_CFLAGS)
AC_SUBST(CONFIG_OBS)
AC_SUBST(CONFIG_LIB_OBS)
Index: maint.c
===================================================================
RCS file: /cvs/src/src/gdb/maint.c,v
retrieving revision 1.30
diff -u -p -r1.30 maint.c
--- maint.c 24 Dec 2002 03:57:58 -0000 1.30
+++ maint.c 5 Jan 2003 22:13:54 -0000
@@ -639,18 +639,57 @@ maintenance_show_cmd (char *args, int fr
cmd_show_list (maintenance_show_cmdlist, from_tty, "");
}
-#ifdef NOTYET
/* Profiling support. */
static int maintenance_profile_p;
+static int profiling_state;
+
+static void
+mcleanup_wrapper (void)
+{
+ extern void _mcleanup (void);
+
+ if (profiling_state)
+ _mcleanup ();
+}
static void
maintenance_set_profile_cmd (char *args, int from_tty, struct cmd_list_element *c)
{
+#ifdef ENABLE_PROFILING
+ if (maintenance_profile_p == profiling_state)
+ return;
+
+ profiling_state = maintenance_profile_p;
+
+ if (maintenance_profile_p)
+ {
+ static int profiling_initialized;
+
+ extern void monstartup (long, long);
+ extern char _etext;
+ extern int main();
+
+ if (!profiling_initialized)
+ {
+ atexit (mcleanup_wrapper);
+ profiling_initialized = 1;
+ }
+
+ /* "main" is now always the first function in the text segment, so use
+ its address for monstartup. */
+ monstartup ((long) &main, (long) &_etext);
+ }
+ else
+ {
+ extern void _mcleanup (void);
+ _mcleanup ();
+ }
+#else
maintenance_profile_p = 0;
warning ("\"maintenance set profile\" command not supported.\n");
-}
#endif
+}
void
_initialize_maint_cmds (void)
@@ -807,16 +846,12 @@ passes without a response from the targe
&showlist);
-#ifdef NOTYET
- /* FIXME: cagney/2002-06-15: A patch implementing profiling is
- pending, this just sets up the framework. */
- tmpcmd = add_setshow_boolean_cmd ("profile", class_maintenance,
- var_boolean, &maintenance_profile_p, "\
-Set internal profiling.\n\
-When enabled GDB is profiled.", "\
-Show internal profiling.\n",
- maintenance_set_profile_cmd, NULL,
- &maintenance_set_cmdlist,
- &maintenance_show_cmdlist);
-#endif
+ add_setshow_boolean_cmd ("profile", class_maintenance,
+ &maintenance_profile_p,
+ "Set internal profiling.\n"
+ "When enabled GDB is profiled.",
+ "Show internal profiling.\n",
+ maintenance_set_profile_cmd, NULL,
+ &maintenance_set_cmdlist,
+ &maintenance_show_cmdlist);
}
Index: doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.136
diff -u -p -r1.136 gdb.texinfo
--- doc/gdb.texinfo 5 Jan 2003 04:34:39 -0000 1.136
+++ doc/gdb.texinfo 5 Jan 2003 22:13:59 -0000
@@ -14385,6 +14385,20 @@ cooked-registers} includes the (cooked)
Takes an optional file parameter.
+@kindex maint set profile
+@kindex maint show profile
+@item maint set profile
+@itemx maint show profile
+Control profiling of @value{GDBN}. These commands are only available if
+@samp{--enable-profiling} is used to build @value{GDBN}.
+
+Configuring with @samp{--enable-profiling} arranges for @value{GDBN} to
+be compiled with the @samp{-pg} compiler option. Profiling will be disabled
+until you use the @samp{maint set profile} command to enable it. When
+profiling is turned on, @value{GDBN} will overwrite the profiling log file
+(often called @file{gmon.out}). If you have a record of important profiling
+data in a @file{gmon.out} file, be sure to move it to a safe location.
+
@end table
More information about the Gdb-patches
mailing list