From owner-freebsd-hackers@freebsd.org Fri Feb 22 23:58:27 2019 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 6C98914FEB97 for ; Fri, 22 Feb 2019 23:58:27 +0000 (UTC) (envelope-from robert.ayrapetyan@gmail.com) Received: from mail-ot1-x344.google.com (mail-ot1-x344.google.com [IPv6:2607:f8b0:4864:20::344]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id D79A98DB57 for ; Fri, 22 Feb 2019 23:58:26 +0000 (UTC) (envelope-from robert.ayrapetyan@gmail.com) Received: by mail-ot1-x344.google.com with SMTP id b3so3380285otp.4 for ; Fri, 22 Feb 2019 15:58:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=soYGC/v8NIHCGviKqxuvfyCePB/gMJaDlGaTddxXMYk=; b=m9Wcc0qKQU+FMULnhLqtAyuVDCUyzmS7tq6a0rWh5peV5qwpY9i5H4Q/lkOk7j3cxI 2uTALj2QVPmOcIgrahDVPPFKpXB1NPlwp44A7UsElJZTNTcmHHg74sYaa3gamxT5r3H4 UWHpQCH9DC8npJi/+ZsZr/Iql/fsO3jFUK8/Fr+BK9farsY1bZg7K8V10os6TwvVdEIP KevEqmKcc/i+gmFB2DLw6ZFtV4MvSKBBLKNasDW16TYJL5hVq28v7BZRLyrPL5wBXTPR 1Km9E9nfa0tqgAlCUR0cY5wZQGxX+QyE+IChf8/TLUN3PS1nhXdqLtSdHHqserTvhHQA BOyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=soYGC/v8NIHCGviKqxuvfyCePB/gMJaDlGaTddxXMYk=; b=G2lCh1kilPRcxLA2t/TYr8RrFJlk2VC1EWG8pgfHWUk2yXMfyjszV5CU5yehyyBZ6U RDagvUFCGqKyB6980fglLdj3DrhDn00UXs1Xe0SNSk9otLLnVRiGWhsfLJ85Rwv9fGE/ z51BSRJuDWweeGY5QIRh47ctno3mwDFGMWZmkJwsm9zLU/aCl4/MsySHCjfou6Kw0Fkh NWNf1oJQDbRgotYD2EkJJ5pCIbl/vTLtS/PNbKyVfko7EJ1KxtCnKhUUEtMoQ01VQC7u iqjrfnEQbpSw+WW+HGVS9CtcNraJUuEV8UEYzvlgH/QvZeheBEF8rp2C5T0N18eEazxC 0jpw== X-Gm-Message-State: AHQUAuYU+Ir2nLqc3lfgxiFG4dcfh2epOkKG/P0iOnDiUc5DCEIrTUd/ 45tGhvDrpKEVQx/+oiu3iJSH4bHN49SQmcZUng== X-Google-Smtp-Source: AHgI3IYWKyp+dS38QCEu6Fyf4+hM/EgryXnXoOn6wUCQe3B3bEazJRd4XGrYbBWDAJjDzUmJ7LYnyIqnuShRjKo8ICI= X-Received: by 2002:a05:6830:1c1:: with SMTP id r1mr4234258ota.229.1550879905872; Fri, 22 Feb 2019 15:58:25 -0800 (PST) MIME-Version: 1.0 References: <20190222101026.GX2420@kib.kiev.ua> In-Reply-To: <20190222101026.GX2420@kib.kiev.ua> From: Robert Ayrapetyan Date: Fri, 22 Feb 2019 15:57:49 -0800 Message-ID: Subject: Re: ptrace: SIGTRAP and EXIT race To: Konstantin Belousov Cc: FreeBSD X-Rspamd-Queue-Id: D79A98DB57 X-Spamd-Bar: ------ Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-6.99 / 15.00]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; REPLY(-4.00)[]; NEURAL_HAM_SHORT(-0.99)[-0.988,0]; TAGGED_FROM(0.00)[] Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 22 Feb 2019 23:58:27 -0000 Hi, thanks for a prompt reply. Here are the instructions of how to reproduce (sorry for inconvenient way of specifying BP address when running app): uname -a FreeBSD XXX 12.0-RELEASE-p3 FreeBSD 12.0-RELEASE-p3 GENERIC amd64 cd /tmp git clone https://github.com/rayrapetyan/ptrace_bug_poc.git cd ptrace_bug_poc mkdir build cd build cmake .. make Run ~20 times: /tmp/ptrace_bug_poc/build/src/ptrace_test/ptrace_test /tmp/ptrace_bug_poc/build/src/mt_example/mt_example 0x201385 ------- Note: make sure 0x201385 is a call to in "/tmp/ptrace_bug_poc/build/src/mt_example/mt_example": gdb /tmp/ptrace_bug_poc/build/src/mt_example/mt_example disassemble foo ------- Wait fo appearance of: "BOOM! Invalid BP hits counter (hits: 1, tid: XXXX)" at the end of the output (most of the times it will be "SUCCESS") Thanks. On Fri, Feb 22, 2019 at 2:10 AM Konstantin Belousov wrote: > On Thu, Feb 21, 2019 at 08:43:20PM -0800, Robert Ayrapetyan wrote: > > Hello. > > > > Before creating a bug and providing some test code, would ask a community > > here. > > When tracing a process using ptrace and there are multiple threads in the > > tracing process hitting the same breakpoint, sometimes main thread exits > > (WIFEXITED(status) is TRUE) before last queued TRAP_BKPT signal(s) have > > been delivered to the tracing process. So a final breakpoint hits counter > > is less than it should be. > > > > So in the example below: > > > > #include > > #include > > > > #include > > > > static const int num_threads = 2; > > > > void foo() { > > for (int i = 0; i < 2; ++i) { > > printf("hi: %d (tid: %d)\n", i, pthread_getthreadid_np()); > > } > > } > > > > int main() { > > std::thread t[num_threads]; > > > > for (int i = 0; i < num_threads; ++i) { > > t[i] = std::thread(foo); > > } > > > > for (int i = 0; i < num_threads; ++i) { > > t[i].join(); > > } > > > > return 0; > > } > > > > If we set breakpoint to printf, it should be triggered 4 times (tracing > > process should receive TRAP_BKPT 4 times). However, in ~1 of 5 runs, it > > receives TRAP_BKPT just 2 or 3 times. > > > > Is this expected? Thanks. > > I indeed would expect that all four breakpoints triggered before the > main thread exits, assuming that the breakpoints were installed before > the threads are created. Please provide the stand-alone (and preferrably > non-interactive) test to reproduce the issue. >