From patchwork Tue Apr 23 21:58:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Waroquiers X-Patchwork-Id: 32396 Received: (qmail 102201 invoked by alias); 23 Apr 2019 21:58:37 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 102188 invoked by uid 89); 23 Apr 2019 21:58:37 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-20.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.1 spammy=willingness, forbid, slash, terminated X-HELO: mailsec115.isp.belgacom.be Received: from mailsec115.isp.belgacom.be (HELO mailsec115.isp.belgacom.be) (195.238.20.111) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 23 Apr 2019 21:58:34 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skynet.be; i=@skynet.be; q=dns/txt; s=securemail; t=1556056714; x=1587592714; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=3g4zvD1pGkmXUm2Qb8ZkHIm+dMr6z0W6ERycCQqjsuc=; b=NloHHDinmv5ydf6gFsGjAmbumR/UObjmisfJD3kOHpVa9jvBJcVmwKfa fv8omRXYr2fBqa/vJWGgISyXCw7n9g==; Received: from unknown (HELO md.home) ([109.131.123.45]) by relay.skynet.be with ESMTP/TLS/DHE-RSA-AES128-GCM-SHA256; 23 Apr 2019 23:58:32 +0200 From: Philippe Waroquiers To: gdb-patches@sourceware.org Cc: Philippe Waroquiers Subject: [RFA] Implement show | set can-call-inferior-functions [on|off] Date: Tue, 23 Apr 2019 23:58:26 +0200 Message-Id: <20190423215826.9936-1-philippe.waroquiers@skynet.be> MIME-Version: 1.0 X-IsSubscribed: yes 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 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()" \