From patchwork Wed Jan 10 15:54:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83758 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 C22893860765 for ; Wed, 10 Jan 2024 15:56:10 +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 2BFE23857B99 for ; Wed, 10 Jan 2024 15:54:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2BFE23857B99 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 2BFE23857B99 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=1704902101; cv=none; b=SHUDBWNRIYNKCzwpFBrVfTPKAXakWkh44f9+7GrfZqsORQIZ/RmLsKt61LXhXf+nX9c+QR4jgjG3GLVJhwBzfe6VEh5cCM/3LofaP7sa6SO+t6IKo5BhB55ctjcIp1LM4ncFCBr1bRARaUQRU6TbzMdBmmeM5Gl+3hbF5I+0lms= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704902101; c=relaxed/simple; bh=EuzstJi/SQudauaNeVnW6Z84FCcaipa77ZcpvAo+Ako=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=vnNcLGhv1mkeLZWmm1BhLOzhoqCxDHX9c+iM+oB6xjnbCIHehnAVpoiuSbZKjN0X8A4hcdYt2JFYOwpp05uVhyxbWbij/8mWGofhFMb6H9qKFzv0mJfX4iBWSJkt2Nh+Ls3tqmS0/vKyFr/BuYUHTbSIp/tJzkQOpvGBVcUZX2w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704902096; 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=b0MOQC5SUyGPDDeFpVuGLqBcJZszM+TBMyx1G1B+80C5aAI17159XxCoVAGFneaZBt1f5s I8+xeXpfgXdp1eKE+PqnIOU8bP0Yi8dtT9QK7SbTD/7n+0qCj+q/AvztXlZBMYJeyt71vt 39bFl0uh+Aypn+3k1klCUHcKurnQTbo= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-74-PC03Njf0NquBKGUwZkDsdA-1; Wed, 10 Jan 2024 10:54:55 -0500 X-MC-Unique: PC03Njf0NquBKGUwZkDsdA-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-3368698efbdso2575270f8f.0 for ; Wed, 10 Jan 2024 07:54:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704902094; x=1705506894; 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=lsZIwfWJf9LgyjTh+FfXoQP/G22Q7HuM+wVjTmtwUmQYDidkSBm3phRNj0obGBJjgl YT0gQJueL87t3jyxxG7sE88DQ72cXj/H2asOn23wIFDyJMg8hCCHzJIHzLNIhIWntFyx S7IY1FSy5XrSVGtwPGl8MdtDroLxv1VP20dzNBk29J5Slr/hLzjpRUYGsXzrzf4DVDA1 yLzyb9lxaMjJsxROZzozkXqV5HudxUcWH7BnKgUEFsHyf6zOuBbwKu62vv5AAwSWLWZh tpZcH4e+YwzL44Adp4eAD3Z8Ujbg5Zc66kCCtPIiI3qRwcAYPnLpuB5M2mkTWjJd8tl0 eKhw== X-Gm-Message-State: AOJu0Yysn5XzCMuk+onA3nCcQosy2H09Z6uK+km9Rwk++Jx0sgQNje4I sqW9MwArn5VcSK8CvKOTjnptkJr4EbzITt8PX50EAPVc6QkyzOr4nyrGf4Y68SDt6QiSITexlG2 4cemUScihlWXKNsVMt3YyPcRlLa97OlDpfPfvkyNojh1DWsNU8dQYuMPMpLQ+jd4QVMhCwqoOwr SF4tzox6ZUPzj8rw== X-Received: by 2002:a5d:6e07:0:b0:336:9e8a:6fde with SMTP id h7-20020a5d6e07000000b003369e8a6fdemr789177wrz.22.1704902093829; Wed, 10 Jan 2024 07:54:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IFUj9T9iiZqpuXfG9eNwRp/s8sn1SAi4O1cpFMaYJXXwPVcgxTko0IIotH1xeAZ+FCvytV8ig== X-Received: by 2002:a5d:6e07:0:b0:336:9e8a:6fde with SMTP id h7-20020a5d6e07000000b003369e8a6fdemr789171wrz.22.1704902093402; Wed, 10 Jan 2024 07:54:53 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id bi6-20020a05600c3d8600b0040e3960f46asm2604545wmb.14.2024.01.10.07.54.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 07:54:52 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 2/8] gdb/python: add gdb.InferiorThread.__repr__() method Date: Wed, 10 Jan 2024 15:54:39 +0000 Message-Id: <37fbaf0a27d3938f3107be2ab79dac3e748ccff6.1704901918.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.0 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"