[PATCH 2/5] Assume KERN_PROC_PATHNAME is present on FreeBSD hosts.

John Baldwin jhb@FreeBSD.org
Wed Jul 22 16:44:03 GMT 2020


On 7/20/20 7:16 PM, Simon Marchi wrote:
> On 2020-07-20 1:31 p.m., John Baldwin wrote:
>> FreeBSD kernels have included this sysctl since 6.0 release.  The most
>> recent release without support is 5.5 which was released in May of
>> 2006.
>>
>> gdb/ChangeLog:
>>
>> 	* fbsd-nat.c (fbsd_nat_target::pid_to_exec_file): Always use
>> 	sysctl and remove procfs fallback.
>> ---
>>  gdb/ChangeLog  |  5 +++++
>>  gdb/fbsd-nat.c | 13 -------------
>>  2 files changed, 5 insertions(+), 13 deletions(-)
>>
>> diff --git a/gdb/ChangeLog b/gdb/ChangeLog
>> index 687d9aede5..0339083f1a 100644
>> --- a/gdb/ChangeLog
>> +++ b/gdb/ChangeLog
>> @@ -1,3 +1,8 @@
>> +2020-07-20  John Baldwin  <jhb@FreeBSD.org>
>> +
>> +	* fbsd-nat.c (fbsd_nat_target::pid_to_exec_file): Always use
>> +	sysctl and remove procfs fallback.
>> +
>>  2020-07-20  John Baldwin  <jhb@FreeBSD.org>
>>  
>>  	* fbsd-nat.c: Assume PT_LWPINFO is always defined.
>> diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
>> index fc7136a97c..6193e0fbde 100644
>> --- a/gdb/fbsd-nat.c
>> +++ b/gdb/fbsd-nat.c
>> @@ -53,11 +53,7 @@
>>  char *
>>  fbsd_nat_target::pid_to_exec_file (int pid)
>>  {
>> -  ssize_t len;
>>    static char buf[PATH_MAX];
>> -  char name[PATH_MAX];
>> -
>> -#ifdef KERN_PROC_PATHNAME
>>    size_t buflen;
>>    int mib[4];
>>  
>> @@ -71,15 +67,6 @@ fbsd_nat_target::pid_to_exec_file (int pid)
>>         for processes without an associated executable such as kernel
>>         processes.  */
>>      return buflen == 0 ? NULL : buf;
>> -#endif
>> -
>> -  xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
>> -  len = readlink (name, buf, PATH_MAX - 1);
>> -  if (len != -1)
>> -    {
>> -      buf[len] = '\0';
>> -      return buf;
>> -    }
>>  
>>    return NULL;
>>  }
>> -- 
>> 2.25.1
>>
> 
> Note that the way the code is currently written allows the readlink method to be used
> if the sysctl method fails at runtime.  We probably don't care, but I thought I'd raise
> it just in case.

Yes, we don't care in this case.  Both the sysctl and the /proc/<pid>/exe file end
up resolving to the same function in the kernel (vn_fullpath).  If it fails for the
sysctl, the readlink() call will also fail.  I will add that to the commit message
though so it is clear to future readers.

-- 
John Baldwin


More information about the Gdb-patches mailing list