This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: New scope checking patch


>>>>> "Rob" == Rob Quill <rob.quill@gmail.com> writes:

Rob> Where can I find out about internal functions?

I've appended the patch.  It adds a new sort of value, which is an
internal function.  These are just like ordinary functions in an
expression but they run inside gdb -- not the inferior.  The user sees
them as convenience variables of "internal function" type.

This patch builds but, because it is divorced from the code that uses
it, I did not test it.  So, if you try it, let me know how it fails :)

BTW -- I noticed that with your patch, an expression using $in_scope
will not print properly.  That is because in_scope is turned into a
constant in the expression structure.


Thiago -- FYI, I took the patch from the python-patches branch,
deleted stuff relying on things not yet in cvs (command destructors
and Python), and then wrote comments and a ChangeLog entry.  You may
want the latter :)

Tom

b/gdb/ChangeLog:
2008-07-29  Tom Tromey  <tromey@redhat.com>

	* value.h (lookup_only_internalvar, create_internalvar,
	lookup_internalvar): Update.
	(internal_function_fn, add_internal_function,
	call_internal_function, value_internal_function_name): Declare.
	* value.c (struct internal_function): New struct.
	(functionlist): New global.
	(internal_fn_type): Likewise.
	(lookup_only_internalvar): Made argument const.
	(create_internalvar): Likewise.
	(lookup_internalvar): Likewise.
	(value_create_internal_function): New function.
	(value_internal_function_name): Likewise.
	(call_internal_function): Likewise.
	(function_command): Likewise.
	(add_internal_function): Likewise.
	(_initialize_values): Add the "function" command.  Initialize
	internal_fn_type.
	* valprint.c (value_check_printable): Handle
	TYPE_CODE_INTERNAL_FUNCTION.
	* gdbtypes.h (enum type_code) <TYPE_CODE_INTERNAL_FUNCTION>: New
	constant.
	* eval.c: (evaluate_subexp_standard) <OP_FUNCALL>: Handle
	TYPE_CODE_INTERNAL_FUNCTION.

projecttype:gdb
email:tromey@gcc.gnu.org
revision:edd41e881c9f658211f1ebc08126a0222488e3a4
configure:
make:
check:

diff --git a/gdb/eval.c b/gdb/eval.c
index bbd7539..7ee910f 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -1293,6 +1293,9 @@ evaluate_subexp_standard (struct type *expect_type,
 	  else
 	    error (_("Expression of type other than \"Function returning ...\" used as function"));
 	}
+      if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_INTERNAL_FUNCTION)
+	return call_internal_function (argvec[0], nargs, argvec + 1);
+
       return call_function_by_hand (argvec[0], nargs, argvec + 1);
       /* pai: FIXME save value from call_function_by_hand, then adjust pc by adjust_fn_pc if +ve  */
 
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index 7ef7d67..365707b 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -133,7 +133,10 @@ enum type_code
 
     TYPE_CODE_NAMESPACE,	/* C++ namespace.  */
 
-    TYPE_CODE_DECFLOAT		/* Decimal floating point.  */
+    TYPE_CODE_DECFLOAT,		/* Decimal floating point.  */
+
+    /* Internal function type.  */
+    TYPE_CODE_INTERNAL_FUNCTION
   };
 
 /* For now allow source to use TYPE_CODE_CLASS for C++ classes, as an
diff --git a/gdb/valprint.c b/gdb/valprint.c
index 99c376f..617a3f4 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -259,6 +259,13 @@ value_check_printable (struct value *val, struct ui_file *stream)
       return 0;
     }
 
+  if (TYPE_CODE (value_type (val)) == TYPE_CODE_INTERNAL_FUNCTION)
+    {
+      fprintf_filtered (stream, _("<internal function %s>"),
+			value_internal_function_name (val));
+      return 0;
+    }
+
   return 1;
 }
 
diff --git a/gdb/value.c b/gdb/value.c
index b38bae0..93436e3 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -41,6 +41,24 @@
 
 void _initialize_values (void);
 
+/* Definition of a user function.  */
+struct internal_function
+{
+  /* The name of the function.  It is a bit odd to have this in the
+     function itself -- the user might use a differently-named
+     convenience variable to hold the function.  */
+  char *name;
+
+  /* The handler.  */
+  internal_function_fn handler;
+
+  /* User data for the handler.  */
+  void *cookie;
+};
+
+/* Command list holding internal functions.  */
+static struct cmd_list_element *functionlist;
+
 struct value
 {
   /* Type of value; either not an lval, or one of the various
@@ -203,6 +221,10 @@ struct value_history_chunk
 static struct value_history_chunk *value_history_chain;
 
 static int value_history_count;	/* Abs number of last entry stored */
+
+/* The type of internal functions.  */
+
+static struct type *internal_fn_type;
 
 /* List of all value objects currently allocated
    (except for those released by calls to release_value)
@@ -747,7 +769,7 @@ init_if_undefined_command (char* args, int from_tty)
    the return value is NULL.  */
 
 struct internalvar *
-lookup_only_internalvar (char *name)
+lookup_only_internalvar (const char *name)
 {
   struct internalvar *var;
 
@@ -763,7 +785,7 @@ lookup_only_internalvar (char *name)
    NAME should not normally include a dollar sign.  */
 
 struct internalvar *
-create_internalvar (char *name)
+create_internalvar (const char *name)
 {
   struct internalvar *var;
   var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
@@ -784,7 +806,7 @@ create_internalvar (char *name)
    one is created, with a void value.  */
 
 struct internalvar *
-lookup_internalvar (char *name)
+lookup_internalvar (const char *name)
 {
   struct internalvar *var;
 
@@ -888,6 +910,81 @@ internalvar_name (struct internalvar *var)
   return var->name;
 }
 
+/* Create a value which represents an internal function.
+   NAME is the name of the function.
+   HANDLER is a pointer to a function which is called when this
+   internal function is invoked during expression evaluation.
+   COOKIE is a pointer which is passed verbatim to HANDLER.  */
+static struct value *
+value_create_internal_function (const char *name,
+				internal_function_fn handler,
+				void *cookie)
+{
+  struct value *result = allocate_value (internal_fn_type);
+  gdb_byte *addr = value_contents_writeable (result);
+  struct internal_function **fnp = (struct internal_function **) addr;
+  /* The internal_function object is leaked here -- to make it truly
+     deletable, we would have to reference count it and add special
+     code to value_free and value_copy.  */
+  struct internal_function *ifn = XNEW (struct internal_function);
+  ifn->name = xstrdup (name);
+  ifn->handler = handler;
+  ifn->cookie = cookie;
+  *fnp = ifn;
+  return result;
+}
+
+/* Return the name of the value VAL, which is assumed to be of
+   "internal function" type.  The result is not copied and should not
+   be freed or modified.  */
+char *
+value_internal_function_name (struct value *val)
+{
+  gdb_byte *addr = value_contents_writeable (val);
+  struct internal_function *ifn = * (struct internal_function **) addr;
+  return ifn->name;
+}
+
+/* Call an internal function.  FUNC is a value which is assumed to be
+   of "internal function" type.  ARGC is the number of arguments to
+   pass to it, and ARGV is a vector holding the arguments.  */
+struct value *
+call_internal_function (struct value *func, int argc, struct value **argv)
+{
+  gdb_byte *addr = value_contents_writeable (func);
+  struct internal_function *ifn = * (struct internal_function **) addr;
+  return (*ifn->handler) (ifn->cookie, argc, argv);
+}
+
+/* The 'function' command.  This does nothing -- it is just a
+   placeholder to let "help function NAME" work.  This is also used as
+   the implementation of the sub-command that is created when
+   registering an internal function.  */
+static void
+function_command (char *command, int from_tty)
+{
+  /* Do nothing.  */
+}
+
+/* Add a new internal function.  NAME is the name of the function; DOC
+   is a documentation string describing the function.  HANDLER is
+   called when the function is invoked.  COOKIE is an arbitrary
+   pointer which is passed to HANDLER and is intended for "user
+   data".  */
+void
+add_internal_function (const char *name, const char *doc,
+		       internal_function_fn handler,
+		       void *cookie)
+{
+  struct internalvar *var = lookup_internalvar (name);
+  struct value *fnval = value_create_internal_function (name, handler, cookie);
+  release_value (fnval);
+  set_internalvar (var, fnval);
+
+  add_cmd (xstrdup (name), no_class, function_command, (char *) doc,
+	   &functionlist);
+}
+
 /* Update VALUE before discarding OBJFILE.  COPIED_TYPES is used to
    prevent cycles / duplicates.  */
 
@@ -1780,4 +1877,13 @@ init-if-undefined VARIABLE = EXPRESSION\n\
 Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
 exist or does not contain a value.  The EXPRESSION is not evaluated if the\n\
 VARIABLE is already initialized."));
+
+  add_prefix_cmd ("function", no_class, function_command, _("\
+Placeholder command for showing help on convenience functions."),
+		  &functionlist, "function ", 0, &cmdlist);
+
+  internal_fn_type = alloc_type (NULL);
+  TYPE_CODE (internal_fn_type) = TYPE_CODE_INTERNAL_FUNCTION;
+  TYPE_LENGTH (internal_fn_type) = sizeof (struct internal_function *);
+  TYPE_NAME (internal_fn_type) = "<internal function>";
 }
diff --git a/gdb/value.h b/gdb/value.h
index 2aac9b2..bb597b2 100644
--- a/gdb/value.h
+++ b/gdb/value.h
@@ -451,11 +451,11 @@ extern void set_internalvar_component (struct internalvar *var,
 				       int bitpos, int bitsize,
 				       struct value *newvalue);
 
-extern struct internalvar *lookup_only_internalvar (char *name);
+extern struct internalvar *lookup_only_internalvar (const char *name);
 
-extern struct internalvar *create_internalvar (char *name);
+extern struct internalvar *create_internalvar (const char *name);
 
-extern struct internalvar *lookup_internalvar (char *name);
+extern struct internalvar *lookup_internalvar (const char *name);
 
 extern int value_equal (struct value *arg1, struct value *arg2);
 
@@ -570,4 +570,19 @@ extern struct value *value_allocate_space_in_inferior (int);
 extern struct value *value_of_local (const char *name, int complain);
 
 extern struct value * value_subscripted_rvalue (struct value *array, struct value *idx, int lowerbound);
+
+/* User function handler.  */
+
+typedef struct value *(*internal_function_fn) (void *cookie,
+					       int argc,
+					       struct value **argv);
+
+void add_internal_function (const char *name, const char *doc,
+			    internal_function_fn handler, void *cookie);
+
+struct value *call_internal_function (struct value *function,
+				      int argc, struct value **argv);
+
+char *value_internal_function_name (struct value *);
+
 #endif /* !defined (VALUE_H) */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]