This is an archive of the discontinued LLVM Phabricator instance.

Implement argv0-less getMainExecutable for FreeBSD
AcceptedPublic

Authored by emaste on Oct 8 2014, 8:01 PM.

Details

Reviewers
samsonov
joerg
Summary

The llvm-symbolizer patch in D5610 needs to call getMainExecutable without argv0.

Today this works on Linux, which reads /proc/self/exe, but does not on FreeBSD and other operating systems, which determine the exe name from argv0.

On FreeBSD the exe name can be found from the kern.proc.pathname sysctl. This probably works on other BSDs as well; others can confirm and update the #ifdefs.

Diff Detail

Event Timeline

emaste updated this revision to Diff 14622.Oct 8 2014, 8:01 PM
emaste retitled this revision from to Implement argv0-less getMainExecutable for FreeBSD.
emaste updated this object.
emaste edited the test plan for this revision. (Show Details)
emaste added a reviewer: samsonov.
emaste added a subscriber: Unknown Object (MLST).
emaste updated this revision to Diff 14664.Oct 9 2014, 11:16 AM
emaste added a reviewer: joerg.

Add NetBSD #ifdefs

joerg edited edge metadata.Oct 9 2014, 11:32 AM

I would prefer using dl_iterate_phdr for this, I think.

I would prefer using dl_iterate_phdr for this, I think.

Ahh, good point - for some reason I thought it did not include the executable itself, but a quick test confirms it behaves as expected on FreeBSD stable/10 at least. That said, I don't see a guarantee that the first result returned is the executable.

joerg added a comment.Oct 9 2014, 12:34 PM

Hm. Right, it's not required to be. OK, I guess the sysctl is the best answer, __ps_strings might have a relative path or so.

samsonov accepted this revision.Oct 10 2014, 2:39 PM
samsonov edited edge metadata.

LGTM

This revision is now accepted and ready to land.Oct 10 2014, 2:39 PM

Looks like patch was not committed.

jbeich added a subscriber: jbeich.Sep 25 2019, 3:07 PM
jbeich added inline comments.
lib/Support/Unix/Path.inc
51

Add || defined(__DragonFly__) as it has KERN_PROC_PATHNAME.

167

Add || defined(__DragonFly__) as its KERN_PROC_PATHNAME is similar to FreeBSD.

171

Not valid on NetBSD, see sysctl(7). Try instead:

#if defined(__NetBSD__)
  int name[4] = { CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
#else
  int name[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
#endif
173

getpid() can be optimized to -1. Other LLVM consumers of KERN_PROC_PATHNAME already use -1.