From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id f/ynL7Lpn2Q+yBIAWB0awg (envelope-from ) for ; Sat, 01 Jul 2023 04:54:10 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=Xkg2rrIu; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id AA4CD1E0BB; Sat, 1 Jul 2023 04:54:10 -0400 (EDT) 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 99A621E00F for ; Sat, 1 Jul 2023 04:54:08 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B3FCA3858418 for ; Sat, 1 Jul 2023 08:54:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B3FCA3858418 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1688201646; bh=jflFKUTaAAU+dvZHVCyLSt7GV58h/P5cIfmmBcWfa2M=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Xkg2rrIuLHK61V8g9JxLA1zMMS5BneIM8qMVKp+7muMOxp9B5dCXw+zC9uOmTAv9H dvvpoBkOr45VoFBjQ2oCXKrHbygILQCgYWsZRIn12iCXTzfvMMRPsPzHe2vw3LtBYw GiW2FhTydGi2t2NeIyPTBFA4tFHBhzuceoUQUGtE= Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id AB54B3858D35 for ; Sat, 1 Jul 2023 08:53:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB54B3858D35 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-632-_q1NcoruOvW1GxrmQwdcvA-1; Sat, 01 Jul 2023 04:53:13 -0400 X-MC-Unique: _q1NcoruOvW1GxrmQwdcvA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D72C71011630 for ; Sat, 1 Jul 2023 08:53:12 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 66708C00049 for ; Sat, 1 Jul 2023 08:53:12 +0000 (UTC) To: gdb@sourceware.org Subject: Using GDB as a Python interpreter Date: Sat, 01 Jul 2023 10:53:10 +0200 Message-ID: <87sfa8vwqx.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Florian Weimer via Gdb Reply-To: Florian Weimer Errors-To: gdb-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb" I want to run a Python script with GDB, from the shell command line, without having to create two files. I came up with the hack included below, but it's not particularly nice. The downside is that the script file name must end in =E2=80=9C.py=E2=80=9D= , which would not allow to install the script in /usr/bin for most distributions. I don't see a way to override that. Thanks, Florian #!/usr/bin/python if 'gdb' not in globals(): import sys import os pid, =3D sys.argv[1:] os.execvp('gdb', [ 'gdb', '--batch', '-ex', 'set debuginfod enabled off', '-ex', 'attach ' + pid, '-x', __file__, ]) dl_ns =3D gdb.lookup_global_symbol('_rtld_global').value()['_dl_ns'] nsid_min, nsid_max =3D dl_ns.type.range() for nsid in range(nsid_min, nsid_max + 1): l =3D dl_ns[nsid]['_ns_loaded'] if not l: continue print('namespace', nsid) while l: print(' {!r}'.format(l['l_name'].string())) ln =3D l['l_libname'] while ln: print(' alias {!r}'.format(ln['name'].string())) ln =3D ln['next'] l =3D l['l_next']