Inject variable value in lldb command - lldb

Is it possible to do that with lldb:
(lldb) p my_var
(uint64_t) $9 = 2
(lldb) set set target.max-children-count 4
But instead of 4, I would like to call the set command with the current value of my_var, in this case 2.

In most lldb commands, backtick blocks are evaluated and the result substituted in their place. For instance,
(lldb) sett show stop-line-count-before
stop-line-count-before (int) = 3
(lldb) p 5
(int) $1 = 5
(lldb) sett set stop-line-count-before `$1`
(lldb) sett show stop-line-count-before
stop-line-count-before (int) = 5

Related

bash how to shift through initial arguments passed to a function

I want to send multiple parameters to a function in bash. How can I accomplish this so the function parses though each parameter properly?
Would like to avoid having to use eval if possible.
Here is the code I am trying to use.
#!/bin/bash
arr_files=(
test_file
test_file1
test_file2
)
user=user10
group=user10
cp_chmod_chown(){
# $1 = chmod value
# $2 = chown value
# $3 = array of files
chmod_value=$1
shift
chown_value=$2
shift
arr=("$#")
for i in "${arr[#]}"; do
echo arr value: $i
done
echo chmod_value: $chmod_value
echo chown_value: $chown_value
}
cp_chmod_chown "644" "$user:$group" "${arr_files[#]}"
However I am not able to shift out of the first two parameters properly so the parameters get jumbled together in the array. Here is the output after running the above script, you can see chown_value is the first value in the array for some reason:
# ./cp_arra_chmod_chown.sh
arr value: test_file
arr value: test_file1
arr value: test_file2
chmod_value: 644
chown_value: test_file
I tried putting the parameters in different orders, and using quotes and not using quotes, nothing I tried seems to work.
How can I pass multiple parameters to a function?
After shift, all values get shifted. Next code is wrong, as after first shift, former $2 becomes $1 :
chmod_value="$1"
shift
chown_value="$2"
shift
You should instead write :
chmod_value="$1"
shift
chown_value="$1"
shift
Or, if you prefer:
chmod_value="$1"
chown_value="$2"
shift 2

Can't get array from cmd line argument bash

For some reason in the code below for loop treats args as array and wc -l can count the lines correctly but I can't get the $(#args[#]) to produce the correct count
function doSomthing() {
local i args a
args=$1;
a=("1" "2" "3" "4");
i=0
echo wc =`wc -l <<< "$args"`;
for arg in $args; do
((i++))
echo "$i"
done;
echo i = $i
echo a = ${#a[#]}
echo args = ${#args[#]}
echo $args
}
The output of this function is
$> doSomthing $'1\n2\n3\n4'
wc = 4
1
2
3
4
i = 4
a = 4
args = 1
1 2 3 4
args is not an array; it is simply a string that contains embedded newlines. That means, if you try to treat it as an array, it will appear as if you defined it as
args=( $'1\n2\n3\4' )
not
args=(1 2 3 4)
Problem solved!
I just needed to put $1 inside parenthesis.
For some reason when I tried it before it did not work but now it does.
Here is the new code:
function doSomthing () {
local i args a;
args=( $1 );
a=("1" "2" "3" "4");
i=0;
echo wc =`wc -l <<< "$args"`;
for arg in $args; do
((i++));
echo "$i";
done;
echo i = $i;
echo a = ${#a[#]};
echo args = ${#args[#]};
echo ${args[#]}
}
And here is the new output:
$> doSomthing $'1\n2\n3\n4'
wc = 1
1
i = 1
a = 4
args = 4
1 2 3 4
Cheers ;-)

How to restart naming GDB's internal variables from $1?

By default, GDB's internal variables will be $1, $2, $3, .... How to restart naming them from $1?
(gdb) p v1
$1 = 7
(gdb) p v2
$2 = 8
(gdb) p v3
$3 = 9
(gdb) ??? // what should be put here?
$1 = 0
Looking at the documentation, there's no explicit command to clear the value history.
It does mention that the file and symbol-file commands, which can change the symbol table, clear the history.
Also, you can use output instead of print to avoid putting the printed value in the value history.

Shell Script - Find length of an array passed as parameter to a function

I am trying to access my array passed as parameter to my function inside my shell script. I am able to process individual element of the array but when I try to find length of the array, it only shows 1. My script is:
ar=(8 2 3 4 5 6 7)
test() {
sum=0
ref=$1[#]
echo ${#ref[#]}
for i in ${!ref}
do
sum=`expr $sum + $i`
done
echo "sum is: $sum"
}
test ar
If I do
echo ${#ref}
it gives me 5 as result. Dont know the reason for this result as well.
Is there any way I can find a way out of this ?
ts () {
set $1[*]
ref=(${!1})
echo ${#ref[*]}
for i in ${ref[*]}
do
(( sm += i ))
done
echo sum is: $sm
}
ar=(8 2 3 4 5 6 7)
ts ar
Output
7
sum is: 35

Gdb conditional regex break

Does gdb allow conditional regex breaks?
I have a source file timer.c, an int64_t ticks, and a function timer_ticks() which returns it.
Neither
rbreak timer.c:. if ticks >= 24
nor
rbreak timer.c:. if ticks_ticks() >= 24
place any breakpoints.
If I remove however either the regex part or the conditional part, the breakpoints are set.
Here's a way to get it done. It takes a couple of steps, and requires some visual inspection of gdb's output.
First, run the rbreak command and note the breakpoint numbers it sets.
(gdb) rbreak f.c:.
Breakpoint 1 at 0x80486a7: file f.c, line 41.
int f();
Breakpoint 2 at 0x80486ac: file f.c, line 42.
int g();
Breakpoint 3 at 0x80486b1: file f.c, line 43.
int h();
Breakpoint 4 at 0x8048569: file f.c, line 8.
int main(int, char **);
Now, loop through that range of breakpoints and use the cond command to add the condition to each:
(gdb) set $i = 1
(gdb) while ($i <= 4)
>cond $i ticks >= 24
>set $i = $i + 1
>end
(gdb) info breakpoints
Num Type Disp Enb Address What
1 breakpoint keep y 0x080486a7 in f at f.c:41
stop only if ticks >= 24
2 breakpoint keep y 0x080486ac in g at f.c:42
stop only if ticks >= 24
3 breakpoint keep y 0x080486b1 in h at f.c:43
stop only if ticks >= 24
4 breakpoint keep y 0x08048569 in main at f.c:8
stop only if ticks >= 24
Unfortunately, no, it doesn't.
However, there is workaround.
You want to break every function from the file conditionally, don't you?
If yes, you can use this answer as start point and then create conditional breaks.
So, first step: get a list of functions in the file:
nm a.out | grep ' T ' | addr2line -fe a.out |
grep -B1 'foo\.cpp' | grep -v 'foo\.cpp' > funclist
Next: create a gdb script that creates breaks:
sed 's/^/break /' funclist | sed 's/$/ if ticks>=24/' > stop-in-foo.gdb
And finally, import script in gdb:
(gdb): source stop-in-foo.gdb

Resources