From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688201594; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=jflFKUTaAAU+dvZHVCyLSt7GV58h/P5cIfmmBcWfa2M=; b=B4ZMUS+jBPea8/DVd3rPljqRI8C1KSMb+Ul1X/chlFTuVlqFXaP/gPEwLwKgX2SSWmA/4j TdY41Fq3rxXh7Vl/xHiVvFLuibpPA+LdB2rH1NQsS62yxxQT7xeuge/stZ7QwDqySjWvvx nuTQD0tPNUISVUR3IOPCeyeGFy3CdQg= 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) From: Florian Weimer 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 List-Id: 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']