From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19971 invoked by alias); 20 Sep 2013 02:47:50 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 19928 invoked by uid 89); 20 Sep 2013 02:47:50 -0000 Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 20 Sep 2013 02:47:50 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00,KHOP_THREADED,RDNS_NONE,SPF_HELO_FAIL autolearn=no version=3.3.2 X-HELO: relay1.mentorg.com Received: from svr-orw-exc-10.mgc.mentorg.com ([147.34.98.58]) by relay1.mentorg.com with esmtp id 1VMqkz-0004hY-L6 from Yao_Qi@mentor.com for gdb-patches@sourceware.org; Thu, 19 Sep 2013 19:47:45 -0700 Received: from SVR-ORW-FEM-03.mgc.mentorg.com ([147.34.97.39]) by SVR-ORW-EXC-10.mgc.mentorg.com with Microsoft SMTPSVC(6.0.3790.4675); Thu, 19 Sep 2013 19:47:45 -0700 Received: from qiyao.dyndns.org (147.34.91.1) by svr-orw-fem-03.mgc.mentorg.com (147.34.97.39) with Microsoft SMTP Server id 14.2.247.3; Thu, 19 Sep 2013 19:47:44 -0700 From: Yao Qi To: Subject: [PATCH 1/7] Emit a query when writing to a readonly section and trust_readonly is true Date: Fri, 20 Sep 2013 02:47:00 -0000 Message-ID: <1379645226-8719-2-git-send-email-yao@codesourcery.com> In-Reply-To: <1379645226-8719-1-git-send-email-yao@codesourcery.com> References: <1378641807-24256-1-git-send-email-yao@codesourcery.com> <1379645226-8719-1-git-send-email-yao@codesourcery.com> MIME-Version: 1.0 Content-Type: text/plain X-IsSubscribed: yes X-SW-Source: 2013-09/txt/msg00720.txt.bz2 If users write readonly section, such as .text, the contents of the inferior and of the executable become out of sync. It is better to emit a query to ask users to "set trust-readonly-sections off". (gdb) set trust-readonly-sections on (gdb) p /x* (char *) 0x080484c1 = 0xcc Address is read-only and trust-readonly-sections is set to "on". Set it to "off" and write to a read-only section? (y or n) y $1 = 0xcc gdb: 2013-09-20 Yao Qi * target.c (memory_xfer_partial_1): Emit a query if GDB writes to a readonly section and 'trust_readonly' is true. gdb/testsuite: 2013-09-20 Yao Qi * gdb.base/trust-readonly.exp: New. --- gdb/target.c | 35 +++++++++--- gdb/testsuite/gdb.base/trust-readonly.exp | 85 +++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 gdb/testsuite/gdb.base/trust-readonly.exp diff --git a/gdb/target.c b/gdb/target.c index d55712d..37db36b 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -1471,11 +1471,13 @@ memory_xfer_partial_1 (struct target_ops *ops, enum target_object object, } } - /* Try the executable files, if "trust-readonly-sections" is set. */ - if (readbuf != NULL && trust_readonly) + /* If "trust-readonly-sections" is set, read from the executable + files. When GDB writes to a readonly section of the inferior, + emit a warning that option 'trust-readonly-sections' should be + turned off. */ + if (trust_readonly) { struct target_section *secp; - struct target_section_table *table; secp = target_section_by_addr (ops, memaddr); if (secp != NULL @@ -1483,12 +1485,27 @@ memory_xfer_partial_1 (struct target_ops *ops, enum target_object object, secp->the_bfd_section) & SEC_READONLY)) { - table = target_get_section_table (ops); - return section_table_xfer_memory_partial (readbuf, writebuf, - memaddr, len, - table->sections, - table->sections_end, - NULL); + + if (readbuf != NULL) + { + struct target_section_table *table; + + table = target_get_section_table (ops); + return section_table_xfer_memory_partial (readbuf, writebuf, + memaddr, len, + table->sections, + table->sections_end, + NULL); + } + + if (writebuf != NULL) + { + if (query (_("Address is read-only and trust-readonly-sections\ + is set to \"on\".\nSet it to \"off\" and write to a read-only section? "))) + trust_readonly = 0; + else + return -1; + } } } diff --git a/gdb/testsuite/gdb.base/trust-readonly.exp b/gdb/testsuite/gdb.base/trust-readonly.exp new file mode 100644 index 0000000..8627800 --- /dev/null +++ b/gdb/testsuite/gdb.base/trust-readonly.exp @@ -0,0 +1,85 @@ +# Copyright 2013 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +standard_testfile start.c + +if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } { + untested "Could not compile $binfile." + return -1 +} + +clean_restart ${binfile} + +if ![runto_main] then { + return -1 +} + +set main_address "" +set test "Extract main address" +gdb_test_multiple "print *main" $test { + -re "\} ($hex) .*$gdb_prompt $" { + pass $test + set main_address $expect_out(1,string) + } + -re ".*$gdb_prompt $" { + # Unable to extract address of main. Bail out. + fail $test + return -1 + } +} + +set main_contents "" +set test "Extract main contents" +gdb_test_multiple "print/x *(char *) $main_address" $test { + -re "\\$$decimal = ($hex).*$gdb_prompt $" { + pass $test + set main_contents $expect_out(1,string) + } + -re ".*$gdb_prompt $" { + # Unable to extract contents of main. Bail out. + fail $test + return -1 + } +} + +# Set option "trust-readonly-sections" off, write one byte and read it +# back. + +gdb_test_no_output "set trust-readonly-sections off" "" +gdb_test "print /x* (char *) $main_address = 0xcc" "\\$$decimal = 0xcc" \ + "write to main (off)" +gdb_test "print /x * (char *) $main_address" "\\$$decimal = 0xcc" \ + "read from main (off) 1" + +gdb_test_no_output "set trust-readonly-sections on" "" +gdb_test "print /x* (char *) $main_address = 0x11" "\\$$decimal = 0x11" \ + "write to main (on)" \ + "Address is read-only and trust-readonly-sections is set to \"on\"\..*y or n. $" \ + "y" + +# Option "trust-readonly-sections" is turned off as answer "y" is +# entered above. +gdb_test "show trust-readonly-sections" \ + "Mode for reading from readonly sections is off\." + +gdb_test "print /x * (char *) $main_address" "\\$$decimal = 0x11" \ + "read from main (off) 2" + +# Set option "trust-readonly-sections" on, and test GDB read data from +# the executable file instead of the inferior. + +gdb_test_no_output "set trust-readonly-sections on" "" +gdb_test "print /x * (char *) $main_address" "\\$$decimal = ${main_contents}" \ + "read from main (on)" -- 1.7.7.6