From patchwork Wed Mar 30 22:11:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Evans X-Patchwork-Id: 11570 Received: (qmail 75757 invoked by alias); 30 Mar 2016 22:11:32 -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 75748 invoked by uid 89); 30 Mar 2016 22:11:31 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.0 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 spammy=H*MI:sk:001a113, H*M:sk:001a113, extracting, value_type X-HELO: mail-pf0-f201.google.com Received: from mail-pf0-f201.google.com (HELO mail-pf0-f201.google.com) (209.85.192.201) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Wed, 30 Mar 2016 22:11:16 +0000 Received: by mail-pf0-f201.google.com with SMTP id 4so4515854pfd.0 for ; Wed, 30 Mar 2016 15:11:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:message-id:date:subject:from:to; bh=mouCvf8q0TiO5cKUc+tutWZvJkvVTMEo9a9JiKtQ03w=; b=QHVglxcrT2XhiUKG4pZlfGSzEYLxMmcTIHCCffiO9LI54V5glb4eFxY8+AWf2iujgF gk/qMwJX8GCdJm4a++8cxrZLitmSHQGn2tNzknT1SPdyrKeJ1CEc9qtlMU8vM844HQ+W hSieCEEqEgUECwiC/RF7mjH6WxtqbDQO6V4MJiVYOxIW72a/6R8sXdz5Riv2EUt2NS43 H4nRTXVlZptZxQhFmQv6fZPqSPsS17nO6uNcOA+7x7rbfu/LAkb1QGNgDsUGsXM/QFXP Xes4duRhbezftRIG1GXgzkaqRQqS4rM6jPViMAQ5fWbcONLOpepxDNiyMOV7dvmMctVm m+5Q== X-Gm-Message-State: AD7BkJKIRisdA38xAe6u+axcYBDCD6wMMqULIRqdjtRNVwwCLOiwLYKhjuuGH/xSPezdYJmyp800D8CkceCxEzyBBg2CAsHEsHCjkITNP9522OWnvzUEwZoVDLSHtOAYLUOgAhQvUpnol5/JbFzk+zDadbq+PVCJI8PuXRac6MSdpfmE3N/ufA== MIME-Version: 1.0 X-Received: by 10.98.33.76 with SMTP id h73mr6382365pfh.0.1459375874558; Wed, 30 Mar 2016 15:11:14 -0700 (PDT) Message-ID: <001a113acad09920e7052f4b6c95@google.com> Date: Wed, 30 Mar 2016 22:11:14 +0000 Subject: [PATCH] Fix use of lazy strings outside of pretty-printers From: Doug Evans To: gdb-patches@sourceware.org X-IsSubscribed: yes Hi. Python lazy string support is broken, outside of usage in pretty-printers. The pretty-printer machinery knows how to read NUL-terminated lazy strings, but there is no such support in py-lazy-string.c:stpy_convert_to_value. Regression tested on amd64-linux. 2016-03-30 Doug Evans * python/py-lazy-string.c (lazy_string_object): Add comment. (stpy_convert_to_value): Handle NUL-terminated strings. * python/py-value.c (valpy_lazy_string): Handle TYPE_CODE_ARRAY like TYPE_CODE_PTR. testsuite/ * gdb.python/py-lazy-string.c (name): New global. * gdb.python/py-lazy-string.exp: Add tests for extracting lazy string values. + "\"Dave\"" +gdb_test "python print name.cast(char_ptr).lazy_string(length=2).value()" \ + "\"Da\"" diff --git a/gdb/python/py-lazy-string.c b/gdb/python/py-lazy-string.c index d4b40df..8688bd9 100644 --- a/gdb/python/py-lazy-string.c +++ b/gdb/python/py-lazy-string.c @@ -41,7 +41,8 @@ typedef struct { long length; /* This attribute holds the type that is represented by the lazy - string's type. */ + string's type. IOW, this is the type of the character in the string. + For a C string, this is "char", not "char *". */ struct type *type; } lazy_string_object; @@ -106,7 +107,25 @@ stpy_convert_to_value (PyObject *self, PyObject *args) TRY { - val = value_at_lazy (self_string->type, self_string->address); + struct type *elm_type = self_string->type; + long length = self_string->length; + struct type *index_type, *range_type, *array_type; + + if (TYPE_OBJFILE_OWNED (elm_type)) + index_type + = objfile_type (TYPE_OWNER (elm_type).objfile)->builtin_long; + else + index_type + = builtin_type (TYPE_OWNER (elm_type).gdbarch)->builtin_long; + + if (self_string->length == -1) + range_type = create_static_range_type (NULL, index_type, 0, -1); + else + range_type + = create_static_range_type (NULL, index_type, 0, length - 1); + + array_type = create_array_type (NULL, elm_type, range_type); + val = value_at_lazy (array_type, self_string->address); } CATCH (except, RETURN_MASK_ALL) { diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index 7dba0ad..88301e2 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -429,7 +429,8 @@ valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw) { struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); - if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR) + if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR + || TYPE_CODE (value_type (value)) == TYPE_CODE_ARRAY) value = value_ind (value); str_obj = gdbpy_create_lazy_string_object (value_address (value), length, diff --git a/gdb/testsuite/gdb.python/py-lazy-string.c b/gdb/testsuite/gdb.python/py-lazy-string.c index a6e1ee8..8bef66f 100644 --- a/gdb/testsuite/gdb.python/py-lazy-string.c +++ b/gdb/testsuite/gdb.python/py-lazy-string.c @@ -15,6 +15,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +const char name[32] = "Dave"; + int main () { diff --git a/gdb/testsuite/gdb.python/py-lazy-string.exp b/gdb/testsuite/gdb.python/py-lazy-string.exp index f57a482..5d0557f 100644 --- a/gdb/testsuite/gdb.python/py-lazy-string.exp +++ b/gdb/testsuite/gdb.python/py-lazy-string.exp @@ -40,3 +40,14 @@ gdb_test "python print(null.lazy_string(length=0).value())" \ "gdb.MemoryError: Cannot create a value from NULL.*Error while executing Python code." gdb_test "python print(null.lazy_string(length=3).value())" \ "gdb.MemoryError: Cannot create a lazy string with address 0x0, and a non-zero length.*Error while executing Python code." + +gdb_test_no_output "python name = gdb.parse_and_eval(\"name\")" +gdb_test_no_output "python char_ptr = gdb.lookup_type(\"char\").pointer()" +gdb_test "python print name.lazy_string().value()" \ + "\"Dave\"" +gdb_test "python print name.lazy_string(length=2).value()" \ + "\"Da\"" +gdb_test "python print name.cast(char_ptr).lazy_string().value()" \