From patchwork Tue Oct 31 16:07:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Partha Satapathy X-Patchwork-Id: 78830 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 34D513858421 for ; Tue, 31 Oct 2023 16:07:49 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id 9C6B23858D1E for ; Tue, 31 Oct 2023 16:07:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9C6B23858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9C6B23858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.177.32 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698768455; cv=none; b=ACwIKdESFjbesTRhrPeEuUmDd5xqc4dTV2kpzsy2aIBSHgPKnd5LxNRY+Q37Iv+FopiC7LFgvaqY17T7x9vptfX/m1/67bBFSbmTA1xO8qAy0JBEIgwfa/Yd/m12aAIgmLGTkxOA7DDcLQpv97VxBfu4JPt5SP/NJoCFgXiJ4QU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698768455; c=relaxed/simple; bh=GTdP3t3S0BKArptxYpB25qR8fLN7uhw7J3sl/TNYg2Q=; h=DKIM-Signature:From:To:Subject:Date:Message-Id; b=UcP24oyMTsm1/+xPtD+8p5BImSC7YLA7FwBRpyLoCmU1D0rJinK6adPVj0kk7zJd+z6oCAuXQM12xx0ru40Ez7c+QuNK9gBwzjBn2WccTiJjvlf8+4gbgILR7dmlo4/6qNRMtT2eeK4SgI+lEiKj7HR0G83k7Tj1fIDl0EZjjkY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 39VCnlmi027684 for ; Tue, 31 Oct 2023 16:07:33 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : subject : date : message-id; s=corp-2023-03-30; bh=cZbnvWF6FK2Nti+fl9/26tsGTPa2tNjniUA2vQE+/wM=; b=03V+iUrQZM62qTnR112GouKSKgd+fSpJDM+J+mGwiNbBX+5I5mRdc8vzc7U17IAfiicG 7aoML6/G16ADyHcUqcWuyP6VSQ+yWxNLyy9kQi8rt07LVTak7yII0Z73XmARbfbqbORP aE1TYna3UD0+rg1gEiGo9z5tDKppso6EB4oN/pLWezspNWAZMoKwybaMng3HkAi0KKmV OgFa30AkYLPuXYUq4CqzdygVRgG6Ci+UQF8M9x1YhH+OBQRP4YwfBnzfKM9wtaf96WTd 8TJgnHLzsubOPiPAZd36I+kaTWcR9ZNF2U8BIOD194h124gW7uinkmGzyelmwo5PB+NV QA== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3u0s33wnvu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 31 Oct 2023 16:07:32 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 39VFiBph020109 for ; Tue, 31 Oct 2023 16:07:32 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3u0rrc4hf6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 31 Oct 2023 16:07:32 +0000 Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 39VG4NXV027412 for ; Tue, 31 Oct 2023 16:07:31 GMT Received: from pssatapa-linux2-26-05-21.osdevelopmeniad.oraclevcn.com (pssatapa-linux2-26-05-21.allregionaliads.osdevelopmeniad.oraclevcn.com [100.100.252.95]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 3u0rrc4heq-1; Tue, 31 Oct 2023 16:07:31 +0000 From: Partha Satapathy To: partha.satapathy@oracle.com, cupertino.miranda@oracle.com, rajesh.sivaramasubramaniom@oracle.com, bert.barbe@oracle.com, gdb-patches@sourceware.org Subject: [PATCH 1/1] gdb : Signal to pstack/gdb kills the attached process. Date: Tue, 31 Oct 2023 16:07:31 +0000 Message-Id: <1698768451-9725-1-git-send-email-partha.satapathy@oracle.com> X-Mailer: git-send-email 1.8.3.1 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.987,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-10-31_03,2023-10-31_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 suspectscore=0 spamscore=0 phishscore=0 mlxlogscore=907 adultscore=0 mlxscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2310240000 definitions=main-2310310128 X-Proofpoint-GUID: 2Wt4EIQwiq-ObLS5xaHmxR21j22xZnT0 X-Proofpoint-ORIG-GUID: 2Wt4EIQwiq-ObLS5xaHmxR21j22xZnT0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, 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 From: Partha Sarathi Satapathy Problem:: While gdb attaching a target, If ctrl-c pressed in the midst of the process attach, the sigint is passed to the debugged process. This triggers exit of the debugged. Let's take the example of pstack, which dumps the stack of all threads in a process. In some cases printing of stack can take significant time and ctrl-c is pressed to abort pstack/gdb application. This in turn kills the debugged process, which can be critical for the system. In this case the intention of "ctrl+c" to kill pstack/gdb, but not the target application. Reproduction: The debugged application generally attached to process by: gdb -p <> or gdb /proc/<>/exe pid pstack uses the latter method to attach the debugged to gdb. If the application is large or process of reading symbols is slow, gives a good window to press the ctrl+c during attach. Spawning "gdb" under "strace -k" makes gdb a lot slower and gives a larger window to easily press the ctrl+c at the precise period i.e. during the attach of the debugged process. The above strace hack will enhance rate of reproduction of the issue. Testcase: With GDB 13.1 ps aux | grep abrtd root 2195168 /usr/sbin/abrtd -d -s Attaching to process 2195168 [New LWP 2195177] [New LWP 2195179] ^C[Thread debugging using libthread_db enabled] <<<< Note the ctrl+c is pressed after attach is initiated and it's still reading the symbols from library >>>> Using host libthread_db library "/lib64/libthread_db.so.1". 0x00007fe3ed6d70d1 in poll () from /lib64/libc.so.6 (gdb) q A debugging session is active. Inferior 1 [process 2195168] will be detached Quit anyway? (y or n) y Detaching from program: /usr/sbin/abrtd, process 2195168 <<<< Process exited >>>> Description: We are installing a signal handler in gdb that marks the Ctrl-c/sigint received by gdb. GDB passes this sigint to the debugged at some definite points during the window of process attach. The process of attaching debugged involves steps like PTRACE_ATTACH , reading symbols, getting the stop signal from the debugged and get ready with GDB prompt. Note: one of the example of this is sigint passing is: " - installs a SIGINT handler that forwards SIGINT to the inferior. Otherwise a Ctrl-C pressed just while waiting for the initial stop would end up as a spurious Quit. " There are few other places where sigint is passed to the debugged during attach of process to gdb. As the debugger and debugged are not fully attached during this period, the sigint takes its default action and terminates the process. Solution: While gdb attaches process, the target is not the current session leader. Hence, until attach is complete and GDB prompt is availed, the sigint should not be passed to the debugged. A similar approach is taken for "gdb) run &". In target_terminal::inferior() /* A background resume (``run&'') should leave GDB in control of the terminal. */ if (ui->prompt_state != PROMPT_BLOCKED) return; The passing of signal is skipped if the process ran in background. With this approach we can skip passing the sigint if the process is attached to gdb and process attach is not complete. Here is the proposed solution: Fix : While gdb attaching a target, If ctrl-c/sigint pressed in the midst of the process attach, the sigint is passed to the debugged process. This triggers exit of the debugged. This issue is evident while getting the process stack with ./gdb --quiet -nx -ex 'set width 0' -ex 'set height 0' -ex 'set pagination no' -ex 'set confirm off' -ex 'thread apply all bt' -ex quit /proc//exe and press the ctrl+c while attach. The above method is also used in pstack application which is a wrapper over gdb to print the process stack. A Ctrl+C intended to kill gdb or pstack, but kills the debugged even if it is attached and not spawned by gdb. --- gdb/inferior.h | 3 +++ gdb/target.c | 4 ++++ gdb/top.c | 2 ++ 3 files changed, 9 insertions(+) diff --git a/gdb/inferior.h b/gdb/inferior.h index 4d001b0ad50e..b7048d10bbe4 100644 --- a/gdb/inferior.h +++ b/gdb/inferior.h @@ -557,6 +557,9 @@ class inferior : public refcounted_object, /* True if this child process was attached rather than forked. */ bool attach_flag = false; + /* True if target process synced and gdb ui is out of block. */ + bool sync_flag = false; + /* If this inferior is a vfork child, then this is the pointer to its vfork parent, if GDB is still attached to it. */ inferior *vfork_parent = NULL; diff --git a/gdb/target.c b/gdb/target.c index d5bfd7d0849b..b2bf49fda5a1 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -3826,6 +3826,10 @@ struct target_ops * through the target_stack. */ scoped_restore_current_inferior restore_inferior; set_current_inferior (inf); + if ((current_inferior()->attach_flag) && + !(current_inferior()->sync_flag)) { + return; + } current_inferior ()->top_target ()->pass_ctrlc (); return; } diff --git a/gdb/top.c b/gdb/top.c index a685dbf5122e..684b0b2df277 100644 --- a/gdb/top.c +++ b/gdb/top.c @@ -542,6 +542,8 @@ struct ui_out ** while (gdb_do_one_event () >= 0) if (ui->prompt_state != PROMPT_BLOCKED) break; + + current_inferior()->sync_flag = true; } /* See top.h. */