kprobe_events fetch-args works for x86 but not arm64 - linux-kernel

I wanted to get do_sys_open filename argument as string. For this i added kprobe following kprobetrace.txt. A simple probe which gives filename as hex works for both x86/arm64.
x86: echo 'p:myprobe do_sys_open filename_string=%si' > kprobe_events
arm64: echo 'p:myprobe do_sys_open filename_string=%x1' > kprobe_events
However changing probe to get string for file name works on x86 but not arm64(ie cannot get string representation filename_string=(fault))
x86:
echo 'p:myprobe do_sys_open filename_string=+0(%si):string' > kprobe_events
output:
adb-30551 [001] d... 4570187.407426: myprobe: (do_sys_open+0x0/0x270) filename_string="/dev/bus/usb/001/001"
arm64:
echo 'p:myprobe do_sys_open filename_string=+0(%x1):string' > kprobe_events
output:
netd-4621 [001] d... 8491.094187: myprobe: (do_sys_open+0x0/0x24c) filename_string=(fault)
To check if i was using arm ABI correctly i tried setting probe using perf.
The probe created by perf as seen from /sys/kernel/debug/tracing/kprobe_events was similar
./perf4.14 probe 'do_sys_open filename:string'
/d/tracing # cat kprobe_events
p:kprobes/myprobe do_sys_open filename_string=+0(%x1):string
But perf probe was also failing (ie filename_string="") in this case.
./perf4.14 record -e probe:do_sys_open -aR sleep 3
/data/local/tmp # ./perf4.14 script
perf4.14 4587 [007] 7490.809036: probe:do_sys_open: (ffffff8337060148) filename_string=""
sleep 4588 [003] 7490.817937: probe:do_sys_open: (ffffff8337060148) filename_string=""
What would be the correct way to set kprobe_events for arm to fetch args as string?
Am i using the ABI incorrectly?

On kernel version >= 4.20, you can use $argN to fetch the Nth function argument. From kernel 4.20 kprobetrace.rst:
FETCHARGS : Arguments. Each probe can have up to 128 args.
.....
.....
$argN : Fetch the Nth function argument. (N >= 1) (\*1)
Since the filename is second argument of do_sys_open(), you should give $arg2 in the kprobe event, like this:
echo 'p:myprobe do_sys_open filename_string=+0($arg2):string' > kprobe_events
This should work on both x86 and arm64.

Related

Retrieve const string value from .elf binary via variable name using command line utility?

Consider the following main.c:
#include <stdio.h>
const char greeting[] = "hello world";
int main() {
printf("%s!\n", greeting);
return 0;
}
I compiled this in Ubuntu with:
gcc -g main.c -o main.exe
I would like to retrieve the value of the variable named greeting; considering it is const, it won't change, so it should be possible to retrieve the value "hello world" from the executable.
Basically, I can see the variable name in the binary using:
$ readelf -p .rodata main.exe | grep hello
[ 8] hello world
... and I can see the value using:
$ readelf -s main.exe | grep greeting
59: 0000000000002008 12 OBJECT GLOBAL DEFAULT 18 greeting
I could try parsing the output of readelf -s and readelf -p to get what I want (retrieve the value of the variable named greeting), but I'm pretty sure I'll mess it up.
So is there some combination of switches of bintools utilities (or any command line program, really), which would perform the equivalent of the following pseudocode:
$ [tool] --get-value-of-variable-name greeting --program=main.exe
"hello world"
or even:
$ [tool] --verbose --get-value-of-variable-name greeting --program=main.exe
The constant value of the variable "greeting" in `main.exe` is:
is there some combination of switches of bintools utilities (or any command line program, really), which would perform the equivalent of the following pseudocode:
Sure:
you need to find the section in which the symbol resides, and the address within that section, and the length of data, and
you need to find where in the file the section itself starts, and
you need to dump length bytes from the right offset in the file.
Getting this all together (my file has slightly different data from yours):
readelf -Ws main.exe | grep greeting
29: 0000000000002008 12 OBJECT GLOBAL DEFAULT 17 greeting
readelf -WS main.exe | grep '\[17\]'
[17] .rodata PROGBITS 0000000000002000 002000 000019 00 A 0 0 8
This tells me that I need to dump 12 bytes (actually 11, since I don't want the terminating \0), starting of offset 0x2000 + (0x2008 (symbol address) - 0x2000 (section address)).
dd if=main.exe bs=1 skip=$((0x2008)) count=11 2>/dev/null
hello world
Now, parsing this data out from readelf output is more trouble than it's worth -- it's much easier to write a simple C++ program to produce the desired output. Using ELFIO should make this very easy.

Why is bash breaking MPI job control loop

I'm attempting to use a simple bash script to sequentially run a batch of MPI jobs. This script works perfectly when running serial code (I am using Fortran 90), but for some reason bash breaks out of the loop when I attempt to execute MPI code.
I already found a work-around to the problem. I just wrote essentially the exact same script in Perl and it worked like a charm. I just really want to understand the issue here because I prefer the simplicity of bash and it perfectly fits my own scripting needs in almost all other cases.
I've tried running the MPI code as a background process and using wait with the same result. If I run the jobs in the background without using wait, bash does not break out of the loop, but it stacks up jobs until eventually crashing. The goal is to run the executable sequentially for each parameter set anyway, I just wanted to note that the loop is not broken in that case.
Bash Script, interp.sh: Usage --> $ ./interp.sh inputfile
#!/bin/bash
PROG=$1
IFILE=$2
kount=0 # Counter variable for looping through input file
sys=0 # Counter variable to store how many times model has been run
while IFS="\n" read -r line
do
kount=$(( $kount + 1 ))
if [ $(( kount % 2 )) -eq 1 ] # if kount is even, then expect headers
then
unset name defs
sys=$(( $sys + 1 ))
name=( $line ) # parse headers
defs=${#name[*]}
k=$(( $defs - 1 ))
else # if count is odd, then expect numbers
unset vals
vals=( $line ) # parse parameters
for i in $( seq 0 $k )
do
# Define variables using header names and set their values
printf -v "${name[i]}" "${vals[i]}"
done
# Print input variable values
echo $a $b $c $d $e $nPROC
# Run executable
mpiexec -np $nPROC --oversubscribe --hostfile my_hostfile $PROG
fi
done < $IFILE
Input file, input.dat:
a b c d e nPROC
1 2 3 4 5 2
nPROC
3
nPROC
4
nPROC
5
nPROC
6
nPROC
7
nPROC
8
Sample MPI f90 code, main.f90:
program main
use mpi
implicit none
integer :: i, ierr, myID, nPROC
integer, parameter :: foolen = 100000
double precision, dimension(0:foolen) :: foo
call MPI_INIT(ierr)
call MPI_COMM_SIZE(MPI_COMM_WORLD, nPROC, ierr)
call MPI_COMM_RANK(MPI_COMM_WORLD, myID, ierr)
if ( myID .eq. 0 ) then
do i=0,foolen
foo(i) = i
end do
else
do i=0,foolen
foo(i) = i
end do
end if
call MPI_FINALIZE(ierr)
end program
Sample makefile:
COMP=mpif90
EXT=f90
CFLAGs=-Wall -Wextra -Wimplicit-interface -fPIC -fmax-errors=1 -g -fcheck=all \
-fbacktrace
MPIflags=--oversubscribe --hostfile my_hostfile
PROG=main.x
INPUT=input.dat
OUTPUT=output
OBJS=main.o
$(PROG): $(OBJS)
$(COMP) $(CFLAGS) -o $(PROG) $(OBJS) $(LFLAGS)
main.o: main.f90
$(COMP) -c $(CFLAGs) main.f90
%.o: %.f90
$(COMP) -c $(CFLAGs) $<
run:
make && make clean
./interp.sh $(PROG) $(INPUT)
clean:
rm -f *.o DONE watch
my_hostfile
localhost slots=4
Note that if the mpiexec line is commented out, the script runs as expected. The output looks like this:
1 2 3 4 5 2
1 2 3 4 5 3
1 2 3 4 5 4
1 2 3 4 5 5
1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 5 8
These are the parameter values which are supposed to be passed to the MPI code in each loop. However, when mpiexec is called in the script, only the first set of parameters is read and passed.
I apologize if all that is a bit excessive, I just wanted to provide all that is needed for testing. Any help solving the issue in bash or explanation of why this happens would be greatly appreciated!
mpiexec is consuming the stdin thus reading all remaining lines in the loop. So after the first loop stdin is empty and the loop breaks.
This is an issue that occurs not only with loops calling mpiexec from whithin but also with loops other commands that consumes stdin by default such as ssh.
The general solution is to use < /dev/null so that the offending command won't consume stdin but the /dev/null instead. Some commands have special flags to replace the redirect command such as ssh -n
so the solution in this case would be to add the redirect at the end of the line where mpiexec is called:
mpiexec -np $nPROC --oversubscribe --hostfile my_hostfile $PROG < /dev/null
there are some issues to pay attention to in the case of mpiexec related to Standard I/O detailed here: https://www.open-mpi.org/doc/v3.0/man1/mpiexec.1.php#toc14

Makefile and command line different behaviour

I am currently working on a bigger project where i want to test executable file with few different codes as input.
I call it like this ./test < code1
and after command echo $?, it shows last returned value [0, 1, 2, ..]
I wanted to automate is, so i created call in makefile like this :
#makefile
[...]
test :
./test < code1
#echo $$?
./test < code2
#echo $$?
[...]
[...]
So i can call make test.
When program returns 0 as success, everything works fine. But when program has to return something else than 0, it shows me this :
./test < code3
Makefile:19: recipe for target 'test' failed
make: *** [test[ Error 2
Weird thing is, when i try to call program with code which made it crash in command line like :
./test < code3; echo $?
It works perfectly and shows me last exit status ( for exapmle 3 ).
I am confused now, because i thought it should work the same. Can someone help me out?
Thank you!
See this answer: https://stackoverflow.com/a/41452754/939557
You need to put the echo into the same logical line as your test invocation:
test :
./test < code1; echo $$?
./test < code2; echo $$?

GNU override target?

I'm wondering if it's possible to override a target in a makefile! The environment I'm working in does not allow me to do this due to auto generation! I was wondering if I coded the same rule above or below the static target would this achieve an override?
%_emul.flist: $(if ${GEN_FLIST},%_synth.flist,) ${rdlh_file_deps}
${QUIET}if test ${SYN_DEBUG} -eq 1 ; then set -xv ; fi; \
$(if ${TOOL_VERILOG},rm -f $#; touch $#,$(if ${TOOL_BBOX_LIBS},echo ${TOOL_BBOX_LIBS} > $#,rm -f $#; touch $#))
/bin/sed -e '/\/libs\//d' -e '/\/place\//d' $(foreach mod,$(filter %.vhd,$^),-e 's%^\(.*\/\)\{0,1\}$(basename $(notdir ${mod}))\.v$$%${mod}%') $*_synth.flist >> $#
Yes , i think that would work .... but you need to be a bit more careful in the way you code things. You don't want to override something that might be useful!
GNU make would take the most recent of the target it encounters. So, the following works (but not as i would have liked it to work :( )
Output: I think you are looking for something like this --
Kaizen ~/make_prac $ make -nf mk.name
mk.name:20: warning: overriding recipe for target `name'
mk.name:17: warning: ignoring old recipe for target `name'
arg1="Kaizen" ;
echo "hello "" ;" ;
hello ;
Code: Here the target "name" appears twice and is overridden.
Kaizen ~/make_prac $ cat mk.name
##
## make to accept name and display hello name
##
arg1="" ;
.PHONY : name \
hello
#.DEFAULT :
# hello
hello : name
+ echo "hello $(arg1)" ;
name :
echo "name given is : $(arg1)" ;
name :
arg1="Kaizen" ;
PS: Take note of the use of : -- if you use :: then both rules get executed.
Explanation for the arg1 .... not showing in the output: The variable arg1, even though it gets assigned in the first parsing, it gets ignored, since its assignment is target dependent. If you would have had a variable declaration elsewhere -- e.g. like arg1 is defined at the start -- there would not be any dereferencing issues.

Running IDL program from bash with variables

I've written a program in IDL to generate scatter plots based on command line arguments. I can successfully call the program directly in the terminal like this:
idl -e "scatterplot_1_2d_file.pro" $infile $outfile $title $xtitle $ytitle $xmin $xmax $ymin $ymax $timescale
Where $* refer to some string literals typed in. The problem is, I thought I'd be able to just type that very line, putting in variable names in place of the literals, into a bash script, and generate a million scatter plots while I'm on break. Unfortunately, if I do it that way, I get the error:
idl: -e option cannot be specified with batch files
So my next attempt was to try writing those commands to an IDL batch file that I'd then run.
That attempt looks like this:
#!/bin/bash
indir=/path/to/indir/
outdir=/path/to/outdir/
files=`ls $indir`
batchfile=/path/to/tempbatchfile.pro
echo .r "/path/to/scatterplot_1_2d_file.pro" >> $batchfile
for file in $files
do
name=${file%\.*}
echo scatterplot_1_2d_file $indir$name.txt $outdir$name.jpg $name "Gauge Precipitation (mm)" "NMQ Precipitation (mm)" "*" "*" "*" "*" 2 >> $batchfile
done #done file
echo exit >> $batchfile
idl <<EOF
#/path/to/scatterplot_1_2d_file
EOF
rm $batchfile
I don't know if the bulk of the errors that script generates are relevant, so I'll just post the start and I'll post the rest later if you need it:
[foo]$ bash script_thing.sh
IDL Version 6.3 (linux x86 m32). (c) 2006, Research Systems, Inc.
Installation number: 91418.
Licensed for personal use by XXXXXXXXX only.
All other use is strictly prohibited.
PRO scatterplot_1_2d_file
^
% Programs can't be compiled from single statement mode.
At: /path/to/scatterplot_1_2d_file.pro, Line 1
% Attempt to subscript ARGS with <INT ( 1)> is out of range.
% Execution halted at: $MAIN$
% Attempt to subscript ARGS with <INT ( 2)> is out of range.
% Execution halted at: $MAIN$
% Attempt to subscript ARGS with <INT ( 3)> is out of range.
% Execution halted at: $MAIN$
% Attempt to subscript ARGS with <INT ( 4)> is out of range.
% Execution halted at: $MAIN$
% Attempt to subscript ARGS with <INT ( 5)> is out of range.
% Execution halted at: $MAIN$
% Attempt to subscript ARGS with <INT ( 6)> is out of range.
% Execution halted at: $MAIN$
% Attempt to subscript ARGS with <INT ( 7)> is out of range.
% Execution halted at: $MAIN$
% Attempt to subscript ARGS with <INT ( 8)> is out of range.
% Execution halted at: $MAIN$
% Attempt to subscript ARGS with <INT ( 9)> is out of range.
% Execution halted at: $MAIN$
I don't know if I'm just trying to do something that can't be done, but it doesn't SEEM like it. Any advice?
There are two ways to go about this: using COMMAND_LINE_ARGS or constructing a valid IDL routine call. This routine uses both:
pro test, other_args
compile_opt strictarr
args = command_line_args(count=nargs)
help, nargs
if (nargs gt 0L) then print, args
help, other_args
if (n_elements(other_args) gt 0L) then print, other_args
end
Call it from the command line in either of the following two ways:
Desktop$ idl -e "test" -args $MODE
IDL Version 8.2, Mac OS X (darwin x86_64 m64). (c) 2012, Exelis Visual Information Solutions, Inc.
Installation number: 216855.
Licensed for use by: Tech-X Corporation
% Compiled module: TEST.
NARGS LONG = 1
test
OTHER_ARGS UNDEFINED = <Undefined>
Desktop$ idl -e "test, '$MODE'"
IDL Version 8.2, Mac OS X (darwin x86_64 m64). (c) 2012, Exelis Visual Information Solutions, Inc.
Installation number: 216855.
Licensed for use by: Tech-X Corporation
% Compiled module: TEST.
NARGS LONG = 0
OTHER_ARGS STRING = 'test'
test
I don't know IDL, but here are fixes for your Bash script that are likely to help:
#!/bin/bash
indir=/path/to/indir/
outdir=/path/to/outdir/
# (commented out) files=`ls $indir` # no, just no
batchfile=/path/to/tempbatchfile.pro
echo ".r /path/to/scatterplot_1_2d_file.pro" > "$batchfile" # overwrite the file on the first write, put everything inside the quotes
for file in "$indir/"*
do
name=${file%\.*}
echo "scatterplot_1_2d_file $indir$name.txt $outdir$name.jpg $name Gauge Precipitation (mm) NMQ Precipitation (mm) * * * * 2" # quote the whole thing once, it's simpler and echo doesn't do anything differently
done >> "$batchfile" # do all the output from the loop
echo exit >> "$batchfile"
# *** where does idl learn the location of "$batchfile"? ***
idl <<EOF
#/path/to/scatterplot_1_2d_file
EOF
rm "$batchfile"
To fix your command line version, use quoting:
idl -e "scatterplot_1_2d_file.pro" "$infile" "$outfile" "$title" "$xtitle" "$ytitle" "$xmin" "$xmax" "$ymin" "$ymax" "$timescale"
Always quote variables when they're expanded.

Resources