* Debugging Emacs with threads
@ 2016-12-11 16:26 Eli Zaretskii
2016-12-11 16:43 ` Andreas Schwab
2016-12-12 3:18 ` Ken Raeburn
0 siblings, 2 replies; 8+ messages in thread
From: Eli Zaretskii @ 2016-12-11 16:26 UTC (permalink / raw
To: Tom Tromey; +Cc: emacs-devel
There's a nasty issue that people who debug Emacs with threads need to
be aware of.
When Emacs stops due to a breakpoint, the thread that is the current
one is in sync between GDB and Emacs. IOW, the current_thread
variable describes the same thread on which GDB commands will act.
But as soon as you say something like "thread 1" at the GDB prompt,
this synchronization is lost: GDB acts on the thread you specified,
while current_thread is still pointing at the thread which was the
current one when Emacs stopped.
The result of this is that invoking functions in Emacs will likely
crash, because thread-specific variables used by Emacs are incorrect
for the thread whose stack GDB will use to create the call-stack frame
for the function it calls. In particular, xbacktrace will definitely
crash. And since src/.gdbinit automatically invokes xbacktrace when
you type "bt", any "bt" issued when the threads mismatch will ruin the
debugging session.
It would be good to protect xbacktrace from this, and avoid calling
functions in Emacs in this situation. Unfortunately, I couldn't find
any way of gleaning the OS thread ID from the GDB thread number.
Maybe Tom, who knows a lot about GDB, could help, or someone else
might have an idea that we then could implement in .gdbinit?
TIA
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Debugging Emacs with threads
2016-12-11 16:26 Debugging Emacs with threads Eli Zaretskii
@ 2016-12-11 16:43 ` Andreas Schwab
2016-12-11 17:37 ` Eli Zaretskii
2016-12-12 3:07 ` Ken Raeburn
2016-12-12 3:18 ` Ken Raeburn
1 sibling, 2 replies; 8+ messages in thread
From: Andreas Schwab @ 2016-12-11 16:43 UTC (permalink / raw
To: Eli Zaretskii; +Cc: Tom Tromey, emacs-devel
On Dez 11 2016, Eli Zaretskii <eliz@gnu.org> wrote:
> When Emacs stops due to a breakpoint, the thread that is the current
> one is in sync between GDB and Emacs. IOW, the current_thread
> variable describes the same thread on which GDB commands will act.
> But as soon as you say something like "thread 1" at the GDB prompt,
> this synchronization is lost: GDB acts on the thread you specified,
> while current_thread is still pointing at the thread which was the
> current one when Emacs stopped.
Why isn't current_thread a thread-local variable?
Andreas.
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Debugging Emacs with threads
2016-12-11 16:43 ` Andreas Schwab
@ 2016-12-11 17:37 ` Eli Zaretskii
2016-12-11 18:04 ` Andreas Schwab
2016-12-12 3:07 ` Ken Raeburn
1 sibling, 1 reply; 8+ messages in thread
From: Eli Zaretskii @ 2016-12-11 17:37 UTC (permalink / raw
To: Andreas Schwab; +Cc: tom, emacs-devel
> From: Andreas Schwab <schwab@linux-m68k.org>
> Cc: Tom Tromey <tom@tromey.com>, emacs-devel@gnu.org
> Date: Sun, 11 Dec 2016 17:43:56 +0100
>
> Why isn't current_thread a thread-local variable?
I don't know. Is that sufficiently portable for us to use?
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Debugging Emacs with threads
2016-12-11 17:37 ` Eli Zaretskii
@ 2016-12-11 18:04 ` Andreas Schwab
2016-12-11 18:12 ` Eli Zaretskii
0 siblings, 1 reply; 8+ messages in thread
From: Andreas Schwab @ 2016-12-11 18:04 UTC (permalink / raw
To: Eli Zaretskii; +Cc: tom, emacs-devel
On Dez 11 2016, Eli Zaretskii <eliz@gnu.org> wrote:
>> From: Andreas Schwab <schwab@linux-m68k.org>
>> Cc: Tom Tromey <tom@tromey.com>, emacs-devel@gnu.org
>> Date: Sun, 11 Dec 2016 17:43:56 +0100
>>
>> Why isn't current_thread a thread-local variable?
>
> I don't know. Is that sufficiently portable for us to use?
If __thread isn't available it can simulated with pthread_getspecific.
Andreas.
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Debugging Emacs with threads
2016-12-11 18:04 ` Andreas Schwab
@ 2016-12-11 18:12 ` Eli Zaretskii
0 siblings, 0 replies; 8+ messages in thread
From: Eli Zaretskii @ 2016-12-11 18:12 UTC (permalink / raw
To: Andreas Schwab; +Cc: tom, emacs-devel
> From: Andreas Schwab <schwab@linux-m68k.org>
> Cc: tom@tromey.com, emacs-devel@gnu.org
> Date: Sun, 11 Dec 2016 19:04:20 +0100
>
> On Dez 11 2016, Eli Zaretskii <eliz@gnu.org> wrote:
>
> >> Why isn't current_thread a thread-local variable?
> >
> > I don't know. Is that sufficiently portable for us to use?
>
> If __thread isn't available it can simulated with pthread_getspecific.
Btw, there are several places in thread.c which compare the value of
current_thread with some other value. I guess these will have to be
reviewed if current_thread is going to be in TLS.
Tom, any comments?
Thanks.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Debugging Emacs with threads
2016-12-11 16:43 ` Andreas Schwab
2016-12-11 17:37 ` Eli Zaretskii
@ 2016-12-12 3:07 ` Ken Raeburn
1 sibling, 0 replies; 8+ messages in thread
From: Ken Raeburn @ 2016-12-12 3:07 UTC (permalink / raw
To: Andreas Schwab; +Cc: Eli Zaretskii, Tom Tromey, emacs-devel
On Dec 11, 2016, at 11:43, Andreas Schwab <schwab@linux-m68k.org> wrote:
> On Dez 11 2016, Eli Zaretskii <eliz@gnu.org> wrote:
>
>> When Emacs stops due to a breakpoint, the thread that is the current
>> one is in sync between GDB and Emacs. IOW, the current_thread
>> variable describes the same thread on which GDB commands will act.
>> But as soon as you say something like "thread 1" at the GDB prompt,
>> this synchronization is lost: GDB acts on the thread you specified,
>> while current_thread is still pointing at the thread which was the
>> current one when Emacs stopped.
>
> Why isn't current_thread a thread-local variable?
Multiple C threads can be running at once, but our current model for Lisp is that only one thread runs at a time; current_thread is the indicator of which thread that is, and that’s global to the process.
When we actually decide to start running a different Lisp thread from the “current” one, we need to save the current dynamic state (let bindings, etc) and restore that of the alternate thread. The thread wanting to “enter” the Lisp environment acquires the global lock, saves the old thread’s state, updates current_thread, restores its own state, and does its business.
This way, threads can release and re-acquire the global lock without having to save and restore all their dynamic state each time, which is a win if we release the lock more often than we actually switch between Lisp threads.
Ken
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Debugging Emacs with threads
2016-12-11 16:26 Debugging Emacs with threads Eli Zaretskii
2016-12-11 16:43 ` Andreas Schwab
@ 2016-12-12 3:18 ` Ken Raeburn
2016-12-12 17:38 ` Eli Zaretskii
1 sibling, 1 reply; 8+ messages in thread
From: Ken Raeburn @ 2016-12-12 3:18 UTC (permalink / raw
To: Eli Zaretskii; +Cc: Tom Tromey, emacs-devel
On Dec 11, 2016, at 11:26, Eli Zaretskii <eliz@gnu.org> wrote:
> There's a nasty issue that people who debug Emacs with threads need to
> be aware of.
>
> When Emacs stops due to a breakpoint, the thread that is the current
> one is in sync between GDB and Emacs. IOW, the current_thread
> variable describes the same thread on which GDB commands will act.
> But as soon as you say something like "thread 1" at the GDB prompt,
> this synchronization is lost: GDB acts on the thread you specified,
> while current_thread is still pointing at the thread which was the
> current one when Emacs stopped.
>
> The result of this is that invoking functions in Emacs will likely
> crash, because thread-specific variables used by Emacs are incorrect
> for the thread whose stack GDB will use to create the call-stack frame
> for the function it calls. In particular, xbacktrace will definitely
> crash. And since src/.gdbinit automatically invokes xbacktrace when
> you type "bt", any "bt" issued when the threads mismatch will ruin the
> debugging session.
>
> It would be good to protect xbacktrace from this, and avoid calling
> functions in Emacs in this situation. Unfortunately, I couldn't find
> any way of gleaning the OS thread ID from the GDB thread number.
> Maybe Tom, who knows a lot about GDB, could help, or someone else
> might have an idea that we then could implement in .gdbinit?
Perhaps a new thread-local variable which has the value of current_thread when the thread is the current Lisp thread. Or we could invoke sys_thread_self() to compare against current_thread->thread_id; it’s not like xbacktrace works on a core file anyway. (I’m assuming GDB will invoke a function or look at TLS in the context of the current OS thread, or the appropriate OS thread when invoked via something like “thread apply all bt”.)
Ken
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Debugging Emacs with threads
2016-12-12 3:18 ` Ken Raeburn
@ 2016-12-12 17:38 ` Eli Zaretskii
0 siblings, 0 replies; 8+ messages in thread
From: Eli Zaretskii @ 2016-12-12 17:38 UTC (permalink / raw
To: Ken Raeburn; +Cc: tom, emacs-devel
> From: Ken Raeburn <raeburn@raeburn.org>
> Date: Sun, 11 Dec 2016 22:18:16 -0500
> Cc: Tom Tromey <tom@tromey.com>,
> emacs-devel@gnu.org
>
> Perhaps a new thread-local variable which has the value of current_thread when the thread is the current Lisp thread. Or we could invoke sys_thread_self() to compare against current_thread->thread_id; it’s not like xbacktrace works on a core file anyway. (I’m assuming GDB will invoke a function or look at TLS in the context of the current OS thread, or the appropriate OS thread when invoked via something like “thread apply all bt”.)
Thanks, I will see if I can solve the problem that way.
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2016-12-12 17:38 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-12-11 16:26 Debugging Emacs with threads Eli Zaretskii
2016-12-11 16:43 ` Andreas Schwab
2016-12-11 17:37 ` Eli Zaretskii
2016-12-11 18:04 ` Andreas Schwab
2016-12-11 18:12 ` Eli Zaretskii
2016-12-12 3:07 ` Ken Raeburn
2016-12-12 3:18 ` Ken Raeburn
2016-12-12 17:38 ` Eli Zaretskii
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.