Implement show | set may-call-functions [on|off]
authorPhilippe Waroquiers <philippe.waroquiers@skynet.be>
Tue, 23 Apr 2019 21:43:24 +0000 (23:43 +0200)
committerPhilippe Waroquiers <philippe.waroquiers@skynet.be>
Sat, 27 Apr 2019 11:12:42 +0000 (13:12 +0200)
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 e.g. by using the shortcut /c.

This is version 2 of the patch.  It handles all the received comments,
mostly replace 'can-call' by 'may-call', and avoid using
'inferior function call' in factor of 'calling function in the program'.

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

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

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

gdb/doc/ChangeLog
* gdb.texinfo (Calling): Document the new
set|show may-call-functions.

gdb/ChangeLog
gdb/NEWS
gdb/doc/ChangeLog
gdb/doc/gdb.texinfo
gdb/infcall.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/callexit.exp

index e0120e7743fa8c77cb680a3f460acc1721064df4..58aa736d28eedc9e4f79cacd491b6634e34bb987 100644 (file)
@@ -1,3 +1,13 @@
+2019-04-27  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
+
+       * NEWS: Mention the new set|show may-call-functions.
+       * infcall.c (may_call_functions_p): New variable.
+       (show_may_call_functions_p): New function.
+       (call_function_by_hand_dummy): Throws an error if not
+       may-call-functions.
+       (_initialize_infcall): Call add_setshow_boolean_cmd for
+       may-call-functions.
+
 2019-04-25  Keith Seitz  <keiths@redhat.com>
 
        PR c++/24367
index 5309a8f923b46a49650bde0bb1ec713d23c3a05c..42b04e952a3aebc6083467e5b64d70346a4aadc0 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
      'array_indexes', 'symbols', 'unions', 'deref_refs', 'actual_objects',
      'static_members', 'max_elements', 'repeat_threshold', and 'format'.
 
+* New commands
+
+set may-call-functions [on|off]
+show may-call-functions
+  This controls whether GDB will attempt to call functions in
+  the program, such as with expressions in the print command.  It
+  defaults to on.  Calling functions in the program being debugged
+  can have undesired side effects.  It is now possible to forbid
+  such function calls.  If function calls are forbidden, GDB will throw
+  an error when a command (such as print expression) calls a function
+  in the program.
+
 *** Changes in GDB 8.3
 
 * GDB and GDBserver now support access to additional registers on
index ba152329d75ef8b4b648011aae8488907b68dd0b..3efed670fa21f234c89348b2a89e2cfc04e4cbca 100644 (file)
@@ -1,3 +1,8 @@
+2019-04-27  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
+
+       * gdb.texinfo (Calling): Document the new
+       set|show may-call-functions.
+
 2019-04-22  Pedro Alves  <palves@redhat.com>
 
        * gdb.texinfo (Reverse Execution): Mention and xref process record
index 0733e1acfd23fa48cb3592f0448ce914e56d74f6..cf8333d86be2ecdca858d9f66b0d8309a867a925 100644 (file)
@@ -18693,6 +18693,28 @@ 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 may-call-functions
+@kindex set may-call-functions
+@cindex disabling calling functions in the program
+@cindex calling functions in the program, disabling
+Set permission to call functions in the program.
+This controls whether @value{GDBN} will attempt to call functions in
+the program, such as with expressions in the @code{print} command.  It
+defaults to @code{on}.
+
+To call a function in the program, @value{GDBN} has to temporarily
+modify the state of the inferior.  This has potentially undesired side
+effects.  Also, having @value{GDBN} call nested functions is likely to
+be erroneous and may even crash the program being debugged.  You can
+avoid such hazards by forbidding @value{GDBN} from calling functions
+in the program being debugged.  If calling functions in the program
+is forbidden, GDB will throw an error when a command (such as printing
+an expression) starts a function call in the program.
+
+@item show may-call-functions
+@kindex show may-call-functions
+Show permission to call functions in the program.
+
 @end table
 
 @subsection Calling functions with no debug info
index af60fdc56b2911f1813c41f7b59c990f10b6978c..f99206c458203e8fa12861607d8f2eeef349c7ec 100644 (file)
    asynchronous inferior function call implementation, and that in
    turn means restructuring the code so that it is event driven.  */
 
+static int may_call_functions_p = 1;
+static void
+show_may_call_functions_p (struct ui_file *file, int from_tty,
+                          struct cmd_list_element *c,
+                          const char *value)
+{
+  fprintf_filtered (file,
+                   _("Permission to call functions in the program 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 +719,10 @@ call_function_by_hand_dummy (struct value *function,
   struct gdb_exception e;
   char name_buf[RAW_FUNCTION_ADDRESS_SIZE];
 
+  if (!may_call_functions_p)
+    error (_("Cannot call functions in the program: "
+            "may-call-functions is off."));
+
   if (!target_has_execution)
     noprocess ();
 
@@ -1359,6 +1374,17 @@ When the function is done executing, GDB will silently stop."),
 void
 _initialize_infcall (void)
 {
+  add_setshow_boolean_cmd ("may-call-functions", no_class,
+                          &may_call_functions_p, _("\
+Set permission to call functions in the program."), _("\
+Show permission to call functions in the program."), _("\
+When this permission is on, GDB may call functions in the program.\n\
+Otherwise, any sort of attempt to call a function in the program\n\
+will result in an error."),
+                          NULL,
+                          show_may_call_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."), _("\
index 9b0725a0335b046430b288d1d9f5fa4369f4a0cb..e81409387e14dceaec67c5064acb7b8dd65a24f6 100644 (file)
@@ -1,3 +1,7 @@
+2019-04-27  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
+
+       * gdb.base/callexit.exp: Test may-call-functions off.
+
 2019-04-25  Keith Seitz  <keiths@redhat.com>
 
        PR c++/24367
index b6d9ae3f875ee899a16e3a9b681e938b66004228..9a32d3d54a5de25722790da466c18f8227d35c2b 100644 (file)
@@ -37,6 +37,13 @@ if { ![runto_main] } {
     return 0
 }
 
+# Verify set may-call-functions behaviour.
+gdb_test_no_output "set may-call-functions off"
+gdb_test "call callexit()" \
+    "Cannot call functions in the program: may-call-functions is off." \
+    "inferior function call refused in off state"
+gdb_test_no_output "set may-call-functions on"
+
 # Call function (causing the program to exit), and see if gdb handles
 # it properly.
 gdb_test "call callexit()" \
This page took 0.064379 seconds and 4 git commands to generate.