Chad Mynhier
2008-Mar-04 01:58 UTC
[dtrace-discuss] Determining curthread pointer (ulwp_t) via libproc on i386
How would I determine the address of the ulwp_t structure for the
current thread of a process from a 32-bit i386 executable looking at a
32-bit executable? (This is in support of a fix for 6593259:
"libdtrace should prefer pid probes to breakpoints".)
For example, if I compile the following code 64-bit snippet, I can get
that address from 32-bit or 64-bit executables:
--------------------------------------------------------------------------------
if ((Pr = Pgrab(pid, NULL, &gret)) == NULL) {
fprintf(stderr, "cannot control process %d\n", pid);
exit(1);
}
if ((lwp = &Pstatus(Pr)->pr_lwp) == NULL) {
fprintf(stderr, "Couldn''t grab pr_lwp\n");
exit(1);
}
#if defined(__amd64)
if (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) {
addr = lwp->pr_reg[REG_FSBASE];
} else {
addr = lwp->pr_reg[REG_GSBASE];
}
#else
#error "Need to figure this out"
#endif
printf("ulwp_t is 0x%lx\n", addr);
--------------------------------------------------------------------------------
But things don''t seem to be as simple as that for the 32-bit/32-bit
case. From usr/src/uts/intel/dtrace/fasttrap_isa.c, we have this code
for the i386 case (to get the address of the thread-local scratch
space used for fasttrap probes, which is just after the ul_self
pointer):
--------------------------------------------------------------------------------
/*
* Compute the address of the ulwp_t and step over the
* ul_self pointer. The method used to store the user-land
* thread pointer is very different on 32- and 64-bit
* kernels.
*/
[ ... ]
#elif defined(__i386)
addr = USEGD_GETBASE(&lwp->lwp_pcb.pcb_gsdesc);
addr += sizeof (void *);
#endif
--------------------------------------------------------------------------------
So we''re using pcb_gsdesc, but it doesn''t appear that this is
exposed
to userland via libproc (unless I''ve missed something.)
I''ve coded something similar to mdb''s "::walk ulwps"
that suits my
purpose, but I''d rather be able to determine the ulwp_t for the
current thread than walk that list. (I''m trying to match %pc against
the thread-local scratch space, so I have a way of searching that
list.)
(I haven''t come up to speed on understanding the Intel architecture
segmentation stuff, so I''m hoping someone can help out here.)
Thanks,
Chad Mynhier
Seemingly Similar Threads
- access to errno when using pid provider
- Debian 6.0 + Xen4.0 + FreeBSD hvm amd64 -> fpudna: fpcurthread == curthread XXXX times
- Asynchronous signal handling in fasttrap provider
- [RFC-v5] tcm_vhost: Initial merge for vhost level target fabric driver
- [RFC-v5] tcm_vhost: Initial merge for vhost level target fabric driver
