From patchwork Thu Mar 22 04:03:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 26413 Received: (qmail 41443 invoked by alias); 22 Mar 2018 04:03:47 -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 41429 invoked by uid 89); 22 Mar 2018 04:03:46 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS autolearn=ham version=3.3.2 spammy=dtd, UD:dtd, DTD, seg X-HELO: sessmg22.ericsson.net Received: from sessmg22.ericsson.net (HELO sessmg22.ericsson.net) (193.180.251.58) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 22 Mar 2018 04:03:41 +0000 Received: from ESESSHC022.ericsson.se (Unknown_Domain [153.88.183.84]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id CF.28.13895.A1B23BA5; Thu, 22 Mar 2018 05:03:38 +0100 (CET) Received: from ESESBMR502.ericsson.se (153.88.183.134) by ESESSHC022.ericsson.se (153.88.183.84) with Microsoft SMTP Server (TLS) id 14.3.382.0; Thu, 22 Mar 2018 05:03:37 +0100 Received: from ESESSMB501.ericsson.se (153.88.183.162) by ESESBMR502.ericsson.se (153.88.183.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1034.26; Thu, 22 Mar 2018 05:03:37 +0100 Received: from NAM02-BL2-obe.outbound.protection.outlook.com (153.88.183.157) by ESESSMB501.ericsson.se (153.88.183.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1034.26 via Frontend Transport; Thu, 22 Mar 2018 05:03:37 +0100 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=simon.marchi@ericsson.com; Received: from elxacz23q12.lan (192.222.164.54) by SN6PR1501MB2014.namprd15.prod.outlook.com (2603:10b6:805:d::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.588.14; Thu, 22 Mar 2018 04:03:33 +0000 From: Simon Marchi To: CC: Simon Marchi Subject: [PATCH] Make target_read_alloc & al return vectors Date: Thu, 22 Mar 2018 00:03:21 -0400 Message-ID: <1521691401-21512-1-git-send-email-simon.marchi@ericsson.com> MIME-Version: 1.0 X-ClientProxiedBy: YQBPR0101CA0011.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:c00::24) To SN6PR1501MB2014.namprd15.prod.outlook.com (2603:10b6:805:d::33) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 052e815b-3256-4239-1302-08d58fa9e1a9 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(5600026)(4604075)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7153060)(7193020); SRVR:SN6PR1501MB2014; X-Microsoft-Exchange-Diagnostics: 1; SN6PR1501MB2014; 3:s08QUWNX7taYHwDEs7Mw03PjBXb6xiMjd1v4nQufl6epULfTQK0GskoW/8xG6J7eoAnsbYxLChMJqSJ2flRHFk0pR0O4ClPVWSnHlVSNW/AJDnQ0cMKBc+ZyFTavWpG0Eb0qnMxxBEiF8gxMLP2D0jJnm6K2ws2SnU2qJwoGIkRm5qOONvQPqUpPdhyC3R5LYaFCPeSzgktDhsbZfRNqkEiJNCAyN6yIjfqykPNZPtlX1+UizQxMlrkmfBttyGOv; 25:1TFabhzULmkyybg3pC5yERGdafWdYDeL+aHfA7FOLxydkQnQu1AwrO0Xf4k5aJG262RJJI/1waCNtkTEyUP8BYgijQygvsSfksPlPwDfzmnYFjL56xRGmu07Wjpy1/xXTbgLJPaNqJwfT/X97JpqC33E6+NnxR9kWr5PYA5goNgY4OKX5lqoYSpCjCNNhSoLOzoaendvyPrrQqnfHwE/GF/agodS93bcN9eqLjwB/0u/vByuZWk+kRDzM28ky4IFUPNm11EUXFMzOLhYyzAtmoQybBMRB/ZGytLp7Da/+Fn8R/2CVLlV8X03of/6TrigPeOnAfFAuQUkcbud1XIKfQ==; 31:dGO+4aj4bz0wx5JAp+x0D/Wi3ybCCUQDV+yXh1Hr7JaUU7CMeJpQ5/v+vfMKSYGOJoLu7tmRq21cI2+NxVvS9yUCfzUHKQNq6vfvWh4R4QE09UDh2HRJ7yNpmkapK/Gmi7lJLsR9eRpZKxQFcI3iyPg/dZHS1AhocNOhD+65k52h5opbU7rXHD//DztcbxfGba5NreawfVu2pe1p08cS5omPsgGrw5+CTXCPWck8cco= X-MS-TrafficTypeDiagnostic: SN6PR1501MB2014: X-Microsoft-Exchange-Diagnostics: 1; SN6PR1501MB2014; 20:7DxYP7d23jsqPRYWhYprM2Mh3WuAaZVgyI5W+4W6q2PH3+rGE34g6GPqxkYqKXz0daTOd6lG6etCmTkDuWpcB0jJhQMRjje9GRTpL04cjEhnBYmhZwu88rXyTFskbff5PtVEoKEomgyLIaDJ3fdyA8DGsqxOhm1kdWU71cP4UvzVMjcPKzyGcjYvA4GvUM88vzYeqx9GsJdLjU+9s8cwTsxjziUE+8K1Xs+r1lFcAQi1MsETtgSdip4sFzn5v9mdcPJ5gCpLGyCUej64NguYt9iUgQf9GYsLWg8vmXZktRu5StZxMIzjjI1TDh7fDRR93E7GsScgN1I34njdLbvwOfWo38r+UNglAZ6plEgEmWAsxLHYvi6FfxFAFxSdU8Yd9PCuhWvVQz6+icmRgoNvZd33sXAtP3kxZSmS/jWLKaACVvPDM1AOE3h4hoJV2Em07Q7c+QhuLJ92L+wRaSCWVJdgVLTpgiVw8Z8suW5Lpz0+mz7WP6vmFakdiITPXyVD; 4:GbahgADPdl88S1dO+IzJ2AQsIvDrBOkvOZ7gUwKEHjiruTk6SoV5VPRtMPapkRf/gqglBqCvm8T6Ji7UhwEQGpAFqJoEovdI0cYI+2uL/Kbm5ygN/ryiOEBKTnr5HN74q9Nwcdag2sEb8vU5kGl/EbtbMENI7TIPWSxSIlLFNJhF/ECy3Q1168FaATvbdUeNlDRQrDAMQkNnnopLHcYemdpZNA0WB5LkgYCLFai4+fy1nJfoBuJGpl1/guzs7SNIlbo2J4LaODAGb1o8SkQMcA== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(10201501046)(3231221)(944501327)(52105095)(93006095)(93001095)(3002001)(6041310)(20161123562045)(20161123558120)(20161123564045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011); SRVR:SN6PR1501MB2014; BCL:0; PCL:0; RULEID:; SRVR:SN6PR1501MB2014; X-Forefront-PRVS: 0619D53754 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(376002)(366004)(396003)(346002)(39860400002)(39380400002)(189003)(199004)(54534003)(2906002)(69596002)(47776003)(6116002)(3846002)(50226002)(6916009)(26005)(25786009)(4326008)(16526019)(68736007)(59450400001)(6506007)(386003)(66066001)(8936002)(186003)(316002)(478600001)(53946003)(6512007)(2361001)(48376002)(2351001)(7736002)(107886003)(36756003)(1857600001)(6486002)(305945005)(81166006)(53936002)(52116002)(21086003)(8676002)(105586002)(86362001)(97736004)(6666003)(106356001)(575784001)(16586007)(5660300001)(51416003)(50466002)(81156014); DIR:OUT; SFP:1101; SCL:1; SRVR:SN6PR1501MB2014; H:elxacz23q12.lan; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: ericsson.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; SN6PR1501MB2014; 23:8Hvo/Xl80Geciz3vI62q85O658VSp7B0bldQyjj?= =?us-ascii?Q?eOvbEf/O6+VKhtKlu7ePzddRYm3EK5NKh5czx/vHYzIKdAsGllKiEppmrxAz?= =?us-ascii?Q?z34gnJ0dgECaBJRwZhDNKPqkxOrOFJyJu3cctpEFkB9C+RkOlkD8+wD22RlR?= =?us-ascii?Q?y89A6sZCI1Ct5KC7m3lGiGtdTeURjCHA1E1XL3wmMd5D5SCA+xM7WDa4ILJE?= =?us-ascii?Q?RA3LyPpzlnT0uyCJb3w2i9CZHbt/dV4GrlhTP5PawFwoowd4/RR+ZIlN35o1?= =?us-ascii?Q?MPh/tyXsPNaXfwsJ3CxHXapXMuOa8TJ6x/CL31jfvUDwAuwXiKB3UqILPdLC?= =?us-ascii?Q?8gqeIlHsLuK+Uj5vRYcOhFEPIKMQbEoO+6KbJP0tI1NDGNQm3Y3Ud6j9rHXx?= =?us-ascii?Q?keoa2T0F6WGbNoDewLIQ1jcnUKtrizMYepXVwEm80uRBkyQGEh9MiIcILBoE?= =?us-ascii?Q?JbOsyWX6cZEMhbwXxuLvTKs/wXfhOjFpj1/l1PvgtbU7AhPHx6dx/6UkkuBu?= =?us-ascii?Q?Vlt66oK6VHmLLszexbHEdgmihQtWXQOoTnZnfUTTUzNENHeJooln+Mo3s5Vm?= =?us-ascii?Q?TH5UF9aL/er5vReC7uw+7U00pkIwFa1AaNfK9bbWwnKc1J9Z550gn53rX1Ix?= =?us-ascii?Q?YZ914enkwxHcfK3a6C8QTW7bVLhG2iIJCKu2stQdq+YZrwMp7paHHQQejspI?= =?us-ascii?Q?+dk3cl9WIW7T8VXOune4McmMh1zkoaiQFDw/xgF93bH+ZMCXfQ8KpfT7isc6?= =?us-ascii?Q?ylrkqHoaw3w6au8vA0wvq7nNQuMYtU2F9p9uMS6/UWcPqb4LhRgZeDljTZbD?= =?us-ascii?Q?Mp7tmdxMRSQ0a7BByEbJ9aqs/ETwDglPVOy1aVeLJfCjSsncnolyBys6QwFm?= =?us-ascii?Q?ePCD6fu9tw6s1LCNZQEkrqw5VSWeQpGR3NLcsKM4gB1hFAvQPHCO0WlHggbR?= =?us-ascii?Q?prwU0cufZC+l+gk60gIIkKBH2GoTExyEU2IwWZAXPbCcZ2u2JhVMiRGwr47a?= =?us-ascii?Q?UFhHBIU4hfqNXQN1B2akSYWWKK++w74Oxx6/6fo0kx2MQ70uxjfBPB0kYGgK?= =?us-ascii?Q?u+NNwSNKuPBSIf4Xa5/M4HvyCfsGQ1SG73LIOvrV96XJdiNRIQzcziJxR/67?= =?us-ascii?Q?/niSvFDIiEcLaRPhlLfVOz4rp/tEbHLL5W4SvqZZoZEjg3djMZTfQOJNz8F9?= =?us-ascii?Q?jrTSNCIMksIPk6F4QkQqOfuOKJ7a8+CoGqOE9W28osz+FmLt0JrDZNJgYKVv?= =?us-ascii?Q?EDPry4tcpin/lRR+L1tb+eigc/rG4AinsFwZGvoTL?= X-Microsoft-Antispam-Message-Info: ++QsbZzzNB+2fsnH58ePRgJY81fwVSkMAozgVIENgmjzrtmWER0xqoznQKeUaM7WLnOuBAxmCfxJx6fWi3flB++HXJPC4MxPgkfCWcvkOafRPOkmaqQO6PPlD9qnLnQEXjnDRHD+6QC0BXZyu6uCC0DLRbRfMhwwJwuAp1A8xDHriMkyBSPoQixKCsGOvVyM X-Microsoft-Exchange-Diagnostics: 1; SN6PR1501MB2014; 6:71gHjyWBG+xpNPH8pANszX8/mrpIBThbW4vMAyJRLEHnVnUk/GH1cKCNODroo0mWKhyiTyqP+HyOvt/OWzjBGN7iw7+R9qzEZ/3CCZ0grQnSGCMH7wU5cm4SReM0sKEEPYPw3YBaow7wnPDEQddDTS/SCV5ADruBeNmLZrVL0F02zGhcELzqgLgZN8CTqC39iKUT7orwJkpg5rn+0oQu3EjYYGMKhb/bmh4tqUX5/KxZb07OZhUaSBaW0iNyEGCsyds8DCpyLjiaDcGQzForruwKHQkewq842eA/ulbn77WPrzszXc98gOkV3LnlZbvQPVGgPX/BIRhmCFjSfdgkCXVapRO7DpyFzHAO3dl03xM=; 5:zPhLRdQiLXZnfklM2eY7rVbdBvrpiQCM7VUYCWwHST46EHtj6h4+gf7LlOFi8qUd2o/LpeGrktQlPaw22/9wkL44UxR8XWe2R+9bXQADzOTHx2Tr+/TVlS9fAtwBq5p7HVwq+ev2xL/pqFaxvVdoI9yPWj5AriYq92LXO5u522U=; 24:BRBr+wF/R/jXt5npfURq3GXwmNOe81rGhKyParU47h+KJHrLbIhjGLe8dfmfmo6gmaxuYXL3RqJ6b5kYyofBWX1GY9LEBd4GlXpoCiWHJ+c=; 7:74rYFpGuMXQl++KoVZftcMp/F1FsyHG/KBnwjbrnzP1z+Rm5jwiR7DFEnMD+35Lmo1SqvcsRs+6B/WqqPzScmdIdv5boCRiFFS0WVsQzpTPuqSTAWEZWhA5p7JKTtyXTnsa85YjT+BfsGgiWdakQpks13Eni3X44I6R1CjHtSB/PI8XCx3FwIcU0ttzh1hJGcPubnsXJ28TzX3BX29GlLgxeXpLzJtcp9Eg0/T1xtUGb0hmboFOSuhkIgsUniV6I SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Mar 2018 04:03:33.7867 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 052e815b-3256-4239-1302-08d58fa9e1a9 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 92e84ceb-fbfd-47ab-be52-080c6b87953f X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR1501MB2014 X-OriginatorOrg: ericsson.com X-IsSubscribed: yes This patch started by changing target_read_alloc_1 to return a byte_vector, to avoid manual memory management (in target_read_alloc_1 and in the callers). To communicate failures to the callers, it actually returns a gdb::optional. Adjusting target_read_stralloc was a bit more tricky, since it wants to return a buffer of char, and not gdb_byte. Since you can't just cast a gdb::byte_vector into a gdb::def_vector, I made target_read_alloc_1 templated, so both versions (that return vectors of gdb_byte and char) are generated. Since target_read_stralloc now returns a gdb::char_vector instead of a gdb::unique_xmalloc_ptr, a few callers need to be adjusted. gdb/ChangeLog: * common/byte-vector.h (char_vector): New type. * target.h (target_read_alloc): Return gdb::optional. (target_read_stralloc): Return gdb::optional. (target_get_osdata): Return gdb::optional. * target.c (target_read_alloc_1): Templatize. Replacement manual memory management with vector. (target_read_alloc): Change return type, adjust. (target_read_stralloc): Change return type, adjust. (target_get_osdata): Change return type, adjust. * auxv.c (struct auxv_info) : Remove. : Change type to gdb::optional. (auxv_inferior_data_cleanup): Free auxv_info with delete. (get_auxv_inferior_data): Allocate auxv_info with new, adjust. (target_auxv_search): Adjust. (fprint_target_auxv): Adjust. * avr-tdep.c (avr_io_reg_read_command): Adjust. * linux-tdep.c (linux_spu_make_corefile_notes): Adjust. (linux_make_corefile_notes): Adjust. * osdata.c (get_osdata): Adjust. * remote.c (remote_get_threads_with_qxfer): Adjust. (remote_memory_map): Adjust. (remote_traceframe_info): Adjust. (btrace_read_config): Adjust. (remote_read_btrace): Adjust. (remote_pid_to_exec_file): Adjust. * solib-aix.c (solib_aix_get_library_list): Adjust. * solib-dsbt.c (decode_loadmap): Don't free buf. (dsbt_get_initial_loadmaps): Adjust. * solib-svr4.c (svr4_current_sos_via_xfer_libraries): Adjust. * solib-target.c (solib_target_current_sos): Adjust. * tracepoint.c (sdata_make_value): Adjust. * xml-support.c (xinclude_start_include): Adjust. (xml_fetch_content_from_file): Adjust. * xml-support.h (xml_fetch_another): Change return type. (xml_fetch_content_from_file): Change return type. * xml-syscall.c (xml_init_syscalls_info): Adjust. * xml-tdesc.c (file_read_description_xml): Adjust. (fetch_available_features_from_target): Change return type. (target_fetch_description_xml): Adjust. (target_read_description_xml): Adjust. --- gdb/auxv.c | 44 ++++++++++--------------- gdb/avr-tdep.c | 40 +++++++++++----------- gdb/common/byte-vector.h | 1 + gdb/linux-tdep.c | 48 +++++++++++---------------- gdb/osdata.c | 6 ++-- gdb/remote.c | 32 +++++++++--------- gdb/solib-aix.c | 8 ++--- gdb/solib-dsbt.c | 19 +++++------ gdb/solib-svr4.c | 6 ++-- gdb/solib-target.c | 6 ++-- gdb/target.c | 86 +++++++++++++++++++----------------------------- gdb/target.h | 37 +++++++++------------ gdb/tracepoint.c | 16 ++++----- gdb/xml-support.c | 20 +++++------ gdb/xml-support.h | 10 +++--- gdb/xml-syscall.c | 6 ++-- gdb/xml-tdesc.c | 20 +++++------ 17 files changed, 180 insertions(+), 225 deletions(-) diff --git a/gdb/auxv.c b/gdb/auxv.c index 26cc63e..ce7a63b 100644 --- a/gdb/auxv.c +++ b/gdb/auxv.c @@ -304,8 +304,7 @@ static const struct inferior_data *auxv_inferior_data; overhead of transfering data from a remote target to the local host. */ struct auxv_info { - LONGEST length; - gdb_byte *data; + gdb::optional data; }; /* Handles the cleanup of the auxv cache for inferior INF. ARG is ignored. @@ -323,8 +322,7 @@ auxv_inferior_data_cleanup (struct inferior *inf, void *arg) info = (struct auxv_info *) inferior_data (inf, auxv_inferior_data); if (info != NULL) { - xfree (info->data); - xfree (info); + delete info; set_inferior_data (inf, auxv_inferior_data, NULL); } } @@ -358,9 +356,8 @@ get_auxv_inferior_data (struct target_ops *ops) info = (struct auxv_info *) inferior_data (inf, auxv_inferior_data); if (info == NULL) { - info = XCNEW (struct auxv_info); - info->length = target_read_alloc (ops, TARGET_OBJECT_AUXV, - NULL, &info->data); + info = new auxv_info; + info->data = target_read_alloc (ops, TARGET_OBJECT_AUXV, NULL); set_inferior_data (inf, auxv_inferior_data, info); } @@ -375,20 +372,17 @@ int target_auxv_search (struct target_ops *ops, CORE_ADDR match, CORE_ADDR *valp) { CORE_ADDR type, val; - gdb_byte *data; - gdb_byte *ptr; - struct auxv_info *info; - - info = get_auxv_inferior_data (ops); + auxv_info *info = get_auxv_inferior_data (ops); - data = info->data; - ptr = data; + if (!info->data) + return -1; - if (info->length <= 0) - return info->length; + gdb_byte *data = info->data->data (); + gdb_byte *ptr = data; + size_t len = info->data->size (); while (1) - switch (target_auxv_parse (ops, &ptr, data + info->length, &type, &val)) + switch (target_auxv_parse (ops, &ptr, data + len, &type, &val)) { case 1: /* Here's an entry, check it. */ if (type == match) @@ -528,19 +522,17 @@ fprint_target_auxv (struct ui_file *file, struct target_ops *ops) { struct gdbarch *gdbarch = target_gdbarch (); CORE_ADDR type, val; - gdb_byte *data; - gdb_byte *ptr; - struct auxv_info *info; int ents = 0; + auxv_info *info = get_auxv_inferior_data (ops); - info = get_auxv_inferior_data (ops); + if (!info->data) + return -1; - data = info->data; - ptr = data; - if (info->length <= 0) - return info->length; + gdb_byte *data = info->data->data (); + gdb_byte *ptr = data; + size_t len = info->data->size (); - while (target_auxv_parse (ops, &ptr, data + info->length, &type, &val) > 0) + while (target_auxv_parse (ops, &ptr, data + len, &type, &val) > 0) { gdbarch_print_auxv_entry (gdbarch, file, type, val); ++ents; diff --git a/gdb/avr-tdep.c b/gdb/avr-tdep.c index c44a3aa..2b9be5e 100644 --- a/gdb/avr-tdep.c +++ b/gdb/avr-tdep.c @@ -1549,21 +1549,15 @@ avr_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) static void avr_io_reg_read_command (const char *args, int from_tty) { - LONGEST bufsiz = 0; - gdb_byte *buf; - const char *bufstr; char query[400]; - const char *p; unsigned int nreg = 0; unsigned int val; - int i, j, k, step; /* Find out how many io registers the target has. */ - bufsiz = target_read_alloc (¤t_target, TARGET_OBJECT_AVR, - "avr.io_reg", &buf); - bufstr = (const char *) buf; + gdb::optional buf + = target_read_alloc (¤t_target, TARGET_OBJECT_AVR, "avr.io_reg"); - if (bufsiz <= 0) + if (!buf) { fprintf_unfiltered (gdb_stderr, _("ERR: info io_registers NOT supported " @@ -1571,36 +1565,42 @@ avr_io_reg_read_command (const char *args, int from_tty) return; } + const char *bufstr = (const char *) buf->data (); + if (sscanf (bufstr, "%x", &nreg) != 1) { fprintf_unfiltered (gdb_stderr, _("Error fetching number of io registers\n")); - xfree (buf); return; } - xfree (buf); - reinitialize_more_filter (); printf_unfiltered (_("Target has %u io registers:\n\n"), nreg); /* only fetch up to 8 registers at a time to keep the buffer small */ - step = 8; + int step = 8; - for (i = 0; i < nreg; i += step) + for (int i = 0; i < nreg; i += step) { /* how many registers this round? */ - j = step; + int j = step; if ((i+j) >= nreg) j = nreg - i; /* last block is less than 8 registers */ snprintf (query, sizeof (query) - 1, "avr.io_reg:%x,%x", i, j); - bufsiz = target_read_alloc (¤t_target, TARGET_OBJECT_AVR, - query, &buf); + buf = target_read_alloc (¤t_target, TARGET_OBJECT_AVR, query); + + if (!buf) + { + fprintf_unfiltered (gdb_stderr, + _("ERR: error reading avr.io_reg:%x,%x\n"), + i, j); + return; + } - p = (const char *) buf; - for (k = i; k < (i + j); k++) + const char *p = (const char *) buf->data (); + for (int k = i; k < (i + j); k++) { if (sscanf (p, "%[^,],%x;", query, &val) == 2) { @@ -1612,8 +1612,6 @@ avr_io_reg_read_command (const char *args, int from_tty) break; } } - - xfree (buf); } } diff --git a/gdb/common/byte-vector.h b/gdb/common/byte-vector.h index 03c4eb8..22a67b8 100644 --- a/gdb/common/byte-vector.h +++ b/gdb/common/byte-vector.h @@ -56,6 +56,7 @@ namespace gdb { and providing the whole std::vector API, if you end up needing it. */ using byte_vector = gdb::def_vector; +using char_vector = gdb::def_vector; } /* namespace gdb */ diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c index b4b87dd..7ea56f0 100644 --- a/gdb/linux-tdep.c +++ b/gdb/linux-tdep.c @@ -1408,47 +1408,41 @@ linux_spu_make_corefile_notes (bfd *obfd, char *note_data, int *note_size) }; enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); - gdb_byte *spu_ids; - LONGEST i, j, size; /* Determine list of SPU ids. */ - size = target_read_alloc (¤t_target, TARGET_OBJECT_SPU, - NULL, &spu_ids); + gdb::optional + spu_ids = target_read_alloc (¤t_target, TARGET_OBJECT_SPU, NULL); + + if (!spu_ids) + return nullptr; /* Generate corefile notes for each SPU file. */ - for (i = 0; i < size; i += 4) + for (size_t i = 0; i < spu_ids->size (); i += 4) { - int fd = extract_unsigned_integer (spu_ids + i, 4, byte_order); + int fd = extract_unsigned_integer (spu_ids->data () + i, 4, byte_order); - for (j = 0; j < sizeof (spu_files) / sizeof (spu_files[0]); j++) + for (size_t j = 0; j < sizeof (spu_files) / sizeof (spu_files[0]); j++) { char annex[32], note_name[32]; - gdb_byte *spu_data; - LONGEST spu_len; xsnprintf (annex, sizeof annex, "%d/%s", fd, spu_files[j]); - spu_len = target_read_alloc (¤t_target, TARGET_OBJECT_SPU, - annex, &spu_data); - if (spu_len > 0) + gdb::optional spu_data + = target_read_alloc (¤t_target, TARGET_OBJECT_SPU, annex); + + if (spu_data && !spu_data->empty ()) { xsnprintf (note_name, sizeof note_name, "SPU/%s", annex); note_data = elfcore_write_note (obfd, note_data, note_size, note_name, NT_SPU, - spu_data, spu_len); - xfree (spu_data); + spu_data->data (), + spu_data->size ()); if (!note_data) - { - xfree (spu_ids); - return NULL; - } + return nullptr; } } } - if (size > 0) - xfree (spu_ids); - return note_data; } @@ -1899,8 +1893,6 @@ linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size) struct linux_corefile_thread_data thread_args; struct elf_internal_linux_prpsinfo prpsinfo; char *note_data = NULL; - gdb_byte *auxv; - int auxv_len; struct thread_info *curr_thr, *signalled_thr, *thr; if (! gdbarch_iterate_over_regset_sections_p (gdbarch)) @@ -1965,13 +1957,13 @@ linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size) return NULL; /* Auxillary vector. */ - auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV, - NULL, &auxv); - if (auxv_len > 0) + gdb::optional auxv = + target_read_alloc (¤t_target, TARGET_OBJECT_AUXV, NULL); + if (auxv && !auxv->empty ()) { note_data = elfcore_write_note (obfd, note_data, note_size, - "CORE", NT_AUXV, auxv, auxv_len); - xfree (auxv); + "CORE", NT_AUXV, auxv->data (), + auxv->size ()); if (!note_data) return NULL; diff --git a/gdb/osdata.c b/gdb/osdata.c index 5c41cb1..50548f7 100644 --- a/gdb/osdata.c +++ b/gdb/osdata.c @@ -163,11 +163,11 @@ std::unique_ptr get_osdata (const char *type) { std::unique_ptr osdata; - gdb::unique_xmalloc_ptr xml = target_get_osdata (type); + gdb::optional xml = target_get_osdata (type); if (xml) { - if (xml.get ()[0] == '\0') + if ((*xml)[0] == '\0') { if (type) warning (_("Empty data returned by target. Wrong osdata type?")); @@ -175,7 +175,7 @@ get_osdata (const char *type) warning (_("Empty type list returned by target. No type data?")); } else - osdata = osdata_parse (xml.get ()); + osdata = osdata_parse (xml->data ()); } if (osdata == NULL) diff --git a/gdb/remote.c b/gdb/remote.c index 501c443..649e3d3 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -3151,13 +3151,13 @@ remote_get_threads_with_qxfer (struct target_ops *ops, #if defined(HAVE_LIBEXPAT) if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE) { - gdb::unique_xmalloc_ptr xml + gdb::optional xml = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL); - if (xml != NULL && *xml != '\0') + if (xml && (*xml)[0] != '\0') { gdb_xml_parse_quick (_("threads"), "threads.dtd", - threads_elements, xml.get (), context); + threads_elements, xml->data (), context); } return 1; @@ -10806,11 +10806,11 @@ static std::vector remote_memory_map (struct target_ops *ops) { std::vector result; - gdb::unique_xmalloc_ptr text + gdb::optional text = target_read_stralloc (¤t_target, TARGET_OBJECT_MEMORY_MAP, NULL); if (text) - result = parse_memory_map (text.get ()); + result = parse_memory_map (text->data ()); return result; } @@ -12921,11 +12921,11 @@ remote_set_circular_trace_buffer (struct target_ops *self, int val) static traceframe_info_up remote_traceframe_info (struct target_ops *self) { - gdb::unique_xmalloc_ptr text + gdb::optional text = target_read_stralloc (¤t_target, TARGET_OBJECT_TRACEFRAME_INFO, NULL); - if (text != NULL) - return parse_traceframe_info (text.get ()); + if (text) + return parse_traceframe_info (text->data ()); return NULL; } @@ -13153,10 +13153,10 @@ btrace_sync_conf (const struct btrace_config *conf) static void btrace_read_config (struct btrace_config *conf) { - gdb::unique_xmalloc_ptr xml + gdb::optional xml = target_read_stralloc (¤t_target, TARGET_OBJECT_BTRACE_CONF, ""); - if (xml != NULL) - parse_xml_btrace_conf (conf, xml.get ()); + if (xml) + parse_xml_btrace_conf (conf, xml->data ()); } /* Maybe reopen target btrace. */ @@ -13353,12 +13353,12 @@ remote_read_btrace (struct target_ops *self, (unsigned int) type); } - gdb::unique_xmalloc_ptr xml + gdb::optional xml = target_read_stralloc (¤t_target, TARGET_OBJECT_BTRACE, annex); - if (xml == NULL) + if (!xml) return BTRACE_ERR_UNKNOWN; - parse_xml_btrace (btrace, xml.get ()); + parse_xml_btrace (btrace, xml->data ()); return BTRACE_ERR_NONE; } @@ -13392,7 +13392,7 @@ remote_load (struct target_ops *self, const char *name, int from_tty) static char * remote_pid_to_exec_file (struct target_ops *self, int pid) { - static gdb::unique_xmalloc_ptr filename; + static gdb::optional filename; struct inferior *inf; char *annex = NULL; @@ -13415,7 +13415,7 @@ remote_pid_to_exec_file (struct target_ops *self, int pid) filename = target_read_stralloc (¤t_target, TARGET_OBJECT_EXEC_FILE, annex); - return filename.get (); + return filename ? filename->data () : nullptr; } /* Implement the to_can_do_single_step target_ops method. */ diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c index 5339f11..608a702 100644 --- a/gdb/solib-aix.c +++ b/gdb/solib-aix.c @@ -279,10 +279,10 @@ solib_aix_get_library_list (struct inferior *inf, const char *warning_msg) if (data->library_list != NULL) return data->library_list; - gdb::unique_xmalloc_ptr library_document + gdb::optional library_document = target_read_stralloc (¤t_target, TARGET_OBJECT_LIBRARIES_AIX, NULL); - if (library_document == NULL && warning_msg != NULL) + if (!library_document && warning_msg != NULL) { warning (_("%s (failed to read TARGET_OBJECT_LIBRARIES_AIX)"), warning_msg); @@ -292,9 +292,9 @@ solib_aix_get_library_list (struct inferior *inf, const char *warning_msg) if (solib_aix_debug) fprintf_unfiltered (gdb_stdlog, "DEBUG: TARGET_OBJECT_LIBRARIES_AIX = \n%s\n", - library_document.get ()); + library_document->data ()); - data->library_list = solib_aix_parse_libraries (library_document.get ()); + data->library_list = solib_aix_parse_libraries (library_document->data ()); if (data->library_list == NULL && warning_msg != NULL) { warning (_("%s (missing XML support?)"), warning_msg); diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c index d038ccc..dc40bb9 100644 --- a/gdb/solib-dsbt.c +++ b/gdb/solib-dsbt.c @@ -224,10 +224,10 @@ dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map) /* Decode int_elf32_dsbt_loadmap from BUF. */ static struct int_elf32_dsbt_loadmap * -decode_loadmap (gdb_byte *buf) +decode_loadmap (const gdb_byte *buf) { enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); - struct ext_elf32_dsbt_loadmap *ext_ldmbuf; + const struct ext_elf32_dsbt_loadmap *ext_ldmbuf; struct int_elf32_dsbt_loadmap *int_ldmbuf; int version, seg, nsegs; @@ -278,7 +278,6 @@ decode_loadmap (gdb_byte *buf) byte_order); } - xfree (ext_ldmbuf); return int_ldmbuf; } @@ -292,26 +291,26 @@ static struct dsbt_info *get_dsbt_info (void); static void dsbt_get_initial_loadmaps (void) { - gdb_byte *buf; struct dsbt_info *info = get_dsbt_info (); + gdb::optional buf + = target_read_alloc (¤t_target, TARGET_OBJECT_FDPIC, "exec"); - if (0 >= target_read_alloc (¤t_target, TARGET_OBJECT_FDPIC, - "exec", &buf)) + if (!buf || buf->empty ()) { info->exec_loadmap = NULL; error (_("Error reading DSBT exec loadmap")); } - info->exec_loadmap = decode_loadmap (buf); + info->exec_loadmap = decode_loadmap (buf->data ()); if (solib_dsbt_debug) dsbt_print_loadmap (info->exec_loadmap); - if (0 >= target_read_alloc (¤t_target, TARGET_OBJECT_FDPIC, - "interp", &buf)) + buf = target_read_alloc (¤t_target, TARGET_OBJECT_FDPIC, "exec"); + if (!buf || buf->empty ()) { info->interp_loadmap = NULL; error (_("Error reading DSBT interp loadmap")); } - info->interp_loadmap = decode_loadmap (buf); + info->interp_loadmap = decode_loadmap (buf->data ()); if (solib_dsbt_debug) dsbt_print_loadmap (info->interp_loadmap); } diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c index 29e74da..07df197 100644 --- a/gdb/solib-svr4.c +++ b/gdb/solib-svr4.c @@ -1274,13 +1274,13 @@ svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list, gdb_assert (annex == NULL || target_augmented_libraries_svr4_read ()); /* Fetch the list of shared libraries. */ - gdb::unique_xmalloc_ptr svr4_library_document + gdb::optional svr4_library_document = target_read_stralloc (¤t_target, TARGET_OBJECT_LIBRARIES_SVR4, annex); - if (svr4_library_document == NULL) + if (!svr4_library_document) return 0; - return svr4_parse_libraries (svr4_library_document.get (), list); + return svr4_parse_libraries (svr4_library_document->data (), list); } #else diff --git a/gdb/solib-target.c b/gdb/solib-target.c index 0968e49..7f96e80 100644 --- a/gdb/solib-target.c +++ b/gdb/solib-target.c @@ -257,13 +257,13 @@ solib_target_current_sos (void) int ix; /* Fetch the list of shared libraries. */ - gdb::unique_xmalloc_ptr library_document + gdb::optional library_document = target_read_stralloc (¤t_target, TARGET_OBJECT_LIBRARIES, NULL); - if (library_document == NULL) + if (!library_document) return NULL; /* Parse the list. */ - library_list = solib_target_parse_libraries (library_document.get ()); + library_list = solib_target_parse_libraries (library_document->data ()); if (library_list == NULL) return NULL; diff --git a/gdb/target.c b/gdb/target.c index 84f5228..4d0fed7 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -48,6 +48,7 @@ #include #include "byte-vector.h" #include "terminal.h" +#include static void generic_tls_error (void) ATTRIBUTE_NORETURN; @@ -1936,18 +1937,17 @@ target_write (struct target_ops *ops, NULL, NULL); } -/* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return - the size of the transferred data. PADDING additional bytes are - available in *BUF_P. This is a helper function for - target_read_alloc; see the declaration of that function for more - information. */ +/* Help for target_read_alloc and target_read_stralloc. See their comments + for details. */ -static LONGEST +template +gdb::optional> target_read_alloc_1 (struct target_ops *ops, enum target_object object, - const char *annex, gdb_byte **buf_p, int padding) + const char *annex) { - size_t buf_alloc, buf_pos; - gdb_byte *buf; + gdb::def_vector buf; + size_t buf_pos = 0; + const int chunk = 4096; /* This function does not have a length parameter; it reads the entire OBJECT). Also, it doesn't support objects fetched partly @@ -1958,82 +1958,64 @@ target_read_alloc_1 (struct target_ops *ops, enum target_object object, /* Start by reading up to 4K at a time. The target will throttle this number down if necessary. */ - buf_alloc = 4096; - buf = (gdb_byte *) xmalloc (buf_alloc); - buf_pos = 0; while (1) { ULONGEST xfered_len; enum target_xfer_status status; - status = target_read_partial (ops, object, annex, &buf[buf_pos], - buf_pos, buf_alloc - buf_pos - padding, + buf.resize (buf_pos + chunk); + + status = target_read_partial (ops, object, annex, + (gdb_byte *) &buf[buf_pos], + buf_pos, chunk, &xfered_len); if (status == TARGET_XFER_EOF) { /* Read all there was. */ - if (buf_pos == 0) - xfree (buf); - else - *buf_p = buf; - return buf_pos; + buf.resize (buf_pos); + return buf; } else if (status != TARGET_XFER_OK) { /* An error occurred. */ - xfree (buf); - return TARGET_XFER_E_IO; + return {}; } buf_pos += xfered_len; - /* If the buffer is filling up, expand it. */ - if (buf_alloc < buf_pos * 2) - { - buf_alloc *= 2; - buf = (gdb_byte *) xrealloc (buf, buf_alloc); - } - QUIT; } } -/* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return - the size of the transferred data. See the declaration in "target.h" - function for more information about the return value. */ +/* See target.h */ -LONGEST +gdb::optional target_read_alloc (struct target_ops *ops, enum target_object object, - const char *annex, gdb_byte **buf_p) + const char *annex) { - return target_read_alloc_1 (ops, object, annex, buf_p, 0); + return target_read_alloc_1 (ops, object, annex); } /* See target.h. */ -gdb::unique_xmalloc_ptr +gdb::optional target_read_stralloc (struct target_ops *ops, enum target_object object, const char *annex) { - gdb_byte *buffer; - char *bufstr; - LONGEST i, transferred; - - transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1); - bufstr = (char *) buffer; - - if (transferred < 0) - return NULL; + gdb::optional buf + = target_read_alloc_1 (ops, object, annex); - if (transferred == 0) - return gdb::unique_xmalloc_ptr (xstrdup ("")); + if (!buf) + return {}; - bufstr[transferred] = 0; + if (buf->back () != '\0') + buf->push_back ('\0'); /* Check for embedded NUL bytes; but allow trailing NULs. */ - for (i = strlen (bufstr); i < transferred; i++) - if (bufstr[i] != 0) + for (auto it = std::find (buf->begin (), buf->end (), '\0'); + it != buf->end (); it++) + if (*it != '\0') { warning (_("target object %d, annex %s, " "contained unexpected null characters"), @@ -2041,7 +2023,7 @@ target_read_stralloc (struct target_ops *ops, enum target_object object, break; } - return gdb::unique_xmalloc_ptr (bufstr); + return buf; } /* Memory transfer methods. */ @@ -2743,7 +2725,7 @@ target_supports_multi_process (void) /* See target.h. */ -gdb::unique_xmalloc_ptr +gdb::optional target_get_osdata (const char *type) { struct target_ops *t; @@ -2757,7 +2739,7 @@ target_get_osdata (const char *type) t = find_default_run_target ("get OS data"); if (!t) - return NULL; + return {}; return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type); } diff --git a/gdb/target.h b/gdb/target.h index fd6c30b..c0bd379 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -327,29 +327,26 @@ LONGEST target_write_with_progress (struct target_ops *ops, void (*progress) (ULONGEST, void *), void *baton); -/* Wrapper to perform a full read of unknown size. OBJECT/ANNEX will - be read using OPS. The return value will be -1 if the transfer - fails or is not supported; 0 if the object is empty; or the length - of the object otherwise. If a positive value is returned, a - sufficiently large buffer will be allocated using xmalloc and - returned in *BUF_P containing the contents of the object. +/* Wrapper to perform a full read of unknown size. OBJECT/ANNEX will be read + using OPS. The return value will be uninstantiated if the transfer fails or + is not supported. This method should be used for objects sufficiently small to store in a single xmalloc'd buffer, when no fixed bound on the object's size is known in advance. Don't try to read TARGET_OBJECT_MEMORY through this function. */ -extern LONGEST target_read_alloc (struct target_ops *ops, - enum target_object object, - const char *annex, gdb_byte **buf_p); +extern gdb::optional target_read_alloc + (struct target_ops *ops, enum target_object object, const char *annex); -/* Read OBJECT/ANNEX using OPS. The result is NUL-terminated and - returned as a string. If an error occurs or the transfer is - unsupported, NULL is returned. Empty objects are returned as - allocated but empty strings. A warning is issued if the result - contains any embedded NUL bytes. */ +/* Read OBJECT/ANNEX using OPS. The result is a NUL-terminated character vector + (therefore usable as a NUL-terminated string). If an error occurs or the + transfer is unsupported, the return value will be uninstantiated. Empty + objects are returned as allocated but empty strings. Therefore, on success, + the returned vector is guaranteed to have at least one element. A warning is + issued if the result contains any embedded NUL bytes. */ -extern gdb::unique_xmalloc_ptr target_read_stralloc +extern gdb::optional target_read_stralloc (struct target_ops *ops, enum target_object object, const char *annex); /* See target_ops->to_xfer_partial. */ @@ -2384,15 +2381,11 @@ extern struct target_ops *find_target_beneath (struct target_ops *); struct target_ops *find_target_at (enum strata stratum); -/* Read OS data object of type TYPE from the target, and return it in - XML format. The result is NUL-terminated and returned as a string. - If an error occurs or the transfer is unsupported, NULL is - returned. Empty objects are returned as allocated but empty - strings. */ +/* Read OS data object of type TYPE from the target, and return it in XML + format. The return value follows the same rules as target_read_stralloc. */ -extern gdb::unique_xmalloc_ptr target_get_osdata (const char *type); +extern gdb::optional target_get_osdata (const char *type); - /* Stuff that should be shared among the various remote targets. */ /* Debugging level. 0 is off, and non-zero values mean to print some debug diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c index 30fe206..cf97353 100644 --- a/gdb/tracepoint.c +++ b/gdb/tracepoint.c @@ -3910,23 +3910,19 @@ static struct value * sdata_make_value (struct gdbarch *gdbarch, struct internalvar *var, void *ignore) { - LONGEST size; - gdb_byte *buf; - /* We need to read the whole object before we know its size. */ - size = target_read_alloc (¤t_target, - TARGET_OBJECT_STATIC_TRACE_DATA, - NULL, &buf); - if (size >= 0) + gdb::optional buf + = target_read_alloc (¤t_target, TARGET_OBJECT_STATIC_TRACE_DATA, + NULL); + if (buf) { struct value *v; struct type *type; type = init_vector_type (builtin_type (gdbarch)->builtin_true_char, - size); + buf->size ()); v = allocate_value (type); - memcpy (value_contents_raw (v), buf, size); - xfree (buf); + memcpy (value_contents_raw (v), buf->data (), buf->size ()); return v; } else diff --git a/gdb/xml-support.c b/gdb/xml-support.c index 2547882..3775e2c 100644 --- a/gdb/xml-support.c +++ b/gdb/xml-support.c @@ -793,13 +793,13 @@ xinclude_start_include (struct gdb_xml_parser *parser, gdb_xml_error (parser, _("Maximum XInclude depth (%d) exceeded"), MAX_XINCLUDE_DEPTH); - gdb::unique_xmalloc_ptr text = data->fetcher (href, - data->fetcher_baton); - if (text == NULL) + gdb::optional text + = data->fetcher (href, data->fetcher_baton); + if (!text) gdb_xml_error (parser, _("Could not load XML document \"%s\""), href); if (!xml_process_xincludes (data->output, parser->name (), - text.get (), data->fetcher, + text->data (), data->fetcher, data->fetcher_baton, data->include_depth + 1)) gdb_xml_error (parser, _("Parsing \"%s\" failed"), href); @@ -971,7 +971,7 @@ show_debug_xml (struct ui_file *file, int from_tty, fprintf_filtered (file, _("XML debugging is %s.\n"), value); } -gdb::unique_xmalloc_ptr +gdb::optional xml_fetch_content_from_file (const char *filename, void *baton) { const char *dirname = (const char *) baton; @@ -990,7 +990,7 @@ xml_fetch_content_from_file (const char *filename, void *baton) file = gdb_fopen_cloexec (filename, FOPEN_RT); if (file == NULL) - return NULL; + return {}; /* Read in the whole file. */ @@ -1001,16 +1001,16 @@ xml_fetch_content_from_file (const char *filename, void *baton) len = ftell (file.get ()); rewind (file.get ()); - gdb::unique_xmalloc_ptr text ((char *) xmalloc (len + 1)); + gdb::char_vector text (len + 1); - if (fread (text.get (), 1, len, file.get ()) != len + if (fread (text.data (), 1, len, file.get ()) != len || ferror (file.get ())) { warning (_("Read error from \"%s\""), filename); - return NULL; + return {}; } - text.get ()[len] = '\0'; + text.back () = '\0'; return text; } diff --git a/gdb/xml-support.h b/gdb/xml-support.h index 5947623..3abae70 100644 --- a/gdb/xml-support.h +++ b/gdb/xml-support.h @@ -24,6 +24,7 @@ #include "gdb_obstack.h" #include "vec.h" #include "xml-utils.h" +#include "common/byte-vector.h" struct gdb_xml_parser; struct gdb_xml_element; @@ -52,8 +53,8 @@ extern const char *xml_builtin[][2]; /* Callback to fetch a new XML file, based on the provided HREF. */ -typedef gdb::unique_xmalloc_ptr (*xml_fetch_another) (const char *href, - void *baton); +typedef gdb::optional (*xml_fetch_another) (const char *href, + void *baton); /* Append the expansion of TEXT after processing tags in RESULT. FETCHER will be called (with FETCHER_BATON) to retrieve @@ -231,9 +232,10 @@ ULONGEST gdb_xml_parse_ulongest (struct gdb_xml_parser *parser, const char *value); /* Open FILENAME, read all its text into memory, close it, and return - the text. If something goes wrong, return NULL and warn. */ + the text. If something goes wrong, return an uninstantiated optional + and warn. */ -extern gdb::unique_xmalloc_ptr xml_fetch_content_from_file +extern gdb::optional xml_fetch_content_from_file (const char *filename, void *baton); #endif diff --git a/gdb/xml-syscall.c b/gdb/xml-syscall.c index 0ba6d44..bf17642 100644 --- a/gdb/xml-syscall.c +++ b/gdb/xml-syscall.c @@ -305,12 +305,12 @@ syscall_parse_xml (const char *document, xml_fetch_another fetcher, static struct syscalls_info * xml_init_syscalls_info (const char *filename) { - gdb::unique_xmalloc_ptr full_file + gdb::optional full_file = xml_fetch_content_from_file (filename, gdb_datadir); - if (full_file == NULL) + if (!full_file) return NULL; - return syscall_parse_xml (full_file.get (), + return syscall_parse_xml (full_file->data (), xml_fetch_content_from_file, (void *) ldirname (filename).c_str ()); } diff --git a/gdb/xml-tdesc.c b/gdb/xml-tdesc.c index 9190d5f..1c3409d 100644 --- a/gdb/xml-tdesc.c +++ b/gdb/xml-tdesc.c @@ -668,15 +668,15 @@ tdesc_parse_xml (const char *document, xml_fetch_another fetcher, const struct target_desc * file_read_description_xml (const char *filename) { - gdb::unique_xmalloc_ptr tdesc_str + gdb::optional tdesc_str = xml_fetch_content_from_file (filename, NULL); - if (tdesc_str == NULL) + if (!tdesc_str) { warning (_("Could not open \"%s\""), filename); return NULL; } - return tdesc_parse_xml (tdesc_str.get (), xml_fetch_content_from_file, + return tdesc_parse_xml (tdesc_str->data (), xml_fetch_content_from_file, (void *) ldirname (filename).c_str ()); } @@ -687,7 +687,7 @@ file_read_description_xml (const char *filename) is "target.xml". Other calls may be performed for the DTD or for . */ -static gdb::unique_xmalloc_ptr +static gdb::optional fetch_available_features_from_target (const char *name, void *baton_) { struct target_ops *ops = (struct target_ops *) baton_; @@ -706,12 +706,12 @@ fetch_available_features_from_target (const char *name, void *baton_) const struct target_desc * target_read_description_xml (struct target_ops *ops) { - gdb::unique_xmalloc_ptr tdesc_str + gdb::optional tdesc_str = fetch_available_features_from_target ("target.xml", ops); - if (tdesc_str == NULL) + if (!tdesc_str) return NULL; - return tdesc_parse_xml (tdesc_str.get (), + return tdesc_parse_xml (tdesc_str->data (), fetch_available_features_from_target, ops); } @@ -735,15 +735,15 @@ target_fetch_description_xml (struct target_ops *ops) return {}; #else - gdb::unique_xmalloc_ptr + gdb::optional tdesc_str = fetch_available_features_from_target ("target.xml", ops); - if (tdesc_str == NULL) + if (!tdesc_str) return {}; std::string output; if (!xml_process_xincludes (output, _("target description"), - tdesc_str.get (), + tdesc_str->data (), fetch_available_features_from_target, ops, 0)) { warning (_("Could not load XML target description; ignoring"));