Utility for finding source code for executable in FreeBSD?

Hi!

I've had a brief look at Plan 9 and it looks quite an interesting operating system. I find some utilities very nice and one of them is actually src(1), which finds a source code for an executable.

Honestly, I really miss this-like utility in FreeBSD: sometimes I slightly modify the source code in /usr/src to fit my needs, or sometimes I'm just curious how this or that program works and I want to look into its source. Usually, I do something like this to figure this out:
find /usr/src -name "ed" - in order to find sources for ed(1), for example. However, this particular approach doesn't always work, 'cause sometimes the directories (or even files) do not match the name of their executable.

I'm aware that there is devel/plan9port that provides "src" utility as well, but I'm a bit confused that I have to install all the rest of the programs that this port provides just in order to get only one I really need. Moreover, for some reason I feel that this particular program (the idea behind it - finding the sources) does not require "mimicking" another operating system and "stealing" its programs as is.

I haven't find anybody being interested in this, so I want to ask here: is there an utility in FreeBSD (maybe in the base, maybe in the ports) that is similar to Plan 9's src(1) tool? The reason I'm particularly interested about FreeBSD is that FreeBSD is shipped with its source code and I suppose it should be possible to implement such a tool and it's gonna fit right in, in my opinion.

Thank you in advance.

Artem
 
What doesn't work? Any other example? Never had problems with finding the source of world parts. If it's GNU, then it's in contrib, right?
 
What doesn't work? Any other example? Never had problems with finding the source of world parts. If it's GNU, then it's in contrib, right?
Hi MG,

I agree, cases where it's not that easy to find the source are rare. However, it may be hard to find sources for stuff like strip(1). It's at /usr/bin/strip, but the source for it is in /usr/src/contrib/elftoolchain/elfcopy, not in /usr/src/usr.bin. It's still possible to locate it, but find(1) trick won't work.

So I just thought, maybe there is a tool that can do the job unambiguously?
 
Code:
#!/bin/sh
TOP=/usr/src
for mf in $(find ${TOP}/bin/ ${TOP}/sbin/ ${TOP}/usr.bin/ ${TOP}/usr.sbin/ \
                 ${TOP}/libexec/ ${TOP}/secure/usr.bin/ ${TOP}/secure/usr.sbin/ \
                 ${TOP}/cddl/*bin \
                 -type f -name Makefile -maxdepth 3)
do
mfd=$(dirname $mf)
grep -q PROG= $mf && make .CURDIR=$mfd  -V '${BINDIR}/${PROGNAME} ${LINKS} ${.PATH}' -f $mf
done
crude approximation
for strip will output something like this
/usr/bin/objcopy /usr/bin/objcopy /usr/bin/strip . /usr/src/usr.bin/objcopy /usr/src/contrib/elftoolchain/elfcopy
where before "." are command aliases and after src dirs
 
Code:
#!/bin/sh
TOP=/usr/src
for mf in $(find ${TOP}/bin/ ${TOP}/sbin/ ${TOP}/usr.bin/ ${TOP}/usr.sbin/ \
                 ${TOP}/libexec/ ${TOP}/secure/usr.bin/ ${TOP}/secure/usr.sbin/ \
                 ${TOP}/cddl/*bin \
                 -type f -name Makefile -maxdepth 3)
do
mfd=$(dirname $mf)
grep -q PROG= $mf && make .CURDIR=$mfd  -V '${BINDIR}/${PROGNAME} ${LINKS} ${.PATH}' -f $mf
done
Looks cool, thank you!

I think I will upgrade your script a bit: I suppose the output of it may be dumped into say /usr/src/src.index file (or maybe somebody will suggest better location) and then another script can grep(1) targets we're interested in.
 
Code:
% whereis -s cc
cc: /usr/ports/devel/py-pyperscan/files/cc
% whereis -s clang
clang: /usr/src/usr.bin/clang

In the example above, only the latter works as expected.
Not a complete solution to the OP's problem, but in cases with hard links whereis(1) may still be useful:
Code:
$ whereis -qsx $(find /usr/bin -samefile /usr/bin/cc)
/usr/src/usr.bin/clang
For strip(1), however, you have to look into the Makefile anyway:
Code:
$ whereis -qsx $(find /usr/bin -samefile /usr/bin/strip)
/usr/src/usr.bin/objcopy
 
Hi MG,

I agree, cases where it's not that easy to find the source are rare. However, it may be hard to find sources for stuff like strip(1). It's at /usr/bin/strip, but the source for it is in /usr/src/contrib/elftoolchain/elfcopy, not in /usr/src/usr.bin. It's still possible to locate it, but find(1) trick won't work.

So I just thought, maybe there is a tool that can do the job unambiguously?
Never noticed. Not even a file with the name "strip" exists but the Makefile in /usr/src/contrib/elftoolchain/elfcopy refers to it:
Code:
LINKS=  ${BINDIR}/elfcopy ${BINDIR}/mcs         \
        ${BINDIR}/elfcopy ${BINDIR}/objcopy     \
        ${BINDIR}/elfcopy ${BINDIR}/strip

EXTRA_TARGETS=  mcs strip objcopy

Not sure if there's a standard for it. I think these references should somehow be indexed to easily find back the source that the executables are made from. It looks like this is the only mention.
In my opinion, this should already exist. Knowing the origin of all system files is relevant, with or without installed source
 
I think these references should somehow be indexed to easily find back the source that the executables are made from.
There seems to be no (completed) standards for it.
Some are in the form you mentioned for strip, but some have Makefile alone in /usr/src/usr.bin/ like xz.
Just in my humble opinion, every (non-builtin) commands in base should have a directory for which the command is installed 1:1 (like xz), and for something like strip / objcopy / mcs (hardlinked for multiple commands) should contain readme.txt (or readme.md) describing which directory to look into for its actual Makefile, and make whereis to understand it.
 
I can suggest to look into Makefiles with recursive grep

Bash:
grep -r -w --include 'Makefile' 'strip' .

and regexp could be modified with more complex and more correct statement
 
Back
Top