public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Jan Kratochvil <jan.kratochvil@redhat.com>
To: gdb-patches@sourceware.org
Cc: Phil Muldoon <pmuldoon@redhat.com>
Subject: [PATCH 1/4] Code cleanup: Make parts of print_command_1 public
Date: Thu, 26 Mar 2015 20:57:00 -0000	[thread overview]
Message-ID: <20150326205727.28223.54648.stgit@host1.jankratochvil.net> (raw)

The later 'compile print' command should share its behavior with the existing
'print' command.  Make the needed existing parts of print_command_1 public.

gdb/ChangeLog
2015-03-26  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* printcmd.c (print_command_parse_format, print_value): New functions
	from ...
	(print_command_1): ... here.  Call them.
	* valprint.h (struct format_data, print_command_parse_format)
	(print_value): New declarations.
---
 gdb/printcmd.c |   89 +++++++++++++++++++++++++++++++++-----------------------
 gdb/valprint.h |    5 +++
 2 files changed, 58 insertions(+), 36 deletions(-)

diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index a1451f8..4efdf1b 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -939,6 +939,56 @@ validate_format (struct format_data fmt, const char *cmdname)
 	   fmt.format, cmdname);
 }
 
+/* Parse print command format string and update *EXPP, return it allocated,
+   caller has to call xfree for it.  Return NULL if no format string has been
+   found.  CMDNAME should name the current command.  */
+
+struct format_data *
+print_command_parse_format (const char **expp, const char *cmdname)
+{
+  struct format_data *fmtp;
+  const char *exp = *expp;
+  struct cleanup *cleanup;
+
+  if (exp == NULL || *exp != '/')
+    return NULL;
+  exp++;
+
+  fmtp = xmalloc (sizeof (*fmtp));
+  cleanup = make_cleanup (xfree, fmtp);
+  *fmtp = decode_format (&exp, last_format, 0);
+  validate_format (*fmtp, cmdname);
+  last_format = fmtp->format;
+
+  discard_cleanups (cleanup);
+  *expp = exp;
+  return fmtp;
+}
+
+
+/* Print VAL to console, including recording it to the history.  */
+
+void
+print_value (struct value *val, const struct format_data *fmtp)
+{
+  struct value_print_options opts;
+  int histindex = record_latest_value (val);
+
+  annotate_value_history_begin (histindex, value_type (val));
+
+  printf_filtered ("$%d = ", histindex);
+
+  annotate_value_history_value ();
+
+  get_formatted_print_options (&opts, (fmtp == NULL ? 0 : fmtp->format));
+  opts.raw = (fmtp == NULL ? 0 : fmtp->raw);
+
+  print_formatted (val, (fmtp == NULL ? 0 : fmtp->size), &opts, gdb_stdout);
+  printf_filtered ("\n");
+
+  annotate_value_history_end ();
+}
+
 /* Evaluate string EXP as an expression in the current language and
    print the resulting value.  EXP may contain a format specifier as the
    first argument ("/x myvar" for example, to print myvar in hex).  */
@@ -946,25 +996,9 @@ validate_format (struct format_data fmt, const char *cmdname)
 static void
 print_command_1 (const char *exp, int voidprint)
 {
-  struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
-  char format = 0;
   struct value *val;
-  struct format_data fmt;
-
-  if (exp && *exp == '/')
-    {
-      exp++;
-      fmt = decode_format (&exp, last_format, 0);
-      validate_format (fmt, "print");
-      last_format = format = fmt.format;
-    }
-  else
-    {
-      fmt.count = 1;
-      fmt.format = 0;
-      fmt.size = 0;
-      fmt.raw = 0;
-    }
+  struct format_data *fmtp = print_command_parse_format (&exp, "print");
+  struct cleanup *old_chain = make_cleanup (xfree, fmtp);
 
   if (exp && *exp)
     {
@@ -979,24 +1013,7 @@ print_command_1 (const char *exp, int voidprint)
 
   if (voidprint || (val && value_type (val) &&
 		    TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
-    {
-      struct value_print_options opts;
-      int histindex = record_latest_value (val);
-
-      annotate_value_history_begin (histindex, value_type (val));
-
-      printf_filtered ("$%d = ", histindex);
-
-      annotate_value_history_value ();
-
-      get_formatted_print_options (&opts, format);
-      opts.raw = fmt.raw;
-
-      print_formatted (val, fmt.size, &opts, gdb_stdout);
-      printf_filtered ("\n");
-
-      annotate_value_history_end ();
-    }
+    print_value (val, fmtp);
 
   do_cleanups (old_chain);
 }
diff --git a/gdb/valprint.h b/gdb/valprint.h
index e3d0137..3ab531f 100644
--- a/gdb/valprint.h
+++ b/gdb/valprint.h
@@ -217,4 +217,9 @@ extern void output_command_const (const char *args, int from_tty);
 
 extern int val_print_scalar_type_p (struct type *type);
 
+struct format_data;
+extern struct format_data *print_command_parse_format (const char **expp,
+						       const char *cmdname);
+extern void print_value (struct value *val, const struct format_data *fmtp);
+
 #endif

             reply	other threads:[~2015-03-26 20:57 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-26 20:57 Jan Kratochvil [this message]
2015-03-26 20:57 ` [PATCH 2/4] compile: Add new field scope_data Jan Kratochvil
2015-04-06 17:28   ` obsolete: " Jan Kratochvil
2015-03-26 20:57 ` [PATCH 3/4] compile: Constify some parameters Jan Kratochvil
2015-04-06 17:28   ` obsolete: " Jan Kratochvil
2015-03-26 20:58 ` [PATCH 4/4] compile: New 'compile print' Jan Kratochvil
2015-03-26 20:59   ` Jan Kratochvil
2015-03-27  7:18   ` Eli Zaretskii
2015-03-27  7:33     ` Jan Kratochvil
2015-03-27  7:41       ` Eli Zaretskii
2015-03-27  7:56         ` Phil Muldoon
2015-03-27  9:08           ` Eli Zaretskii
2015-03-27  9:10           ` Eli Zaretskii
2015-03-27  9:16             ` Jan Kratochvil
2015-03-27  9:56               ` Eli Zaretskii
2015-03-27 10:11                 ` Jan Kratochvil
2015-03-27 10:20                 ` Phil Muldoon
2015-03-27 13:09                   ` Eli Zaretskii
2015-03-27 10:24                 ` Jan Kratochvil
2015-03-27 13:12                   ` Eli Zaretskii
2015-04-05 17:01   ` cancel: " Jan Kratochvil
2015-04-06 17:29   ` obsolete: " Jan Kratochvil
2015-03-27 20:06 ` [PATCH 1/4] v2: Code cleanup: Make parts of print_command_1 public Jan Kratochvil
2015-04-06 17:27   ` obsolete: " Jan Kratochvil

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=20150326205727.28223.54648.stgit@host1.jankratochvil.net \
    --to=jan.kratochvil@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=pmuldoon@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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).