How to edit user-defined command in GDB - gdb

For example, I create a command named print_info
(gdb) define print_info
> i r
> end
now I want to edit this command to
i r $eax
I know I can just run define print_info and redefine it. But I want to know if I can just edit it base on the existed command. So that when the command is complex, I don't have to do a complete rewrite.

I want to know if I can just edit it base on the existed command.
No.
So that when the command is complex, I don't have to do a complete rewrite.
Save the commands into a file, and use source filename to re-define your print_info command after editing that file.

Related

CakePHP 3.7 Shell commands inside a plugin couldn't execute

namespace Admin\Shell;
use Cake\Console\Shell;
class AdminAlertShell extends Shell{
...
...
}
Here 'Admin' is plugin, So I created this file inside the plugins folder structure.
File path : /plugins/Admin/src/Shell/AdminAlertShell.php
Tried to run this in CLI
bin/cake admin_alert
But an exception throws
Exception: Unknown command cake admin_alert. Run cake --help to get the list of valid commands. in [localpath/vendor/cakephp/cakephp/src/Console/CommandRunner.php, line 346]
It was working. But I don't know what happened to this. I had upgraded cakephp 3.5 to 3.7. But, I am not sure this caused the issue.
I just tracked down the source of the issue in my project.
Inside my plugin there was a file: src/Plugin.php
Inside this class there was the following lines of code:
/**
* #inheritDoc
*/
public function console(CommandCollection $commands): CommandCollection
{
// Add console commands here.
return $commands;
}
This was probably generated via bake.
I saw that the parent was not called. In the path is added in the parent.
Change this method to look like this:
/**
* #inheritDoc
*/
public function console(CommandCollection $commands): CommandCollection
{
// Add console commands here.
$commands = parent::console($commands);
return $commands;
}
Now the parent is called and the path is added to the command collection.
As a side note I also see the middleware is not calling its parent.
Think it would be a good idea to fix that one aswell.
As an alternative you can just clear out the class and all defaults should be used.
Hope this saves someone the hours it cost me to figure this one out.

Is it possible to remove gdb aliases without restarting gdb?

Suppose I define a new gdb command which includes an alias.
import gdb
import string
class PrettyPrintString (gdb.Command):
"Command to print strings with a mix of ascii and hex."
def __init__(self):
super (PrettyPrintString, self).__init__("ascii-print",
gdb.COMMAND_DATA,
gdb.COMPLETE_EXPRESSION, True)
gdb.execute("alias -a pp = ascii-print", True)
Now, I'd like to make a small change to the script and source it again in the same gdb session. Unfortunately, when I try to source again, I get the following error.
gdb.error: Alias already exists: pp
How can I delete the original alias and source the updated script?
Note that the alias documentation does not appear to say anything about deleting aliases, and I tried unalias and delete but neither had the desired effect.
You can define a keyword to a function instead of as an alias. For example I have
define w
where
end
in my .gdbinit. And re-defining works, as opposed to re-aliasing.

lldb: conditional breakpoint on a most derived type

typical debugging pattern:
class Button : public MyBaseViewClass
{
...
};
....
void MyBaseViewClass::Resized()
{
//<---- here I want to stop in case MyBaseViewClass is really a Button, but not a ScrollBar, Checkbox or something else. I.e. I want a breakpoint condition on a dynamic (most derived) type
}
trivial approaches like a breakpoint on strstr(typeid(*this).name(), "Button") don't work because on typeid lldb console tells:
(lldb) p typeid(*this)
error: you need to include <typeinfo> before using the 'typeid' operator
error: 1 errors parsing expression
surely #include in console before making the call doesn't help
You can do this in Python pretty easily. Set the breakpoint - say it is breakpoint 1 - then do:
(lldb) break command add -s python 1
Enter your Python command(s). Type 'DONE' to end.
def function (frame, bp_loc, internal_dict):
"""frame: the lldb.SBFrame for the location at which you stopped
bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information
internal_dict: an LLDB support object not to be used"""
this_value = frame.FindVariable("this", lldb.eDynamicDontRunTarget)
this_type = this_value.GetType().GetPointeeType().GetName()
if this_type == "YourClassNameHere":
return True
return False
DONE
The only tricky bit here is that when calling FindVariable I passed lldb.eDynamicDontRunTarget which told lldb to fetch the "dynamic" type of the variable, as opposed to the static type. As an aside, I could have also used lldb.eDynamicRunTarget but I happen to know lldb doesn't have to run the target go get C++ dynamic types.
This way of solving the problem is nice in that you don't have to have used RTTI for it to work (though then we'll only be able to get the type of classes that have some virtual method - since we use the vtable to do this magic.) It will also be faster than a method that requires running code in the debugee as your expression would have to do.
BTW, if you like this trick, you can also put the breakpoint code into a python function in some python file (just copy the def above), then use:
(lldb) command script import my_functions.py
(lldb) breakpoint command add -F my_functions.function
so you don't have to keep retyping it.

is there any tools which give the list of function usage/call/ details in an project(bulk code)

Thanks in advance
C++: LINUX machine
Please May i know ,is there any tools which give the list of function usage/call/ details in an project(bulk code).
EX: int fun(int var){return var;}
some where in the code the function is called like this :
fun(10);
fun(21);
cscope does exactly what you want. Run it in your project like this:
cscope -R
Then in the curses interface, type in the function name to search for. It will show a list of matching calls and the arguments passed in.
You can jump to each one pressing the letter in the menu.
Finally, press Ctrl+d to exit the program.

How to Call command line from TestCase in Laravel 5

I am developing an application in Laravel 5, I have a test file which extends from TestCase.php, I need to call the phpcs command in my file
class MyTest extends TestCase {
public function testFunction()
{
//here I need to call the phpcs command
}
}
In te examples here http://laravel.com/docs/5.0/testing I just found the this->call function, which I don't think is the proper choice for me, since it is returning a response object, what is the right way to do it? which Class and functions should I use to to run a command-line inside this class I also need to have the result of the command in a variable
I don't Laravel has something built in for calling the command line. However it doesn't really need to because you can simply use the exec() function. Basically something like this:
public function testFunction()
{
exec('phpcs', $output);
echo $output[0]; // output line 1
}
As second argument you can pass a variable that will contain every line of the output as an array. The exec() function itself returns the last line from the output as string. (Especially useful when running single line commands like php -v)