Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 13 Aug 2018 00:01:03 +0200
From:      Willem Jan Withagen <wjw@digiware.nl>
To:        Konstantin Belousov <kostikbel@gmail.com>
Cc:        FreeBSD Hackers <freebsd-hackers@freebsd.org>
Subject:   Re: Write a version for pthread_get_name_np
Message-ID:  <bdf7e3bd-e33f-019e-c6ff-a10e927e3a01@digiware.nl>
In-Reply-To: <20180812205835.GB2340@kib.kiev.ua>
References:  <7fa2b876-9397-da2b-cb29-56badf11b66d@ecoracks.nl> <42689feb-9bd4-429b-63d7-b9193123ee47@digiware.nl> <20180812205835.GB2340@kib.kiev.ua>

next in thread | previous in thread | raw e-mail | index | archive | help
On 12/08/2018 22:58, Konstantin Belousov wrote:
> On Sun, Aug 12, 2018 at 10:33:06PM +0200, Willem Jan Withagen wrote:
>> Hi,
>>
>> For some porting I'm trying to write a pthread_get_name_np(), but
>> I have run into a snag....
>> Tried some the the procstat code I would come up with:
>>
>> int pthread_get_name_np(pthread_t *thread,
>>   ššššššššššššššššššššššš char *name, size_t len){
>>   ššš struct procstat *procstat;
>>   ššš int pid = getpid();
>>   ššš unsigned int count, i;
>>   ššš struct kinfo_proc *kip, *kipp;
>>
>>   ššš procstat = procstat_open_sysctl();
>>   ššš kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
>>   ššššššššššš pid, &count);
>>   ššš for (i = 0; i < count; i++) {
>>   ššš ššš kipp = &kip[i];
>>   ššš ššš printf("thread: %ld(0x%lx), %ld(0x%lx)\n", thread, thread,
>> kipp->ki_tid, kipp->ki_tid);
>>   ššš ššš if (thread == kipp->ki_tid) {
>>   ššš ššš ššš kinfo_proc_thread_name(kipp, name, len);
>>   ššš ššš }
>>   ššš }
>>   ššš return 0;
>> }
>>
>> Problem only is that the TID value in what procstat_getprocs returns in
>> ki_tid is nowhere near
>> what pthread_self() returns.
>> But both manual page and the include file describe the value as Thread ID.
>> Only in sys/user.h the type is
>>   ššš lwpid_t ki_tid;šššššššššššššššš /* XXXKSE thread id */
>>
>> What do I need to translate one tid into the other so I can really
>> compare them?
> 
> You need to cast pthread_t * to struct thread *, this is an internal
> libthr structure which represents the thread in userspace.  The
> structure field tid gives you the tid you want to pass to sysctl.

mmmm,

I'm afraid I'm not quit able to followup on your answer.

Trying to find what to include to be able to cast this, I can only find 
a small bit defined in /usr/src/lib/libthr/thread/thr_private.h.
/*
  * lwpid_t is 32bit but kernel thr API exports tid as long type
  * to preserve the ABI for M:N model in very early date (r131431).
  */
#define TID(thread)     ((uint32_t) ((thread)->tid))

But that I cannot just "include" that file without a lot of mess, and 
then still it does not compile.

So could you point me to where this private part of struct thread is 
hidding?

Thanx,
--WjW






Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?bdf7e3bd-e33f-019e-c6ff-a10e927e3a01>