Can anybody please tell me what is the difference b/w the two types of stacks.
If I see /proc/<pid>/map
and proc/pid/task/<tid>
I see same map. Is there a way we can see the stack belonging to thread exclusively (I mean not the stack of process thread) or if there is any gdb command to find out thread specific stack.
Thanks,
Kapil
Is there a way we can see the stack belonging to thread exclusively
There is no such thing: all the threads share the entire address space, so the stack doesn't "belong exclusively" to any given thread. In fact, you can take an address of a local variable, and pass that address to a different thread, which can then read or write values to it.
What I believe you are asking is "how to tell which memory region in /proc/<pid>/maps
is thread X currently using as its stack?". If that's the question, you can print $sp
to find out current stack pointer for the thread you are interested in, and then find a region in /proc/<pid>/maps
that overlaps $sp
.
you can list all threads using info threads
and switch to a specific thread using thread <id>
you can type thread apply all info registers
to print the current registers of all threads.
or for instance thread apply all bt
to print backtraces for all threads.
@Employedrussian
There is no such thing: all the threads share the entire address space, so the stack
doesn't "belong exclusively" to any given thread. In fact, you can take an address of a
local variable, and pass that address to a different thread, which can then read or write
values to it.
What I believe you are asking is "how to tell which memory region in /proc/<pid>/maps is
thread X currently using as its stack?". If that's the question, you can print $sp to
find out current stack pointer for the thread you are interested in, and then find a
region in /proc/<pid>/maps that overlaps $sp.
Right, they share entire address space and its also true that the threads have the stack of their own, but still this does not explains how the stack of a thread different from that of a another thread or athe process thread. I mean, if this is the way we can visualize it:
+--------+ stack vma start
| +--+ |
| +--+ <------- stack of process
| +--+ |
| +--+ |
| : : |
| |
| |
| +--+ |
| +--+ <------- stack of thread1
| +--+ |
| +--+ |
| : : |
| |
| |
| +--+ |
| +--+ |
| +--+ <------ stack of thread2
| +--+ |
| : : |
: :
: :
+--------+ stack vma end
(may be that i am completely wrong in this, but this is just an attempt to clarify the things)
Regarding passing of an address (of a local variable), When you pass that as an address you can you read or write to that memory location, that's inherent property with pointer.
Just for the sake of completeness, I am posint here what ever i could understand.
The diagram which is posted above is wrong and should be modified this way:
Process address Space:
+----------------------------------------------------+
| |
: :
: :
| |
| +--------+ thread2 stack vma start |
| | +--+ | |
| | +--+ | |
| | +--+ | |
| | +--+ | | stack grows downwards |
| | : : | | |
| : : V |
| : : |
| +--------+ thread2 stack vma ends |
| |
| |
| +--------+ thread1 stack vma start |
| | +--+ | |
| | +--+ | |
| | +--+ | |
| | +--+ | | stack grows downwards |
| | : : | | |
| : : V |
| : : |
| +--------+ thread1 stack vma ends |
| |
| |
| +--------+ Process stack vma start |
| | +--+ | |
| | +--+ | |
| | +--+ | |
| | +--+ | | stack grows downwards |
| | : : | | |
| : : V |
: : : :
: +--------+ Process stack vma ends :
: :
+----------------------------------------------------+
The thereads get their separate stacks from the mmap'd memory. This i am talking about the POSIX implementation in glibc. For better reference consult function allocate_stack () in
nptl in glibc.