Re: [PATCH v9] scripts/bash_aliases: Add useful functions

Alejandro Colomar (man-pages)
2021-02-27T17:20:33Z

Hi Stefan,

On 2/27/21 4:09 PM, Stefan Puiu wrote:
> Sorry for jumping in this discussion so late, but I was wondering
> about one thing (see below).
>
> On Fri, Feb 19, 2021 at 4:38 PM Alejandro Colomar
> <alx.manpages@gmail.com> wrote:
>> +# grep_syscall() finds the prototype of a syscall in the kernel sources,
>> +# printing the filename, line number, and the prototype.
>> +# It should be run from the root of the linux kernel source tree.
>> +# Usage example: .../linux$ grep_syscall openat2;
>> +
>> +function grep_syscall()
>> +{
>> + if ! [ -v 1 ]; then
>> + >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
>> + return ${EX_USAGE};
>> + fi
>> +
>> + find * -type f \
>> + |grep '\.c$' \
>> + |sort -V \
>> + |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
>> + |sed -E 's/^[^:]+:[0-9]+:/&\n/';
>> +
>> + find * -type f \
>> + |grep '\.[ch]$' \
>
> Any reason not to use "find . -type f -name '*.[ch]'" when you need to
> restrict the files you're looking at? I would expect that to be
> faster.

I don't like find syntax. I never remember how all of its options work.
grep is much simpler, and everyone knows how it works.

find has: -[i]lname, -[i]name, -[i]path, -[i]regex, -[i]wholename
I don't want to be reading the manual for all of them each time I use
find. grep does the same with optional -i and some simple regex which
anyone could understand with some basic regex knowledge.

For the performance part, I don't know; but we might be surprised. At
most it might be a bit faster (nothing like 200%), but I care more about
readability.

I also avoid using find -exec option, and instead use xargs. It's way
simpler to understand, at least for me.

See also:
<http://doc.cat-v.org/unix/find-history>
<http://harmful.cat-v.org/cat-v/>

> Also, not sure what you are trying to do with 'sort -V' - why
> not feed the results directly to pcregrep?

Some consistency. Sometimes this function finds more than one prototype
for a syscall. I prefer that everyone using this function gets the
results in the same predictable order, instead of some random order.

>
>> + |sort -V \
>> + |xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
>> + |sed -E 's/^[^:]+:[0-9]+:/&\n/';
>> +}

Regards,

Alex

--
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/