Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Hui Zhu <teawater@gmail.com>
To: Joel Brobecker <brobecker@adacore.com>
Cc: Stan Shebs <stanshebs@earthlink.net>,
	Tom Tromey <tromey@redhat.com>,
		Stan Shebs <stan@codesourcery.com>,
	Michael Snyder <msnyder@vmware.com>,
		gdb-patches ml <gdb-patches@sourceware.org>,
	Eli Zaretskii <eliz@gnu.org>
Subject: Re: [RFC] Let "gcore" command accept a suffix argument
Date: Mon, 21 Jun 2010 15:05:00 -0000	[thread overview]
Message-ID: <AANLkTinVPdwg6HuAp08wknw8DYe80LcR33tbEaRkMDXz@mail.gmail.com> (raw)
In-Reply-To: <20100110140029.GF2007@adacore.com>

On Sun, Jan 10, 2010 at 22:00, Joel Brobecker <brobecker@adacore.com> 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.  Sorry 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.  For 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:
>
>  1. Extract out the entire contents of the printf_command implementation
>     into a separate function. Let's call it ui_printf:
>
>       void
>       ui_printf (char *args, ui_file *stream)
>
>  2. Adjust the body so that all the printf/puts, etc, basically anything
>     printing on stdout, are replace by equivalent calls that print in
>     the given ui_file *stream;
>
>  3. Implement the body of printf_command by simply calling this new
>     function with gdb_stdout as the ui_file;
>
>       printf_command (char *args, int from_tty)
>       {
>         ui_printf (args, gdb_stout);
>       }
>
>  4. Implement the eval function roughly like so:
>
>        eval_command (char *args, int from_tty)
>        {
>          struct ui_file *ui_out = mem_fileopen ();
>          char *expanded;
>          struct cleanup *old_chain;
>
>          ui_printf (args, ui_out);  // That's the new function extracted
>                                     // from printf_command, which prints
>                                     // in a ui_file instead of stdout.
>          expanded = ui_file_xstrdup (ui_out, NULL);
>          old_chain = make_cleanup (xfree, expanded);
>          execute_command (expanded, from_tty);
>          do_cleanups (old_chain);
>        }
>
> --
> Joel
>

Hi Joel,

I make a new patch according to your mail.
Please help me review it.

Thanks,
Hui

2010-06-21  Hui Zhu  <teawater@gmail.com>

       * 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 = NULL;
   char *s = arg;
@@ -2340,7 +2340,10 @@ printf_command (char *arg, int from_tty)
 		read_memory (tem, str, j);
 	      str[j] = 0;

-	      printf_filtered (current_substring, (char *) str);
+              if (filter)
+	        fprintf_filtered (stream, current_substring, (char *) str);
+              else
+                fprintf_unfiltered (stream, current_substring, (char *) str);
 	    }
 	    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) val);
 	      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 = 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 = 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 = 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++ = 'l';
 		  *fmt_p++ = 'x';
 		  *fmt_p++ = '\0';
-		  printf_filtered (fmt, val);
+                  if (filter)
+		    fprintf_filtered (stream, fmt, val);
+                  else
+                    fprintf_unfiltered (stream, fmt, val);
 		}
 	      else
 		{
 		  *fmt_p++ = 's';
 		  *fmt_p++ = '\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 = mem_fileopen ();
+  char *expanded;
+  struct cleanup *old_chain;
+
+  ui_printf_maybe_filtered (arg, ui_out, 0);
+  expanded = ui_file_xstrdup (ui_out, NULL);
+  old_chain = 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."));
 }


  parent reply	other threads:[~2010-06-21 15:05 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <4B11DA3C.3000203@vmware.com>
     [not found] ` <daef60380911300437o4c616eb2v5ad7bfe99bd3c5e9@mail.gmail.com>
     [not found]   ` <20091130162246.GE4034@adacore.com>
     [not found]     ` <4B141157.3070709@vmware.com>
     [not found]       ` <20091130185341.GI4034@adacore.com>
     [not found]         ` <4B141469.5030402@vmware.com>
     [not found]           ` <20091130190619.GJ4034@adacore.com>
     [not found]             ` <4B1428F0.7090608@vmware.com>
     [not found]               ` <daef60380912091827u6ff7127bp1d03998a40932914@mail.gmail.com>
2009-12-10  2:32                 ` Hui Zhu
2009-12-10 17:08                   ` Tom Tromey
2009-12-11 10:06                     ` Joel Brobecker
2009-12-11 18:25                       ` Tom Tromey
2009-12-12  8:33                         ` Hui Zhu
2009-12-13  4:05                           ` Hui Zhu
2009-12-14 17:09                           ` Tom Tromey
2009-12-15  1:57                             ` Hui Zhu
2009-12-15 19:21                               ` Tom Tromey
2009-12-16  3:58                                 ` Hui Zhu
2009-12-16 15:49                                   ` Stan Shebs
2009-12-17  2:45                                     ` Hui Zhu
2009-12-17  4:26                                       ` Joel Brobecker
2009-12-17  4:29                                         ` Michael Snyder
2009-12-17  9:32                                           ` Andreas Schwab
2009-12-21 20:15                                     ` Tom Tromey
2009-12-31  0:18                                       ` Stan Shebs
2010-01-04 14:42                                         ` Hui Zhu
2010-01-06  6:57                                           ` Hui Zhu
2010-01-06  7:58                                             ` Joel Brobecker
2010-01-09  9:55                                               ` Hui Zhu
2010-01-09 10:56                                                 ` Joel Brobecker
2010-01-09 15:18                                                   ` Hui Zhu
2010-01-10  5:43                                                     ` Joel Brobecker
2010-01-10 13:30                                                       ` Hui Zhu
2010-01-10 14:00                                                         ` Joel Brobecker
2010-01-10 14:16                                                           ` Hui Zhu
2010-06-21 15:05                                                           ` Hui Zhu [this message]
2010-06-21 16:57                                                             ` Joel Brobecker
2010-06-21 21:34                                                               ` Joel Brobecker
2010-06-21 21:40                                                             ` Tom Tromey
2010-06-22  7:29                                                               ` Hui Zhu
2010-06-22  9:50                                                                 ` Pedro Alves
2010-06-22 15:21                                                                 ` Joel Brobecker
2010-06-22 18:05                                                                 ` Eli Zaretskii
2010-06-23  2:03                                                                   ` Hui Zhu
2010-06-23 16:59                                                                     ` Joel Brobecker
2010-06-24  6:13                                                                       ` Hui Zhu
2010-01-06 10:13                                             ` Eli Zaretskii

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=AANLkTinVPdwg6HuAp08wknw8DYe80LcR33tbEaRkMDXz@mail.gmail.com \
    --to=teawater@gmail.com \
    --cc=brobecker@adacore.com \
    --cc=eliz@gnu.org \
    --cc=gdb-patches@sourceware.org \
    --cc=msnyder@vmware.com \
    --cc=stan@codesourcery.com \
    --cc=stanshebs@earthlink.net \
    --cc=tromey@redhat.com \
    /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