public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [RFA] Implement show | set can-call-inferior-functions [on|off]
@ 2019-04-23 21:58 Philippe Waroquiers
  2019-04-24  6:53 ` Eli Zaretskii
  2019-04-25 17:17 ` Pedro Alves
  0 siblings, 2 replies; 7+ messages in thread
From: Philippe Waroquiers @ 2019-04-23 21:58 UTC (permalink / raw)
  To: gdb-patches; +Cc: Philippe Waroquiers

Inferior function calls are powerful but might lead to undesired
results such as crashes when calling nested functions (frequently
used in particular in Ada).

This implements a GDB setting to disable calling inferior functions.

Note: the idea is that if/when the 'slash command' patch is pushed,
that this setting can be changed by using the shortcut /c.

2019-04-23  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

gdb/ChangeLog
	* NEWS: Mention the new set|show can-call-inferior-functions.
	* infcall.c (can_call_inferior_functions_p): New variable.
	(show_can_call_inferior_functions_p): New function.
	(call_function_by_hand_dummy): Throws an error if not
	can-call-inferior-functions.
	(_initialize_infcall): Call add_setshow_boolean_cmd for
	can-call-inferior-functions.

gdb/testsuite/ChangeLog
	* gdb.base/callexit.exp: Test can-call-inferior-functions off.

gdb/doc/ChangeLog
	* gdb.texinfo (Calling): Document the new
	set|show can-call-inferior-functions.
---
 gdb/NEWS                            | 11 +++++++++++
 gdb/doc/gdb.texinfo                 | 16 +++++++++++++++
 gdb/infcall.c                       | 30 +++++++++++++++++++++++++++++
 gdb/testsuite/gdb.base/callexit.exp |  7 +++++++
 4 files changed, 64 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index 5309a8f923..6288fed8af 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -27,6 +27,17 @@
      'array_indexes', 'symbols', 'unions', 'deref_refs', 'actual_objects',
      'static_members', 'max_elements', 'repeat_threshold', and 'format'.
 
+* New commands
+
+set can-call-inferior-functions [on|off]
+show can-call-inferior-functions
+  Control whether GDB is allowed to do inferior function calls.
+  Inferior function calls are e.g. needed to evaluate and print
+  some expressions.  Such inferior function calls can have undesired
+  side effects.  It is now possible to forbid such inferior function
+  calls.
+  By default, GDB is allowed to do inferior function calls.
+
 *** Changes in GDB 8.3
 
 * GDB and GDBserver now support access to additional registers on
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 0733e1acfd..38fd109369 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -18693,6 +18693,22 @@ the default C@t{++} exception handler and the inferior terminated.
 Show the current setting of stack unwinding in the functions called by
 @value{GDBN}.
 
+@item set can-call-inferior-functions
+@kindex set can-call-inferior-functions
+@cindex disabling inferior function calls.
+Set debugger's willingness to call inferior functions.
+To call an inferior function, @value{GDBN} has to temporarily modify the state
+of the inferior.  This has potentially undesired side effects.
+Also, having @value{GDBN} calling nested functions is likely to be erroneous
+and may even crash the program being debugged.
+You can avoid such hazards by forbidding  @value{GDBN} to do inferior
+functions calls.
+The default is to allow inferior function calls.
+
+@item show can-call-inferior-functions
+@kindex show can-call-inferior-functions
+Show debugger's willingness to call inferior functions.
+
 @end table
 
 @subsection Calling functions with no debug info
diff --git a/gdb/infcall.c b/gdb/infcall.c
index c102b301e0..a8965bd74a 100644
--- a/gdb/infcall.c
+++ b/gdb/infcall.c
@@ -55,6 +55,18 @@
    asynchronous inferior function call implementation, and that in
    turn means restructuring the code so that it is event driven.  */
 
+static int can_call_inferior_functions_p = 1;
+static void
+show_can_call_inferior_functions_p (struct ui_file *file, int from_tty,
+				    struct cmd_list_element *c,
+				    const char *value)
+{
+  fprintf_filtered (file,
+		    _("Debugger's willingness to call "
+		      "inferior functions is %s.\n"),
+		    value);
+}
+
 /* How you should pass arguments to a function depends on whether it
    was defined in K&R style or prototype style.  If you define a
    function using the K&R syntax that takes a `float' argument, then
@@ -708,6 +720,10 @@ call_function_by_hand_dummy (struct value *function,
   struct gdb_exception e;
   char name_buf[RAW_FUNCTION_ADDRESS_SIZE];
 
+  if (!can_call_inferior_functions_p)
+    error (_("Cannot call inferior function: "
+	     "can-call-inferior-functions is off."));
+
   if (!target_has_execution)
     noprocess ();
 
@@ -1359,6 +1375,20 @@ When the function is done executing, GDB will silently stop."),
 void
 _initialize_infcall (void)
 {
+  add_setshow_boolean_cmd ("can-call-inferior-functions", no_class,
+			   &can_call_inferior_functions_p, _("\
+Set debugger's willingness to call inferior functions."), _("\
+Show debugger's willingness to call inferior functions."), _("\
+To call an inferior function, GDB has to temporarily modify the state\n\
+of the inferior.  This has potentially undesired side effects.\n\
+Also, having GDB calling nested functions is likely to be erroneous\n\
+and may even crash the program being debugged.\n\
+You can avoid such hazards by forbidding GDB to do inferior functions calls.\n\
+The default is to allow inferior function calls."),
+			   NULL,
+			   show_can_call_inferior_functions_p,
+			   &setlist, &showlist);
+
   add_setshow_boolean_cmd ("coerce-float-to-double", class_obscure,
 			   &coerce_float_to_double_p, _("\
 Set coercion of floats to doubles when calling functions."), _("\
diff --git a/gdb/testsuite/gdb.base/callexit.exp b/gdb/testsuite/gdb.base/callexit.exp
index b6d9ae3f87..ac93125eb4 100644
--- a/gdb/testsuite/gdb.base/callexit.exp
+++ b/gdb/testsuite/gdb.base/callexit.exp
@@ -37,6 +37,13 @@ if { ![runto_main] } {
     return 0
 }
 
+# Verify set can-call-inferior-functions behaviour.
+gdb_test_no_output "set can-call-inferior-functions off"
+gdb_test "call callexit()" \
+    "Cannot call inferior function: can-call-inferior-functions is off." \
+    "Inferior function call refused in off state"
+gdb_test_no_output "set can-call-inferior-functions on"
+
 # Call function (causing the program to exit), and see if gdb handles
 # it properly.
 gdb_test "call callexit()" \
-- 
2.20.1

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2019-04-25 19:44 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-23 21:58 [RFA] Implement show | set can-call-inferior-functions [on|off] Philippe Waroquiers
2019-04-24  6:53 ` Eli Zaretskii
2019-04-24 21:25   ` Philippe Waroquiers
2019-04-25  6:12     ` Eli Zaretskii
2019-04-25 13:15       ` Tom Tromey
2019-04-25 19:44         ` Pedro Alves
2019-04-25 17:17 ` Pedro Alves

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).