Ian Wienand: Debugging __thead variables from coredumps
I think the old-fashioned coredump is a little under-appreciated
these days. I'm not sure when it changed, but I even had to add
myself to /etc/security/limits.conf to raise my
ulimit to even create one.
Anyway, debugging __thread variables from coredumps is a
bit of a pain. For the uninitiated, the __thread identifier
specifies a thread-local variable, i.e. every thread gets its
own copy of the variable automatically.
The implementation of this is highly architecture specific. The
reason is that TLS entries need to be accessed via a register kept as
part of the thread state, and thus every architecture chooses their
own register and builds their own ABI. On x86-32, which is very
register-limited, you certainly don't want to dedicate a register to a
pointer to TLS variables and take it out of operation. Luckily there
is the hang-over from the 70's (60's? 50's?) segmentation.
Without going into real detail, segment registers can be used to
offset into a region of memory based on a look-up of a region
descriptor stored in a table.
Above, you see a simplified example of the %gs register
loaded with the index value 2, and thus when you access
%gs:20 what you are saying is "find entry 2 in the global
descriptor table (GDT), follow it and offset 20 into that
region.
The kernel gives each thread its own GDT (i.e. the GDT register is
part of the thread-state). Thus __thread variables are
stored based on segment offsets and voila thread-local
storage. Now, there's a few tricks here. For various reasons, a
process can not setup entries in the GDT; this is a privileged
operation that must be done by the kernel. There is actually a
special system call for threads to setup their TLS areas in the GDT
set_thread_area. When a new thread starts, the
thread-library and dynamic linker conspire to allocate and load any
static TLS data (i.e. if you have a global __thread variable
initialised to some value, then every thread must see that value when
it starts) and then calls this to make sure the variables are ready to
go. After that, the gs register is filled with the index of
that GDT entry, and all TLS access goes via it. That, in a nut-shell,
is TLS for x86-32.
Now, to the problem. Take, for example, the following short
program:
We start a few threads and then abort to make it dump core. But,
if you try and examine foo:
It seems that gdb doesn't know how to find the value of
foo because its not a variable in the usual sense ("this
target", in this case, means a coredump). It relies on accessing via
the gs register, which relies on the current processes' GDT
state, which has since been destroyed. If you care to consult the canonical source of
TLS info, you can find out exactly why this is so hard to figure out
generically. However, with some work, we can start to figure out the
value by hand.
A coredump is really a ELF file full of just two things: a bunch of
LOAD segments that are just dumps of the process memory
regions, and a NOTE section that includes a bunch of notes
that the kernel dumps out for us such as the current register state,
the process id, the signal that killed us, etc. Here's an example of
a core file under readelf
You can examine the notes; with readelf we see:
This shows 5 notes of type NT_PRSTATUS; it should be
little surprise that each of these notes describes the process status
of one running thread. When gdb pops up [New Thread 4973]
that's because it just hit another note that describes the new
thread.
To actually make sense of the note, however, we need some other
tools that look deeper. The elfutils based tools give us a better
description of the various notes in the coredump; more akin to what
GDB is interpreting them as. Below I've extracted one thread's
info:
What's particularly interesting here is that the note type that was
previously unknown (Unknown note type: (0x00000200)) has been
resolved for us it is in fact of type NT_386_TLS and
is a dump of the GDT entries for the thread.
So, if we examine how our function is accessing our TLS variable by
disassembling the thread function:
Examining the disassembly we can see that foo is accessed
via an offset of -4 from %gs (this is OK, as our limit value
is maxed out. See the TLS ABI doc for more info). Now, we can
examine gs and see which selector it is telling us to
use:
Above we shift out the last 3 bits, as these refer to the privilege
level (bits 0 and 1) and if this is a GDT or LDT reference (bit 2).
Thus, looking at the GDT descriptor for index 6:
we can finally do some maths from the base-address to figure out
the value:
Thus we have found our TLS value; in this case for thread 3 the
value is indeed 3. I caution this is the simplest possible case;
other "models" (see the TLS doc, again) may not be so simple to work
out by hand, but this would certainly be how you would start.
Above, you see a simplified example of the %gs register
loaded with the index value 2, and thus when you access
%gs:20 what you are saying is "find entry 2 in the global
descriptor table (GDT), follow it and offset 20 into that
region.
The kernel gives each thread its own GDT (i.e. the GDT register is
part of the thread-state). Thus __thread variables are
stored based on segment offsets and voila thread-local
storage. Now, there's a few tricks here. For various reasons, a
process can not setup entries in the GDT; this is a privileged
operation that must be done by the kernel. There is actually a
special system call for threads to setup their TLS areas in the GDT
set_thread_area. When a new thread starts, the
thread-library and dynamic linker conspire to allocate and load any
static TLS data (i.e. if you have a global __thread variable
initialised to some value, then every thread must see that value when
it starts) and then calls this to make sure the variables are ready to
go. After that, the gs register is filled with the index of
that GDT entry, and all TLS access goes via it. That, in a nut-shell,
is TLS for x86-32.
Now, to the problem. Take, for example, the following short
program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int __thread foo;
void* thread(void *in)
foo = (int)in;
printf("foo is %d\n", foo);
while (1)
sleep(10);
int main(void)
pthread_t threads[5];
int i;
for (i=0; i<5; i++)
pthread_create(&threads[i], NULL,
thread, (void*)i);
sleep(5);
abort();
$ gdb ./thread core GNU gdb (GDB) 7.2-debian Reading symbols from /home/ianw/tmp/thread/thread...done. [New Thread 4970] [New Thread 4975] [New Thread 4974] [New Thread 4973] [New Thread 4972] [New Thread 4971] Core was generated by ./thread'. Program terminated with signal 6, Aborted. #0 0xffffe424 in __kernel_vsyscall () (gdb) print foo Cannot find thread-local variables on this target
$ readelf --headers ./core ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 ... OS/ABI: UNIX - System V ... Type: CORE (Core file) ... There are no sections in this file. Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align NOTE 0x0003d4 0x00000000 0x00000000 0x006b4 0x00000 0 LOAD 0x001000 0x08048000 0x00000000 0x00000 0x01000 R E 0x1000 LOAD 0x001000 0x08049000 0x00000000 0x01000 0x01000 RW 0x1000 LOAD 0x002000 0x0804a000 0x00000000 0x21000 0x21000 RW 0x1000 LOAD 0x023000 0x46015000 0x00000000 0x00000 0x1b000 R E 0x1000 LOAD 0x023000 0x46030000 0x00000000 0x01000 0x01000 R 0x1000 LOAD 0x024000 0x46031000 0x00000000 0x01000 0x01000 RW 0x1000 ...
$ readelf --notes ./core Notes at offset 0x000003d4 with length 0x000006b4: Owner Data size Description CORE 0x00000090 NT_PRSTATUS (prstatus structure) CORE 0x0000007c NT_PRPSINFO (prpsinfo structure) CORE 0x000000a0 NT_AUXV (auxiliary vector) LINUX 0x00000030 Unknown note type: (0x00000200) CORE 0x00000090 NT_PRSTATUS (prstatus structure) LINUX 0x00000030 Unknown note type: (0x00000200) CORE 0x00000090 NT_PRSTATUS (prstatus structure) LINUX 0x00000030 Unknown note type: (0x00000200) CORE 0x00000090 NT_PRSTATUS (prstatus structure) LINUX 0x00000030 Unknown note type: (0x00000200) CORE 0x00000090 NT_PRSTATUS (prstatus structure) LINUX 0x00000030 Unknown note type: (0x00000200) CORE 0x00000090 NT_PRSTATUS (prstatus structure) LINUX 0x00000030 Unknown note type: (0x00000200)
$ eu-readelf --notes core
...
CORE 144 PRSTATUS
info.si_signo: 6, info.si_code: 0, info.si_errno: 0, cursig: 6
sigpend: <>
sighold: <>
pid: 4970, ppid: 4960, pgrp: 4970, sid: 4960
utime: 0.000000, stime: 0.000000, cutime: 0.000000, cstime: 0.000000
orig_eax: 270, fpvalid: 0
ebx: 4970 ecx: 4970 edx: 6
esi: 0 edi: 1176018932 ebp: 0xbfa5b710
eax: 0 eip: 0xffffe424 eflags: 0x00000206
esp: 0xbfa5b6f8
ds: 0x007b es: 0x007b fs: 0x0000 gs: 0x0033 cs: 0x0073 ss: 0x007b
LINUX 48 386_TLS
index: 6, base: 0xb6043b70, limit: 0x000fffff, flags: 0x00000051
index: 7, base: 0x00000000, limit: 0x00000000, flags: 0x00000028
index: 8, base: 0x00000000, limit: 0x00000000, flags: 0x00000028
(gdb) disassemble thread Dump of assembler code for function thread: 0x08048514 <+0>: push %ebp 0x08048515 <+1>: mov %esp,%ebp 0x08048517 <+3>: sub $0x18,%esp 0x0804851a <+6>: mov 0x8(%ebp),%eax 0x0804851d <+9>: mov %eax,%gs:0xfffffffc 0x08048523 <+15>: mov %gs:0xfffffffc,%edx 0x0804852a <+22>: mov $0x8048670,%eax 0x0804852f <+27>: mov %edx,0x4(%esp) 0x08048533 <+31>: mov %eax,(%esp) 0x08048536 <+34>: call 0x80483f4 <printf@plt> 0x0804853b <+39>: movl $0xa,(%esp) 0x08048542 <+46>: call 0x8048404 <sleep@plt> 0x08048547 <+51>: jmp 0x804853b <thread+39>
(gdb) print $gs >> 3 $31 = 6
LINUX 48 386_TLS
index: 6, base: 0xb6043b70, limit: 0x000fffff, flags: 0x00000051
(gdb) print *(int*)(0xb6043b70 - 4) $34 = 3
My
A while ago I registered with
So as Alpha 2 approaches, I find myself working towards porting the alternate installer/d-i to the pandaboard to support the netboot installer. There's not a lot of documentation that describes the internals of d-i, nor what bits are platform specific.
I often get requests of persons who would like to contribute to Debian but who don t know where to start. Let s try to answer this question properly so that I can give out this URL the next time that I am asked.
The Debian website has a
You re a volunteer, you re not doing stuff for Debian because someone told you so. You must have some intrinsic motivation and the ultimate motivation is usually that you re enjoying what you re doing.
So what are you enjoying and/or what are your motivations ?
Find something to do
At this point you have a generic idea of what you d like to do but you re still missing a concrete objective. Let s try to find one, we ll explore several ways to do this.
Scratch your itch
The best objectives are those that satisfy your own needs. Here are some examples:
If you don t have a specific itch to scratch, you might want to focus your work on a specific team. Head over to
You can concentrate your work on a specific package even if you re in a team, it s often a good idea. But I list it separately because not all packages are team maintained and you might want to help maintain a package where there s a single maintainer currently. I leave it up to you to find a way to select a package that interests you
Then head over to the package tracking system:
Today I was made aware of bug
After testing Emacs, Eclipse, KDevelop and CodeBlocks for writing C++ code, I decided to stick with Netbeans. It seems to be the most simple to configure and yet full of features and plugins. I was having one problem with it that when I focused out the window, and then focused on it again, it would not really grab the focus, in the sense that I would not be able to type without first clicking with the mouse. At first I thought the problem was with Netbeans, then with the JRE. I tried using sun-java6-jre, I tried upgrading my openjdk-6-jre, and nothing worked. I searched a little bit more and got to
Trying the BitTorrent DHT
A month ago, noticing that there was a lack of a good BitTorrent tracker
software (good meaning complying with my random requirements ), I
I am involved in developing something coined as the
For some years, tech media has tried predict when Linux have reached
momentum for ordinary users. That current or next year was to become
I have bought a
I've been in Europe for the last couple weeks but pretty occupied with
things like attending my brother wedding and a series of outdoor
excursions in Spain.
Today
I've taken a tiny step towards getting together
a prototype toolset for developing an initial development iteration
of the NAS functionality of the FreedomBox.
Here's what I am trying to do:
Right around the time I immigrated to the US Alex Trebek began a revival of
a TV game show originally hosted by Art Fleming called
It has been over 5 years since we voted on the GNU FDL.
At the time I was misled by certain people who insisted
that they had inside knowledge and that the GFDL was going
to be fixed within the year.
The FDL is still a bad license, and the successor meant
to be less bad, the GSFDL, has not progressed beyond
a discussion draft from September 2006.
Personally, I feel dirty having voted the way I did.
We compromised our principles and ended up in a stalemate
that many people on either side of the issue are still
unhappy about to this day.
Hopefully I have learned my lesson.
Some error messages can be confusing at times. Most of the error strings do not appear very often and developers thus tend to use very terse description of the underlying problem. In other cases the architecture of the software makes it difficult to pin-point the real problem because the part that displays the error is several layers above the one that generated the initial error.
This is for example the case with this error of dpkg:
I sponsored the upload of fonts-anonymous-pro prepared by Rog rio
Brito. This fixed-width font is especially designed for people who
write code, for instance by making clear differences between
characters such as O and 0, or I, l and 1.
I fixed a few glitches in various l10n scripts related to D-I (those
tthigns that run daily, for which I receive a daily mail). They were
the consequence of Alioth migration and I omitted to fix them until
now....mumbling every morning about the scripts' noisy output..:-)
I worked on only one bug for samba (doh). This was #631118 which I
forwarded upstream in order to get it fixed in 3.5 series (even if 3.6
is due out soon and will be what we target for wheezy).
I tried to follow the flow of translations/reviews in DDTP for
French. The activity is high, here, with about 10 packages translated
every day (most often packages I target for the team...by using
high-popcon stats). We even had a big bump during the week-end when
Georges Khaznadar (who is very active when it comes at educative
software) initiated the translation of many edu-related packages (many
of which he maintains himself).
That's about it. Quiet week, indeed with more time invested in paid
work, including during my spare time..:-). And also a week-end in our
country house in Normandy, which usually means more gardening than hacking.