From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id M/geOBiClmUTbjIAWB0awg (envelope-from ) for ; Thu, 04 Jan 2024 05:02:00 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=Q6KcYkXt; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id D6DE21E0C3; Thu, 4 Jan 2024 05:02:00 -0500 (EST) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id C16F21E0B9 for ; Thu, 4 Jan 2024 05:01:58 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EBC253861824 for ; Thu, 4 Jan 2024 10:01:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EBC253861824 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1704362518; bh=yoI+y6mGwcZ/ZrNRRPDVQVoyefUmrC65BJkBANOFPx4=; h=References:In-Reply-To:Date:Subject:To:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Q6KcYkXtpGJkJIDkFna+iRpmF5zamub11sggI1xWXqXp33a53l1LTp5HnXttoNzvb ZZwiC2GkqSdOKz5pjC4AZM06uZUc02pq4IjR/Il+MuWiibdlKFuxtblax2DIg6w8+L 5ajufSKA03EDC8qlwU1e3ASmAFw6r261BDtA47bw= Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id E51EC3858C2C for ; Thu, 4 Jan 2024 10:01:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E51EC3858C2C ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E51EC3858C2C ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704362474; cv=none; b=Qz9GjoU/8oHWtqw8a+qiAiWQZa4mH2VAHDT+DL48iaINXuUNxu5M8it1pJnT71b7DKYg3rAdn/m4um/7n/A7WX7RPcYaZCW+H/kvJ+UmsPoxW8l1bfKa/WzfsWN/WHhr1PIyOerezqqgWNUOM11pK2eRgVlDRn/+x+POkKgXVWA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704362474; c=relaxed/simple; bh=51UD8BEPUnJdkYIPTXhUau7yoFa81XV+voxSGJJ02qg=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=GC8pkKRhWXjnBM/GlQDAgeYztiGzzchWSEdItz3598OcCpjIhg3yPPfJ64tssJ8uykHwEeocypt9sgdApuMKrUzepn8Epm9DbR0nn2AmsYr5wc3igQNogydjtM/mP10SR6v4oq3OOMzaJRlXM5hUlbHzoXyDH/7P67Ycmyk6L90= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-a277339dcf4so33902466b.2 for ; Thu, 04 Jan 2024 02:01:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704362470; x=1704967270; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yoI+y6mGwcZ/ZrNRRPDVQVoyefUmrC65BJkBANOFPx4=; b=Rmdd6gSKv/R+80aJf7o+X23YzPHRgLBcF4pjEhlmOpjmF/MUvWatlpzqf5CL/74H7o jBYjqJxm7EztakGwmms0ANjDyLEKlJH1qeK7Z8cJIrTTPtR3XjvCbh9YrrYhcaNyokhE g3X3L5WISyPPPgtOZER7PpZtgtxxgfRWVoHP9pRlApaTSv3p0ybq3zEiV9o2A7mpnv1C J+8u4v8Fy9oGR3fmfThkq1jVAp154DRHOPHU+9gdsvVppK6w6CP3yxMUjBpp19Jshe2x fFtKdor6e5phP1q1rFOxzMtvgAkdAOuIkHAg+DjEGS4qspEav96KNpYpVqOMC1KAii3b I+qg== X-Gm-Message-State: AOJu0YzdeVkJVL8bAXNR5YCDio6aEez0plzZOmrqOJbORrrBpuYRwEZX RUFuvLTvpQYL1nX/bsurFqQR2j56keb3ddpu2KwnG0Kbwoo= X-Google-Smtp-Source: AGHT+IF6FQhYpnr6HeHJplzj9tGmuREUwE8g9gZHMcfZl/m7keLhHK/fZHdgouq41pJbqbqcmSzN3bGeSSomu7hBjVk= X-Received: by 2002:a17:907:320e:b0:a28:cf39:bf68 with SMTP id xg14-20020a170907320e00b00a28cf39bf68mr228822ejb.115.1704362470250; Thu, 04 Jan 2024 02:01:10 -0800 (PST) MIME-Version: 1.0 References: <874jgp7ksd.fsf@tromey.com> <87plzceio4.fsf@tromey.com> <87msu3dqp4.fsf@tromey.com> <87edfedozl.fsf@tromey.com> In-Reply-To: <87edfedozl.fsf@tromey.com> Date: Thu, 4 Jan 2024 13:00:59 +0300 Message-ID: Subject: Re: "previous frame inner to this frame" error when unwinding fibers To: Tom Tromey Cc: Andrey Turkin via Gdb Content-Type: multipart/mixed; boundary="0000000000009bacd7060e1bcfce" X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Andrey Turkin via Gdb Reply-To: Andrey Turkin Errors-To: gdb-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb" --0000000000009bacd7060e1bcfce Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Tom, Thanks for the update. Re stopping the unwind for green threads - callback makes sense I think; I guess most users would want to chop off some useless tail there. But also custom unwinders might want to have some clean way to do the same for whatever reason. I currently just do create_unwind_info() without filling any registers; that works but results in an ugly error message. Re inner-than thing - this is orthogonal to green threads. This is something that happens because of unwinder stitching together different stacks; it doesn't have to be due to green threads. In fact, with the green threads support it might not be necessary to do the stiching; we might get away with doing several bts for the threads we want shown together, or something like a custom command to switch between callee/caller contexts etc. But anyway, if this is to be implemented, it seems to me this belongs to the unwinders domain. Re PoC implementation on GH - I tried it out. There was an obvious bug (see attached patch), after dealing with that I was able to create a green thread for my own test example (boost coroutines, using core file) as well as Vireo examples (live process) with your python file. Thread exists, it is shown in info threads with the name I gave it in python, I can switch to it etc, but the registers don't get fetched and bt, info frame etc shows information of a native thread with id 1 (even if I'm switching from the different thread, and creating the green thread while switched to another thread, and tid passed to create_green_thread being some random number which is definitely not 1). py_green_thread::fetch_registers doesn't get called for some reason, so the Python counterpart doesn't as well. =D0=BF=D1=82, 22 =D0=B4=D0=B5=D0=BA. 2023=E2=80=AF=D0=B3. =D0=B2 22:19, Tom= Tromey : > > Tom> Yeah, the API on the branch is very simple. > > I made a new branch and reworked it a bit, dropping a few stale parts. > It's in my github as "green-threads-v2"; you can try it if you want. > > I've appended a sample .py that works with the "vireo" user-space > threads package so you can see how it works. > > I still haven't implemented a way for green threads to know when to stop > unwinding. And, I haven't added a way to indicate that the "inner than" > requirement should be lifted. > > If you have ideas for how those ought to work, that would be good to > hear. We talked about doing this in an unwinder but it seems kind of > heavy to require an unwinder to supplement green threads; though maybe > in your case that's ok. > > One thought I had was to have a callback on the green thread object that > could be passed a frame to see if it is the outermost frame. This way > green threads, at least, could stop unwinding at their scheduler; and > users wanting to debug a scheduler could switch to a "real" thread to > see that. > > Maybe inner-than could just be an optional flag passed to > create_green_thread. > > Tom > > > import gdb > > thread_map =3D {} > > main_thread =3D None > > # From glibc/sysdeps/unix/sysv/linux/x86/sys/ucontext.h > x8664_regs =3D [ 'r8', 'r9', 'r10', 'r11', 'r12', 'r13', 'r14', > 'r15', 'rdi', 'rsi', 'rbp', 'rbx', 'rdx', 'rax', > 'rcx', 'rsp', 'rip', 'efl', 'csgsfs', 'err', > 'trapno', 'oldmask', 'cr2' ] > > def vireo_current(): > return int(gdb.parse_and_eval('curenv')) + 1 > > class VireoGreenThread: > def __init__(self, tid): > self.tid =3D tid > > def _get_state(self): > return gdb.parse_and_eval('envs')[self.tid]['state'] > > def fetch(self, reg): > """Fetch REG from memory.""" > global x8664_regs > global thread_map > thread =3D thread[self.tid] > state =3D self._get_state() > gregs =3D state['uc_mcontext']['gregs'] > for i in range(0, len(x8664_regs)): > if reg is None or reg =3D=3D x8664_regs[i]: > thread.write_register(x8664_regs[i], gregs[i]) > > def store(self, reg): > global x8664_regs > global thread_map > thread =3D thread[self.tid] > state =3D self._get_state() > gregs =3D state['uc_mcontext']['gregs'] > for i in range(0, len(x8664_regs)): > if reg is None or reg =3D=3D x8664_regs[i]: > gregs[i] =3D thread.read_register(x8664_regs[i]) > > def name(self): > return "Vireo Thread " + str(self.tid) > > def underlying_thread(self): > if vireo_current() =3D=3D self.tid: > global main_thread > return main_thread > return None > > class VFinish(gdb.FinishBreakpoint): > def stop(self): > tid =3D int(self.return_value) + 1 > global thread_map > thread_map[tid] =3D gdb.create_green_thread(tid, VireoGreenThread= (tid)) > return False > > class VCreate(gdb.Breakpoint): > def stop(self): > VFinish(gdb.newest_frame(), True) > return False > > class VExit(gdb.Breakpoint): > def stop(self): > global main_thread > if main_thread is None: > main_thread =3D gdb.selected_thread() > global thread_map > tid =3D vireo_current() > if tid in thread_map: > thread_map[tid].set_exited() > del thread_map[tid] > > VCreate('vireo_create', internal=3DTrue) > VExit('vireo_exit', internal=3DTrue) --0000000000009bacd7060e1bcfce Content-Type: application/octet-stream; name="patch.py" Content-Disposition: attachment; filename="patch.py" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_lqz0zw4s0 ZGlmZiAtLWdpdCBhL2dkYi9weXRob24vcHl0aG9uLmMgYi9nZGIvcHl0aG9uL3B5dGhvbi5jCmlu ZGV4IDZhNDg2N2ZlZWJjLi4zNGNiYzdhMWIyNSAxMDA2NDQKLS0tIGEvZ2RiL3B5dGhvbi9weXRo b24uYworKysgYi9nZGIvcHl0aG9uL3B5dGhvbi5jCkBAIC0yNzU0LDcgKzI3NTQsNyBAQCBSZXR1 cm4gdGhlIGN1cnJlbnQgcHJpbnQgb3B0aW9ucy4iIH0sCiBPdXRwdXQgYXN5bmMgcmVjb3JkIHRv IE1JIGNoYW5uZWxzIGlmIGFueS4iIH0sCiAKICAgeyAiY3JlYXRlX2dyZWVuX3RocmVhZCIsIChQ eUNGdW5jdGlvbikgZ2RicHlfY3JlYXRlX2dyZWVuX3RocmVhZCwKLSAgICBNRVRIX1ZBUkFSR1Ms CisgICAgTUVUSF9WQVJBUkdTIHwgTUVUSF9LRVlXT1JEUywKICAgICAiY3JlYXRlX2dyZWVuX3Ro cmVhZCAoQ0FMTEJBQ0spIC0+IEdyZWVuVGhyZWFkLlxuXAogQ3JlYXRlIGEgbmV3IGdyZWVuIHRo cmVhZC4iIH0sCiAK --0000000000009bacd7060e1bcfce--