I can't get this simple LLDB alias to work - iphone

I want to create an LLDB alias ps, such that
ps foo
becomes
print [self foo]
I've been watching the LLDB talk (WWDC session 321 on iTunes), and based on that, it looks like the alias to do that should be this one:
command alias ps print [ self %1 ]
but it doesn't work. Here I've given my app delegate a simple "count" method that returns an integer:
(lldb) command alias ps print [ self %1 ]
(lldb) ps count
error: invalid operands to binary expression ('AppDelegate *' and 'int')
error: 1 errors parsing expression
(lldb) print [ self count ]
(int) $6 = 2
(lldb)
What am I missing?

It seems arguments (%1, %2, etc) doesn't work to alias an expression. There is a workaround by using a regular expression instead:
command regex ps 's/(.+)/print [self %1]/'
It makes an alias ps for the above regular expression:
(lldb) ps firstName
print [self firstName]
(NSString *) $1 = 0x06e64e20 #"John"
However this will last till the debug session ends. You'll have to enter it again for the next debug session. If you want your ps command to persist through debug sessions, you'll have to save it in your ~/.lldbinit file (if it doesn't exist, create one).
See llvm blog for more deails about regex command.

Related

GDB: How remove/replace pattern from a variable?

I found the similar post here and here. But those solution seems like replacing the pattern from a file, not from a variable.
I have a function in a gdb script, which takes an address of a structure as $arg0 and prints the fields of that structure like following,
Function:
define call_rb_node
set $rb_left_child= (*(struct rb_node*)$arg0).rb_left
set $rb_right_child= (*(struct rb_node*)$arg0).rb_right
# printing fields
p $rb_left_child
p $rb_right_child
end
Output:
$1 = (struct rb_node *) 0xffff888236960f88
$2 = (struct rb_node *) 0xffff888236be92c8
I want to remove this (struct rb_node *) from both $rb_left_child and $rb_right_child. I want my output to be like
$rb_left_child = 0xffff888236960f88
$rb_right_child = 0xffff888236be92c8
I was trying to use sed (don't know if I have any other options) to do that, but it's not working, I tried the following approach, but it's not working
define call_rb_node
set $rb_left_child= (*(struct rb_node*)$arg0).rb_left
set $rb_right_child= (*(struct rb_node*)$arg0).rb_right
set $rb_left_child = $(sed /(struct rb_node *) /d $rb_left_child)
# printing fields
p $rb_left_child
end
I'm getting the following error
Error in sourced command file:
No symbol "sed" in current context.
I replace the line like this,
shell echo set \$left_child=$(sed '/(struct rb_node *) /d' "$rb_left_child") > gdb.tmp
source gdb.tmp
p $left_child
but keep getting the following error,
Error in sourced command file: gdb.tmp:1: Error in sourced command file:
A syntax error in expression, near `'.
please help.

How to combine CLI arguments as variables within a perl script

I am trying to write a script that basically executes a cli command like:
snmpget -v 1 -c xxxxxx-Ovq xx.xx.xx.xxx .1.3.6.1.2.1.1.8.0
where xxxxx is a password and xx.xx.xx.xxx and IP that normally returns:
49:22:12:15.00
My script is:
#!/usr/local/bin/perl
#snmpget -v 1 -c xxxxx -Ovq xx.xx.xx.xxx .1.3.6.1.2.1.1.8.0
$SNMP_GET_CMD = "snmpget -v1 -c xxxxx-Ovq";
$SNMP_TARGET = "xx.xx.xx.xxx";
my $sysORLastChange = '${SNMP_GET_CMD} ${SNMP_TARGET} .1.3.6.1.2.1.1.8.0';
chomp($sysORLastChange);
print("${SNMP_TARGET} as an Input Line Reading of ${sysORLastChange}\n");
and the output is:
xx.xx.xx.xxx as an Input Line Reading of ${SNMP_GET_CMD} ${SNMP_TARGET} .1.3.6.1.2.1.1.8.0
It should return the following:
xx.xx.xx.xxx as an Input Line Reading of 49:22:12:15.00
Is there any problem with the syntax i used in the script?
In Perl, use double-quotes to interpolate another variable into a string. When you define $sysORLastChange using other variables within a single-quoted string like this:
my $sysORLastChange = '${SNMP_GET_CMD} ${SNMP_TARGET} .1.3.6.1.2.1.1.8.0';
...the string is being assigned verbatim (ie. the inner variables aren't being expanded).
To correct this, assign to the variable using double-quotes, which will interpolate the inner variables into their values:
my $sysORLastChange = "${SNMP_GET_CMD} ${SNMP_TARGET} .1.3.6.1.2.1.1.8.0";
If you want to actually execute the string, you can use the qx() operator, aka the "backtick" style quotes:
my $sysORLastChange = qx(${SNMP_GET_CMD} ${SNMP_TARGET} .1.3.6.1.2.1.1.8.0);
# or...
my $sysORLastChange = `${SNMP_GET_CMD} ${SNMP_TARGET} .1.3.6.1.2.1.1.8.0`;
See Perl Quote and Quote-like Operators in perlop.

Why is my command line argument being interpreted as a Boolean (Perl 6)?

Given this program:
#!/bin/env perl6
sub MAIN ($filename='test.fq', :$seed=floor(now) )
{
say "Seed is $seed";
}
When I run it without any command line arguments, it works fine. However, when I give it a command line argument for seed, it says that its value is True:
./seed.p6 --seed 1234
Seed is True
Why is the number 1234 being interpreted as a boolean?
Perl 6's MAIN argument handling plays well with gradual typing. Arguments can, and should be typecast to reduce ambiguity and improve validation:
#!/bin/env perl6
sub MAIN (Str $filename='test.fq', Int :$seed=floor(now))
{
say "Seed is $seed.";
}
After typecasting seed to Int, this option must be given a numeric argument and no longer defaults to a Boolean:
perl6 ./seed.pl -seed 1234
Usage:
./seed.pl [--seed=<Int>] [<filename>]
perl6 ./seed.pl -seed=abc
Usage:
./seed.pl [--seed=<Int>] [<filename>]
perl6 ./seed.pl -seed=1234
Seed is 1234.
You need to use an = sign between your option --seed and its value 1234:
./seed.p6 --seed=1234
Since you have a positional argument in your MAIN subroutine signature (i.e. $filename), the first argument not tied to an value with an = sign will be assigned to it.
Your original
./seed.p6 --seed 1234
was being interpreted as if 1234 were the filename (i.e. it was assigned to the variable $filename). Since a command line option without an argument is considered to be True, $seed was being assigned True in your original invocation of that script.

powershell: add pscustomobject to ArrayList, but all items goto 1st property

I just created a simple function "f" that adds an pscustomobject element into an arraylist, but when displaying it, result is not what I expected:
$c=New-Object System.Collections.ArrayList($null)
function f([string]$a1,[string]$a2)
{
$c.Add([PSCustomObject]#{item1=$a1;item2=$a2})
}
f("kkk","aaa")
$c
result is:
item1 item2
----- -----
kkk aaa
It seems to me that both "kkk" and "aaa" goes to item1, if I type
$c.item1
it prints
kkk aaa
Why? I expect item1 to be "kkk" and item2 to be "aaa".
have a look at the the powershell gotchas here :
https://stackoverflow.com/tags/powershell/info
PowerShell separates function arguments with spaces, not commas.
for those who don't read comments #TheMadTechnician gave you the two correct ways of calling function with arguments in PS :
f "kkk" "aaa" or f -a1 "kkk" -a2 "aaa"
Ok, as suggested, making an answer. Kayasax really did give the correct answer, even if he didn't help you fix the problem. The reason that both of those strings are assigned to the first property is because you have called your function incorrectly. You have effectively passed 1 argument to the function, that being an array containing two strings. To properly pass multiple arguments to the function either specify them by name (derived by the variable you assign the parameter to within the function, or any declared aliases), or you can pass them without naming them in the order that they are declared in the function, separated by a space.
In your code you are effectively doing this:
f -a1 ("kkk","aaa") -a2
To correct the problem you can either pass the arguments by position:
f "kkk" "aaa"
Or you can pass the arguments by name (order does not matter when passing by name):
f -a1 "kkk" -a2 "aaa"
Doing either of those will result in your desired output.

Error while using .ForEach in WinDbg

Why am I getting a Invalid parameter poi(adr+4) when I run the following command in WinDbg while debugging a dump file?
.foreach ( adr { !dumpheap -mt 66df13d4 -short } ) { !do poi(adr+4); }
The following shows that the value of adr is getting populated just fine.
.foreach ( adr { !dumpheap -mt 66df13d4 -short } ) { .echo adr; }
I want to get the contents of a .NET string variable that is stored at the 4th offset of a System.Web.Caching.CacheEntry object.
You need to have spaces around adr or use ${adr}. This is documented in MSDN
Note When the string Variable appears within OutCommands, it must be
surrounded by spaces. If it is adjacent to any other text -- even a
parenthesis -- it will not be replaced by the current token value,
unless you use the ${ } (Alias Interpreter) token.