From owner-freebsd-hackers@freebsd.org Sun Aug 12 20:58:48 2018 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id C3272107BD70 for ; Sun, 12 Aug 2018 20:58:48 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 27BF87900C for ; Sun, 12 Aug 2018 20:58:48 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id w7CKwZrn028433 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Sun, 12 Aug 2018 23:58:38 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua w7CKwZrn028433 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id w7CKwZp5028432; Sun, 12 Aug 2018 23:58:35 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Sun, 12 Aug 2018 23:58:35 +0300 From: Konstantin Belousov To: Willem Jan Withagen Cc: FreeBSD Hackers Subject: Re: Write a version for pthread_get_name_np Message-ID: <20180812205835.GB2340@kib.kiev.ua> References: <7fa2b876-9397-da2b-cb29-56badf11b66d@ecoracks.nl> <42689feb-9bd4-429b-63d7-b9193123ee47@digiware.nl> MIME-Version: 1.0 Content-Type: text/plain; charset=koi8-r Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <42689feb-9bd4-429b-63d7-b9193123ee47@digiware.nl> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on tom.home X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 12 Aug 2018 20:58:48 -0000 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.