From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id I3CZD3xaAmIIWgAAWB0awg (envelope-from ) for ; Tue, 08 Feb 2022 06:56:44 -0500 Received: by simark.ca (Postfix, from userid 112) id 2DA071F3C6; Tue, 8 Feb 2022 06:56:44 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 14A291EE1F for ; Tue, 8 Feb 2022 06:56:43 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 04F9D385841E for ; Tue, 8 Feb 2022 11:56:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 04F9D385841E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1644321402; bh=vAkniY4BEO5p4ex4fZrEhp0BqWbS99acuHtymQYQeDw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=VeGoYhMtiH4GAIdK0KtTs4NlK24JLOsQFycQEYiqAC1Ic/XYB1e7Wf42CXWUTpiog ph/tBTTU3cON7R7G+JJKWcWRqtCsOqq2I4e1aBCAw5ofOAtyS5mCz8uR2nPqmvhiDA 22fE2jq0DcKNOth1Rbc3tDqWz/QmL4ePTlHyivzw= Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2070.outbound.protection.outlook.com [40.107.243.70]) by sourceware.org (Postfix) with ESMTPS id 2A65D3858C27 for ; Tue, 8 Feb 2022 11:56:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2A65D3858C27 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=e5arT1lb7TFQ0l/FtTRL6L5jGDsj4+7you8uygIOKUaULLYb7eatxHE7oW0pXfWKVfXgnNyL/paz0aFdWrafmxvJ573BEPcjJKqadFr5EG4OQsn9cXM05At5ZptcnOPRx3TxMDbxeoWAbTGx/h5bj74QGm1BQSD2nqoRJgmXaI8SigVlBrDsOudPrmDh4QvNc6kV+Sr4nDktkc69wEoVahNGsgA7ys4FckWyoilEefUW6SIBQLCjxZARDh8cTMy1K2rjVKcoE0KGlQm622S7848liQM29TvVRCOcr2bh3RFU+N2/Z6ugqKVvtjy63YqjsjqI6NCW2Xh2kqZ0p8j8OQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=vAkniY4BEO5p4ex4fZrEhp0BqWbS99acuHtymQYQeDw=; b=TYer+kbRjoxJHLAddvgjKVorPXlgI2UZ949ZFtdMp7k7bJJDwLTlPQfNwn2r2SWAV+e2u5de+/1HAkDYM/uSNP4QdSMAb9SLfXvDeoUL+hZH+Fjx9h9Ay+F49s7wDouXHh/55EfSGJbWbkOT/nDUZ4NWp58e3w3AAbk4AlDl1D+kbLCpCQ5XjKkfb8ZWLp4tXxl8FAyWH40oihQDF4DMQqIzCa1gczs/zazLqQQykUJQZmMtuAEmQQ+NvMjUOqR6LAs9uxoZU+/DtjF9Yfm1t6NjeVRL6Th4YsAbfWWr9zK0r8H3WCcjpLBsqDEoGS5fzRmTlVvxyLAol8JOy2Srtg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from DM6PR17MB3113.namprd17.prod.outlook.com (2603:10b6:5:6::10) by DM5PR17MB1945.namprd17.prod.outlook.com (2603:10b6:3:89::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4975.11; Tue, 8 Feb 2022 11:56:18 +0000 Received: from DM6PR17MB3113.namprd17.prod.outlook.com ([fe80::78f9:3a56:7d30:e2c9]) by DM6PR17MB3113.namprd17.prod.outlook.com ([fe80::78f9:3a56:7d30:e2c9%6]) with mapi id 15.20.4951.019; Tue, 8 Feb 2022 11:56:18 +0000 To: gdb-patches@sourceware.org Subject: [RFC PATCH] gdb: create symbols for JITed code Date: Tue, 8 Feb 2022 11:56:10 +0000 Message-Id: <20220208115610.16574-1-jan.vrany@labware.com> X-Mailer: git-send-email 2.30.2 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: LO4P123CA0495.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1ab::14) To DM6PR17MB3113.namprd17.prod.outlook.com (2603:10b6:5:6::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 61cb7b17-e69a-4ca1-c4bf-08d9eafa03c3 X-MS-TrafficTypeDiagnostic: DM5PR17MB1945:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3383; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: t/Y6tg9yNYm0ifYqqlEq4/YpwFGCiWYZMplLr7ITS5A5ecLO60/ABBgpSCtFJWRuPK2DRmSNwN5Zq/3LYZGiXEPnA2ySfkrVYrIPgKsTnJ/+jpo35m2Gh64qBvSeJlBhAKK/06Zguxm9F0gLHamLvR/hZNr0RlA/sdmwOXcmwhVJNgT5Pg9wPQGoVVoHReFSw8fPrh0lmKjjTIngoB8DEF4d4ilI1UYuuTyhWhm32E0CyA6qBjFcYU6hUxc98w/wL4kh7xzBMpd9z1+PwCXdVcFVWF2AMswdhUwXB+e86ffyJdxxN9gMh5XxSu1e6L0VymUtbTyNKRs7cKFk9p3NAASeFLm+7nSqhcj03x3HruaIFwG2dUk8ajwWSg9IWu/ZthBRB1dSoruHEm0ILRbgYLWN2ENCJh+lnvLcuEpSL5JrtU2iDaSQA7O0J/KWjQWoUTmxFxwGFg8iOF7cYRW+mYp3yM1jnvG3dB5UecDoeBOQRQXIcHnqjiyC9bj8nBq/qi2UYzH6rFhjmKymyHhWPZvUZshbKZ+37d4MBL7VVA2apOLlVr0JGx90h/uwi3Lx/ydzW9xwp4eMKj3zc12y/r3UC2xDeHnxoiq1EnBZDoUaCfJQ685VhJFzwGfOQ/Xk4mtkqxG/mGJ+XPQFpmMghAsy54edRGD0kOVfggaXL9lGi4Aus0sUO56fwBpsK2tg5oxYsYImtaaDpdj8qY0/Ww== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DM6PR17MB3113.namprd17.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(83380400001)(6486002)(186003)(2616005)(316002)(26005)(2906002)(6916009)(1076003)(107886003)(66946007)(6506007)(6512007)(5660300002)(6666004)(44832011)(86362001)(38350700002)(38100700002)(36756003)(508600001)(4326008)(8936002)(8676002)(66476007)(66556008)(52116002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?1Ufok0x2niVSrHAN6MOocfk1cHXQQWg4q9P0huG5hLdMqp8yCmCVTKpdPiOK?= =?us-ascii?Q?4YJ8X/15PzIne8CZIB3ImC0k9l2nxFlwz87IXt9VuenaxHi97AP4acRilQu/?= =?us-ascii?Q?PP52bewuMgelsI599F6Vv4O/Pns5V+wAnDkKMKBjcE3/qR3MMDwTQuw5MK91?= =?us-ascii?Q?AzDOwI1dSP5SBZn70fcYmDl4YJVatScZULZWmC4Io75/CEcGHJ8yDaVs+7og?= =?us-ascii?Q?WDLkjQ8vGQuPjv1ZCp5k1740yJQ5F7J9HeIb3niyucYBfsGqAtyPVOvSnvnQ?= =?us-ascii?Q?UmwOZvrLP61N1PNvgUTuVjMKFxzse9K3EmT0NTbXHworFCVEzgF8YKLQT2VS?= =?us-ascii?Q?mXq1hcZ6u1mgnHlmteEoht3sKjHHeWjT0u/xY+VEq6IqfCo+EZBDSeQW/RZJ?= =?us-ascii?Q?Si9Ii8S6zkBFfG+5e40jgpw7bOX+COO0xrBWvDiAnF5ERM1YY935CbeSt9vX?= =?us-ascii?Q?PLxpSXJ99+Qe7NhjYmTmBOLgaFrYHiduxQbYAjnESrdnMPBCggcSH68k2673?= =?us-ascii?Q?t6ER7RVpY7Ff7Tcxh5tyT9/Zpp+JGfWpotdr50p30xTEAwIociTsAr/mBNUk?= =?us-ascii?Q?WQae+dC9daTzNtk+hH5VJaAxu66zZ+8EqJcjzeX752CqabmWx2KMwcAMqqll?= =?us-ascii?Q?sURlyavB0sTUKZoKNW72wr/AxRu12FkTKOxUe+fc7YHW3WnWLXUdcpzf8yiQ?= =?us-ascii?Q?7CAPX3Jk9FxNY70bljJBqi/I+0LPvTfBTQkozT6bpPeCcSDxEOxLYmj0TeUy?= =?us-ascii?Q?14pNh5wZ7GQodxnPT3U1k7u/CCVWROku1pTumO34NNavrSqDV68tvplpwKyu?= =?us-ascii?Q?Vrl/1oDI1MxvafGzdcpa1atokbgMcbiNHXc4QBE81pNS/BVAlK3HORvkoege?= =?us-ascii?Q?mWIfwF9oVkOKoNf0frtLkkmqJc0x5nB66dTii4VyW0VRyo+899WIJ/Ok/qDc?= =?us-ascii?Q?qPwYF0nidoOj5EGhaq227CY0BiuK+rfarADdznYvpuMhARi5sQudotHHN/eH?= =?us-ascii?Q?HWDvtc5K2nKuG7IRmCC1kNP/17L06ArN3t4Hl8r33f3xnLQMG6qGjuoti8/5?= =?us-ascii?Q?cGPRfW28E2jGMACsxtfXFruBCd+ACkw8vAaZovmfXwdaC4l16iuvBH3//1az?= =?us-ascii?Q?meuUxh2STDRNr4y7iacl3BLtXJi1EzgYHDpGrqmrUxTCCW1PxwDRR/OcOqn+?= =?us-ascii?Q?Bpc4TMQ75vNipB/Ykeg3M7ZVe3zbT8IdXbqeCisaS2/xrBkIMXCGvQiH3LD2?= =?us-ascii?Q?wmfLYHruiuAIM6ZF3Sf2kJr+0mOHydGyLY421CNXhhwaf70mWT6uw6lB7IYz?= =?us-ascii?Q?LgKLI2Rmx6DvJpnmEOxhG3UHhUrHThMmUk50C1yd4NVN6gdzoqS1uaRcg5cD?= =?us-ascii?Q?rZkCg7ODI633hAByty1K48N7Kg/9Z4q1HztjojKe7JKRIOi58vHrpwBGtpW2?= =?us-ascii?Q?taX4KNKtq/K2tbxrjpxCw23jVx8VbqMwgF9fQvEz5LxB0BavMCJCALU38+oX?= =?us-ascii?Q?k/YTPloEOT3AHDxsJKtbPjAsiWqsZ8kW8dx41AUZxScXPQ9xcWj98rDDmMNT?= =?us-ascii?Q?jSCPhLB253bwIsGYDG4W7OA9nHGxqMvKzC9EC0At97nUuS/Rg8GZHoIr4aGw?= =?us-ascii?Q?A2M/tkDqr2egtZONST8zmQ8=3D?= X-OriginatorOrg: labware.com X-MS-Exchange-CrossTenant-Network-Message-Id: 61cb7b17-e69a-4ca1-c4bf-08d9eafa03c3 X-MS-Exchange-CrossTenant-AuthSource: DM6PR17MB3113.namprd17.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Feb 2022 11:56:17.8489 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b5db0322-1aa0-4c0a-859c-ad0f96966f4c X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: uCBkT047Y11g9N45Gu/4NzLcNILSQZQbH0W8gJkBi7a7pLMcuoEV6QuxxM4X8iz/fhAMhwp651FfSpmRccA5GA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR17MB1945 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Jan Vrany via Gdb-patches Reply-To: Jan Vrany Cc: Jan Vrany Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" While experimenting with JIT reader API I realized that no symbols are created for JITed code. Running jit-reader.exp test, "info symbol" does not show JIT symbol: (gdb) info symbol jit_function_add No symbol "jit_function_add" in current context. Nor are JIT symbols available in Python: (gdb) python print(gdb.lookup_symbol("jit_function_add")) (None, False) This work-in-progress patch fixes some of the issues by adding created symbols to objfile's static block. To make 'info symbol some_jit_symbol' work, I had to add special case to info_symbol_command () since current implementation walks sections and for dynamic (JIT) objfiles, there are no sections. Also, I had to change find_symbol_at_address () to also report LOC_BLOCK symbols, not only LOC_STATIC. I do not pretend I understand GDB's internal symbol structure and how it should be used and whether this is a good direction. Hence this RFC. Later, I'd like to introduce Python API to create (no only JIT) symbols in GDB. TODO: * fix finalize_symtab to allow for more than PENDINGSIZE symbols per JIT objfile * introduce objfile::find_symbol_at_address () and use if from find_symbol_at_address () * test (and fix) JIT PC-to-source mapping and line breakpoints. --- gdb/jit.c | 27 +++++++++++++++++++++++---- gdb/objfiles.h | 6 ++++++ gdb/printcmd.c | 14 +++++++++++++- gdb/symtab.c | 4 ++++ gdb/testsuite/gdb.base/jit-reader.exp | 17 +++++++++++++++++ 5 files changed, 63 insertions(+), 5 deletions(-) diff --git a/gdb/jit.c b/gdb/jit.c index 7819d763ab3..83a8489a1b5 100644 --- a/gdb/jit.c +++ b/gdb/jit.c @@ -42,6 +42,7 @@ #include "readline/tilde.h" #include "completer.h" #include +#include "buildsym.h" static std::string jit_reader_dir; @@ -541,8 +542,10 @@ finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile) /* First run over all the gdb_block objects, creating a real block object for each. Simultaneously, keep setting the real_block - fields. */ + fields and create a list of static symbols to put into static block + below */ int block_idx = FIRST_LOCAL_BLOCK; + struct pending static_symbols = { nullptr, 0 }; for (gdb_block &gdb_block_iter : stab->blocks) { struct block *new_block = allocate_block (&objfile->objfile_obstack); @@ -578,6 +581,14 @@ finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile) gdb_block_iter.real_block = new_block; + /* If the block has no parent, it will go to a static block, so + remember it here. */ + if (gdb_block_iter.parent == nullptr) + { + gdb_assert (static_symbols.nsyms < PENDINGSIZE-1); + static_symbols.symbol[static_symbols.nsyms++] = block_name; + } + block_idx++; } @@ -590,8 +601,16 @@ finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile) new_block = (i == GLOBAL_BLOCK ? allocate_global_block (&objfile->objfile_obstack) : allocate_block (&objfile->objfile_obstack)); - BLOCK_MULTIDICT (new_block) - = mdict_create_linear (&objfile->objfile_obstack, NULL); + if (i == STATIC_BLOCK) + { + BLOCK_MULTIDICT (new_block) + = mdict_create_linear (&objfile->objfile_obstack, &static_symbols); + } + else + { + BLOCK_MULTIDICT (new_block) + = mdict_create_linear (&objfile->objfile_obstack, nullptr); + } BLOCK_SUPERBLOCK (new_block) = block_iter; block_iter = new_block; @@ -638,7 +657,7 @@ jit_object_close_impl (struct gdb_symbol_callbacks *cb, priv_data->entry.symfile_addr)); objfile *objfile = objfile::make (nullptr, objfile_name.c_str (), - OBJF_NOT_FILENAME); + OBJF_NOT_FILENAME | OBJF_READNOW); objfile->per_bfd->gdbarch = priv_data->gdbarch; for (gdb_symtab &symtab : obj->symtabs) diff --git a/gdb/objfiles.h b/gdb/objfiles.h index 8bd76705688..c929ebf2548 100644 --- a/gdb/objfiles.h +++ b/gdb/objfiles.h @@ -767,6 +767,12 @@ struct objfile next time. If an objfile does not have the symbols, it will never have them. */ bool skip_jit_symbol_lookup = false; + + /* Return true if this objfile is dynamic (i.e, created by JIT). */ + bool is_dynamic () + { + return this->obfd == nullptr; + } }; /* A deleter for objfile. */ diff --git a/gdb/printcmd.c b/gdb/printcmd.c index 6f9be820b0c..abc6c15dff6 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -1539,6 +1539,7 @@ info_symbol_command (const char *arg, int from_tty) addr = parse_and_eval_address (arg); for (objfile *objfile : current_program_space->objfiles ()) + { ALL_OBJFILE_OSECTIONS (objfile, osect) { /* Only process each object file once, even if there's a separate @@ -1610,8 +1611,19 @@ info_symbol_command (const char *arg, int from_tty) else printf_filtered (_("%s in section %s\n"), loc_string, sec_name); - } + } } + if (objfile->is_dynamic ()) + { + symbol *sym = find_symbol_at_address (addr); + if (sym != nullptr) + { + matches = 1; + printf_filtered (_("%s in dynamic symbol file %s\n"), + sym->m_name, objfile_name(objfile)); + } + } + } if (matches == 0) printf_filtered (_("No symbol matches %s.\n"), arg); } diff --git a/gdb/symtab.c b/gdb/symtab.c index 1a39372aad0..4692a37d12c 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -3089,6 +3089,10 @@ find_symbol_at_address (CORE_ADDR address) if (sym->aclass () == LOC_STATIC && SYMBOL_VALUE_ADDRESS (sym) == addr) return sym; + else if (sym->aclass () == LOC_BLOCK + && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= addr + && BLOCK_END (SYMBOL_BLOCK_VALUE (sym)) > addr) + return sym; } } return nullptr; diff --git a/gdb/testsuite/gdb.base/jit-reader.exp b/gdb/testsuite/gdb.base/jit-reader.exp index d94360cd7d9..e06d2daab91 100644 --- a/gdb/testsuite/gdb.base/jit-reader.exp +++ b/gdb/testsuite/gdb.base/jit-reader.exp @@ -124,6 +124,8 @@ proc jit_reader_test {} { gdb_run_cmd gdb_test "" "Program received signal SIGTRAP, .*" "expect SIGTRAP" + + # Test the JIT reader unwinder. with_test_prefix "with jit-reader" { @@ -236,7 +238,22 @@ proc jit_reader_test {} { gdb_test "python print(list(map(lambda objf : objf.filename, gdb.objfiles())))" \ "$any'<< JIT compiled code at $hex >>'$any" \ "python gdb.Objfile.filename" + + gdb_test "python print(gdb.lookup_symbol(\"jit_function_add\"))" \ + "$any$any" \ + "python gdb.lookup_symbol" } + + gdb_test "info symbol jit_function_add" \ + "jit_function_add in dynamic symbol file << JIT compiled code at $hex >>" \ + "info symbol jit_function_add" + + gdb_test "b jit_function_add" \ + "Breakpoint \[0-9]\+ at $hex" \ + "b jit_function_add" + + gdb_test_no_output "del \$bpnum" + } } -- 2.30.2