From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31352 invoked by alias); 21 Jun 2010 15:05:02 -0000 Received: (qmail 31092 invoked by uid 22791); 21 Jun 2010 15:04:59 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE X-Spam-Check-By: sourceware.org Received: from mail-pv0-f169.google.com (HELO mail-pv0-f169.google.com) (74.125.83.169) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 21 Jun 2010 15:04:53 +0000 Received: by pvg16 with SMTP id 16so3298372pvg.0 for ; Mon, 21 Jun 2010 08:04:51 -0700 (PDT) Received: by 10.142.8.13 with SMTP id 13mr3558342wfh.210.1277132690298; Mon, 21 Jun 2010 08:04:50 -0700 (PDT) MIME-Version: 1.0 Received: by 10.143.32.8 with HTTP; Mon, 21 Jun 2010 08:04:30 -0700 (PDT) In-Reply-To: <20100110140029.GF2007@adacore.com> References: <4B3BEDCC.9040103@earthlink.net> <20100106075757.GF24777@adacore.com> <20100109105557.GA2007@adacore.com> <20100110054328.GD2007@adacore.com> <20100110140029.GF2007@adacore.com> From: Hui Zhu Date: Mon, 21 Jun 2010 15:05:00 -0000 Message-ID: Subject: Re: [RFC] Let "gcore" command accept a suffix argument To: Joel Brobecker Cc: Stan Shebs , Tom Tromey , Stan Shebs , Michael Snyder , gdb-patches ml , Eli Zaretskii Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2010-06/txt/msg00455.txt.bz2 On Sun, Jan 10, 2010 at 22:00, Joel Brobecker wrote: >> But I think we have know the type of the val that we want output, the >> string we want it as string, the number we want it to be number. >> Are you sure we need point out the type of val to be output? >> >> And this idea is out of my ability. =A0Sorry about it. > > If you could have a look at how the "printf" command is implemented, > you might be surprised as to how easy this task might be. =A0For someone > who tackled the really complex project of grafting process record onto > GDB, I sincerely think that this should nearly be a no-brainer for you: > > =A01. Extract out the entire contents of the printf_command implementation > =A0 =A0 into a separate function. Let's call it ui_printf: > > =A0 =A0 =A0 void > =A0 =A0 =A0 ui_printf (char *args, ui_file *stream) > > =A02. Adjust the body so that all the printf/puts, etc, basically anything > =A0 =A0 printing on stdout, are replace by equivalent calls that print in > =A0 =A0 the given ui_file *stream; > > =A03. Implement the body of printf_command by simply calling this new > =A0 =A0 function with gdb_stdout as the ui_file; > > =A0 =A0 =A0 printf_command (char *args, int from_tty) > =A0 =A0 =A0 { > =A0 =A0 =A0 =A0 ui_printf (args, gdb_stout); > =A0 =A0 =A0 } > > =A04. Implement the eval function roughly like so: > > =A0 =A0 =A0 =A0eval_command (char *args, int from_tty) > =A0 =A0 =A0 =A0{ > =A0 =A0 =A0 =A0 =A0struct ui_file *ui_out =3D mem_fileopen (); > =A0 =A0 =A0 =A0 =A0char *expanded; > =A0 =A0 =A0 =A0 =A0struct cleanup *old_chain; > > =A0 =A0 =A0 =A0 =A0ui_printf (args, ui_out); =A0// That's the new functio= n extracted > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 /= / from printf_command, which prints > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 /= / in a ui_file instead of stdout. > =A0 =A0 =A0 =A0 =A0expanded =3D ui_file_xstrdup (ui_out, NULL); > =A0 =A0 =A0 =A0 =A0old_chain =3D make_cleanup (xfree, expanded); > =A0 =A0 =A0 =A0 =A0execute_command (expanded, from_tty); > =A0 =A0 =A0 =A0 =A0do_cleanups (old_chain); > =A0 =A0 =A0 =A0} > > -- > Joel > Hi Joel, I make a new patch according to your mail. Please help me review it. Thanks, Hui 2010-06-21 Hui Zhu * printcmd.c (ui_printf_maybe_filtered): New function. (printf_command): Call ui_printf_maybe_filtered. (_initialize_printcmd): New command "eval". --- printcmd.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++----= ----- 1 file changed, 82 insertions(+), 14 deletions(-) --- a/printcmd.c +++ b/printcmd.c @@ -1957,7 +1957,7 @@ print_variable_and_value (const char *na } static void -printf_command (char *arg, int from_tty) +ui_printf_maybe_filtered (char *arg, struct ui_file *stream, int filter) { char *f =3D NULL; char *s =3D arg; @@ -2340,7 +2340,10 @@ printf_command (char *arg, int from_tty) read_memory (tem, str, j); str[j] =3D 0; - printf_filtered (current_substring, (char *) str); + if (filter) + fprintf_filtered (stream, current_substring, (char *) str); + else + fprintf_unfiltered (stream, current_substring, (char *) st= r); } break; case wide_string_arg: @@ -2384,7 +2387,12 @@ printf_command (char *arg, int from_tty) &output, translit_char); obstack_grow_str0 (&output, ""); - printf_filtered (current_substring, obstack_base (&output)); + if (filter) + fprintf_filtered (stream, current_substring, + obstack_base (&output)); + else + fprintf_unfiltered (stream, current_substring, + obstack_base (&output)); do_cleanups (inner_cleanup); } break; @@ -2416,7 +2424,12 @@ printf_command (char *arg, int from_tty) &output, translit_char); obstack_grow_str0 (&output, ""); - printf_filtered (current_substring, obstack_base (&output)); + if (filter) + fprintf_filtered (stream, current_substring, + obstack_base (&output)); + else + fprintf_unfiltered (stream, current_substring, + obstack_base (&output)); do_cleanups (inner_cleanup); } break; @@ -2433,7 +2446,10 @@ printf_command (char *arg, int from_tty) if (inv) error (_("Invalid floating value found in program.")); - printf_filtered (current_substring, (double) val); + if (filter) + fprintf_filtered (stream, current_substring, (double) val); + else + fprintf_unfiltered (stream, current_substring, (double) va= l); break; } case long_double_arg: @@ -2450,7 +2466,12 @@ printf_command (char *arg, int from_tty) if (inv) error (_("Invalid floating value found in program.")); - printf_filtered (current_substring, (long double) val); + if (filter) + fprintf_filtered (stream, current_substring, + (long double) val); + else + fprintf_unfiltered (stream, current_substring, + (long double) val); break; } #else @@ -2461,7 +2482,10 @@ printf_command (char *arg, int from_tty) { long long val =3D value_as_long (val_args[i]); - printf_filtered (current_substring, val); + if (filter) + fprintf_filtered (stream, current_substring, val); + else + fprintf_unfiltered (stream, current_substring, val); break; } #else @@ -2471,14 +2495,20 @@ printf_command (char *arg, int from_tty) { int val =3D value_as_long (val_args[i]); - printf_filtered (current_substring, val); + if (filter) + fprintf_filtered (stream, current_substring, val); + else + fprintf_unfiltered (stream, current_substring, val); break; } case long_arg: { long val =3D value_as_long (val_args[i]); - printf_filtered (current_substring, val); + if (filter) + fprintf_filtered (stream, current_substring, val); + else + fprintf_unfiltered (stream, current_substring, val); break; } @@ -2490,7 +2520,10 @@ printf_command (char *arg, int from_tty) #if defined (PRINTF_HAS_DECFLOAT) /* If we have native support for Decimal floating printing, handle it here. */ - printf_filtered (current_substring, param_ptr); + if (filter) + fprintf_filtered (stream, current_substring, param_ptr); + else + fprintf_unfiltered (stream, current_substring, param_ptr); #else /* As a workaround until vasprintf has native support for DFP @@ -2579,7 +2612,10 @@ printf_command (char *arg, int from_tty) decimal_to_string (dfp_ptr, dfp_len, byte_order, decstr); /* Print the DFP value. */ - printf_filtered (current_substring, decstr); + if (filter) + fprintf_filtered (stream, current_substring, decstr); + else + fprintf_unfiltered (stream, current_substring, decstr); break; #endif @@ -2634,13 +2670,19 @@ printf_command (char *arg, int from_tty) *fmt_p++ =3D 'l'; *fmt_p++ =3D 'x'; *fmt_p++ =3D '\0'; - printf_filtered (fmt, val); + if (filter) + fprintf_filtered (stream, fmt, val); + else + fprintf_unfiltered (stream, fmt, val); } else { *fmt_p++ =3D 's'; *fmt_p++ =3D '\0'; - printf_filtered (fmt, "(nil)"); + if (filter) + fprintf_filtered (stream, fmt, "(nil)"); + else + fprintf_unfiltered (stream, fmt, "(nil)"); } break; @@ -2658,11 +2700,34 @@ printf_command (char *arg, int from_tty) puts_filtered here. Also, we pass a dummy argument because some platforms have modified GCC to include -Wformat-security by default, which will warn here if there is no argument. */ - printf_filtered (last_arg, 0); + if (filter) + fprintf_filtered (stream, last_arg, 0); + else + fprintf_unfiltered (stream, last_arg, 0); } do_cleanups (old_cleanups); } +static void +printf_command (char *arg, int from_tty) +{ + ui_printf_maybe_filtered (arg, gdb_stdout, 1); +} + +static void +eval_command (char *arg, int from_tty) +{ + struct ui_file *ui_out =3D mem_fileopen (); + char *expanded; + struct cleanup *old_chain; + + ui_printf_maybe_filtered (arg, ui_out, 0); + expanded =3D ui_file_xstrdup (ui_out, NULL); + old_chain =3D make_cleanup (xfree, expanded); + execute_command (expanded, from_tty); + do_cleanups (old_chain); +} + void _initialize_printcmd (void) { @@ -2826,4 +2891,7 @@ Show printing of source filename and lin NULL, show_print_symbol_filename, &setprintlist, &showprintlist); + + add_com ("eval", no_class, eval_command, _("\ +Call command with variable.")); }