gcc

Linux System Call Table in JSON

One-liner to generate a JSON object mapping syscall names to their numbers:

1
2
3
4
5
6
7
echo -n '{'; gcc -M /usr/include/asm/unistd.h | tr -d '\\\n' | cut -d' ' -f2- | xargs cat | cpp -E -fpreprocessed -dM | awk '/__NR_/ {print $3 " " $2}' | sort -h | awk 'NR > 1 {printf "," } {printf "\"%s\": %s\n", substr($2, 6), $1}'; echo '}'
{"read": 0
,"write": 1
,"open": 2
,"close": 3
,"stat": 4
...

Optionally, minify the JSON by piping it through tr -d' \n':

1
2
(echo -n '{'; gcc -M /usr/include/asm/unistd.h | tr -d '\\\n' | cut -d' ' -f2- | xargs cat | cpp -E -fpreprocessed -dM | awk '/__NR_/ {print $3 " " $2}' | sort -h | awk 'NR > 1 {printf "," } {printf "\"%s\": %s\n", substr($2, 6), $1}'; echo '}') | tr -d ' \n'
{"read":0,"write":1,"open":2,"close":3,...

A reverse mapping can be made by swapping the columns in the awk expression:

1
2
3
4
5
6
7
echo -n '{'; gcc -M /usr/include/asm/unistd.h | tr -d '\\\n' | cut -d' ' -f2- | xargs cat | cpp -E -fpreprocessed -dM | awk '/__NR_/ {print $3 " " $2}' | sort -h | awk 'NR > 1 {printf "," } {printf "\"%s\": \"%s\"\n", $1, substr($2, 6)}'; echo '}';
{"0": "read"
,"1": "write"
,"2": "open"
,"3": "close"
,"4": "stat"
...

Print Header File Includes After Preprocessing

You can use gcc -M to print all the dependencies of a C source file. For example:

1
gcc -I../my/include/path -M my_header.h

To see the resulting file after all those includes, you can cat them together:

1
gcc -I../my/include/path -M my_header.h | tr -d '\\\n' | cut -d' ' -f2- | xargs cat

Then, to print all the preprocessor #defines, excluding the predefined ones:

1
gcc -I../my/include/path -M my_header.h | tr -d '\\\n' | cut -d' ' -f2- | xargs cat | cpp -E -fpreprocessed -dM