From patchwork Fri Jan 5 11:48:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83392 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2182D385E005 for ; Fri, 5 Jan 2024 11:49:43 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org 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 69FD83857C49 for ; Fri, 5 Jan 2024 11:48:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 69FD83857C49 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 69FD83857C49 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704455328; cv=none; b=KmkS2INSduDHz/scutWFSYOyZ8KoKV3UfkD3+ErRjMss1YBEVZj89YODpUgsLwFlvvdLvaameK71taA6oXrbB8bzTLlrEug8uxeyQgR4k48iZ61sdR21UAFCymIPze40s2T64ePNwHvQt9Grt7NQ8Zg3qbdiKcPG1ibIuCI2iSc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704455328; c=relaxed/simple; bh=EuzstJi/SQudauaNeVnW6Z84FCcaipa77ZcpvAo+Ako=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=iBzLAoY8/mGLKYBK8GGv8IXndJt6/eiq/CmaFnldhKD5aytf9mW0IgdFZxYhRSJoyS4zRZNbF6kUIhvR5tkaPNqlxOeoyp3OGrc08gRy3oyg1NwGsC3Doitl/g+j1IDmn9qL4k4YxSFimKENsh5dfe8uFPE+zDyIG/vHwHjiwGs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704455326; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fnSE3H2ACK1VEqkPatnv0AwGOzic/lZTZ0DBaMWIQQQ=; b=jHP0C4zJXHWPwCnGB4umdwPazHNtW0tq+/3MPZcjzKKyQNZnTqp7QqwX7Eznd+1zCja19p YhgR6oR20LDBMvn8LtoZjc70FxekMRxQmskQtIM6RGPpZmpSvq5CPuVWlmAXfeIAsZUs5p D7iaaZRi3IsYxnf0GLsAgCXPUlJ7b1E= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-630-9YDXxYVXNIqx6EkpvpWpPg-1; Fri, 05 Jan 2024 06:48:45 -0500 X-MC-Unique: 9YDXxYVXNIqx6EkpvpWpPg-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a28f6a78b83so52390666b.1 for ; Fri, 05 Jan 2024 03:48:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704455323; x=1705060123; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fnSE3H2ACK1VEqkPatnv0AwGOzic/lZTZ0DBaMWIQQQ=; b=v/o/Vg41Mh9FYYGPxsBW2DbtWtGw3UN+t3V5Kvc22HzWNl7Hr3IJ3hsbpT442KrCC1 U/C3/7/Bh3SDHkzufXcBwob/mGT4Kxtqr1BpBp//hu/gkCsV9UvHBfZLw+/zxaZwqsuc ZLgj//jgU9gZedoqBM+GLim+aQKkC5B3DeiQ1wcBe7HrrDXlJB8/SEgVkOpWWQO/4rKu tQA6UkIGLMM7IKobDCP8LoL+umdHP+2Sbo+0jCYH+Dl8UOqqbucQRYvS0Sw+u2B9NkMj OhIoIITML7mcaOYrYKMPJD2ngAOWuvROamo1fpWTGqRIBi9BubCSb46sZBLcY6+jNGAj GMMw== X-Gm-Message-State: AOJu0YxRlghoSWoBwhnKnzwgGk+0YSFnI+cFkGfKNRQeLmtkwD53/LIk 5poRhMAn3rrVF/r/EP8bMjOSlQUjp5H4GAp1eTQg5rh9/230U4iXIq9cB2OGUj/v9/Mx4jGwV5r AVx93LIja/aO02k/hh5IRam+pt1e78hQsfVeP7zG9GD7/iw7+uPnbKx4G3bVq0DFa9sDA2KT4ij n/L0lu9D38zamlEg== X-Received: by 2002:a17:906:1dd:b0:9cc:450c:b0d5 with SMTP id 29-20020a17090601dd00b009cc450cb0d5mr970281ejj.4.1704455322864; Fri, 05 Jan 2024 03:48:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IFUbYnxbtxUY7MdKCIfotZrb3frNRn6GdA+UuS8bvYCmjeykOOv6W0aPb8pzJ2ACXNhWGH1tA== X-Received: by 2002:a17:906:1dd:b0:9cc:450c:b0d5 with SMTP id 29-20020a17090601dd00b009cc450cb0d5mr970275ejj.4.1704455322369; Fri, 05 Jan 2024 03:48:42 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id q13-20020a170906770d00b00a2697984204sm797206ejm.72.2024.01.05.03.48.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 03:48:40 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/6] gdb/python: add gdb.InferiorThread.__repr__() method Date: Fri, 5 Jan 2024 11:48:31 +0000 Message-Id: <37fbaf0a27d3938f3107be2ab79dac3e748ccff6.1704455158.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, 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-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Add a gdb.InferiorThread.__repr__() method. Before this patch we would see output like this: (gdb) pi >>> gdb.selected_thread() After this patch, we now see: (gdb) pi >>> gdb.selected_thread() More verbose, but, I hope, more useful. If the gdb.InferiorThread becomes invalid, then we will see: (gdb) pi >>> invalid_thread_variable Which is inline with how other invalid objects are displayed. --- gdb/python/py-infthread.c | 19 ++++++++++++++++++- gdb/testsuite/gdb.python/py-inferior.exp | 16 +++++++++++++++- gdb/testsuite/gdb.python/py-infthread.exp | 3 ++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c index 00d7171de64..2d892b10b69 100644 --- a/gdb/python/py-infthread.c +++ b/gdb/python/py-infthread.c @@ -313,6 +313,23 @@ thpy_thread_handle (PyObject *self, PyObject *args) return object; } +/* Implement repr() for gdb.InferiorThread. */ + +static PyObject * +thpy_repr (PyObject *self) +{ + thread_object *thread_obj = (thread_object *) self; + + if (thread_obj->thread == nullptr) + return gdb_py_invalid_object_repr (self); + + thread_info *thr = thread_obj->thread; + return PyUnicode_FromFormat ("<%s id=%s target-id=\"%s\">", + Py_TYPE (self)->tp_name, + print_full_thread_id (thr), + target_pid_to_str (thr->ptid).c_str ()); +} + /* Return a reference to a new Python object representing a ptid_t. The object is a tuple containing (pid, lwp, tid). */ PyObject * @@ -429,7 +446,7 @@ PyTypeObject thread_object_type = 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ - 0, /*tp_repr*/ + thpy_repr, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp index 6fbcdd6822f..5a221f800c3 100644 --- a/gdb/testsuite/gdb.python/py-inferior.exp +++ b/gdb/testsuite/gdb.python/py-inferior.exp @@ -78,7 +78,9 @@ gdb_test "python print ('result = %s' % (i0.connection_num == i0.connection.num) "Inferior.connection_num equals Inferior.connection.num" gdb_test "python print ('result = %s' % i0.pid)" " = \[0-9\]+" "test Inferior.pid" gdb_test "python print ('result = %s' % i0.was_attached)" " = False" "test Inferior.was_attached" -gdb_test "python print (i0.threads ())" "\\(,\\)" "test Inferior.threads" +gdb_test "python print (i0.threads ())" \ + "\\(,\\)" \ + "test Inferior.threads" gdb_test "python print (i0.progspace)" "" gdb_test "python print (i0.progspace == gdb.progspaces()\[0\])" "True" @@ -89,11 +91,23 @@ gdb_breakpoint check_threads gdb_continue_to_breakpoint "cont to check_threads" ".*pthread_barrier_wait.*" gdb_test "python print (len (i0.threads ()))" "\r\n9" "test Inferior.threads 2" +# Grab the last thread from the list. This thread object will become +# invalid when the corresponding thread exits. +gdb_test_no_output "python last_thread = i0.threads()\[-1\]" +gdb_test "python print(last_thread)" \ + "" \ + "test repr of a valid thread" + # Proceed to the next test. gdb_breakpoint [gdb_get_line_number "Break here."] gdb_continue_to_breakpoint "cont to Break here." ".*Break here\..*" +# Check the repr() for an invalid gdb.InferiorThread object. +gdb_test "python print(last_thread)" \ + "" \ + "test repr of an invalid thread" + # Test memory read and write operations. gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \ diff --git a/gdb/testsuite/gdb.python/py-infthread.exp b/gdb/testsuite/gdb.python/py-infthread.exp index 0b10ce9ff77..49622c8ac28 100644 --- a/gdb/testsuite/gdb.python/py-infthread.exp +++ b/gdb/testsuite/gdb.python/py-infthread.exp @@ -57,7 +57,8 @@ gdb_py_test_silent_cmd "python gdb.selected_thread ().inferior" "test InferiorTh gdb_py_test_silent_cmd "python t0 = gdb.selected_thread ()" "test gdb.selected_thread" 1 -gdb_test "python print (t0)" "\\" "verify InferiorThread object" +gdb_test "python print (t0)" \ + "" gdb_test "python print ('result = %s' % t0.num)" " = 1" "test InferiorThread.num" gdb_test "python print ('result = %s' % t0.global_num)" " = 1" "test InferiorThread.global_num" gdb_test "python print ('result = %s' % str (t0.ptid))" " = \\(\[0-9\]+, \[0-9\]+, \[0-9\]+\\)" "test InferiorThread.ptid"