Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call assembly in gdb?

Tags:

assembly

gdb

In gdb I can use call to run functions,but what if I want to run some additional assembly?

like image 566
gdb Avatar asked Mar 30 '11 01:03

gdb


People also ask

Can you use GDB on assembly?

gdb is the GNU source-level debugger that is standard on linux (and many other unix) systems. It can be used both for programs written in high-level languages like C and C++ and for assembly code programs; this document concentrates on the latter.

How do I run a program in GDB?

Use the run command to start your program under GDB. You must first specify the program name (except on VxWorks) with an argument to GDB (see section Getting In and Out of GDB), or by using the file or exec-file command (see section Commands to specify files).

How do I debug an assembler code?

You start debugging by clicking Start Debugging on the Debug menu. On the Start Debugging dialog box, check Enable Assembler debugging, then click OK. If you debug the module again during the same session, you can start it by clicking Start Debugging, Run or Debug.

What is Stepi in GDB?

stepi stepi arg si. Execute one machine instruction, then stop and return to the debugger. It is often useful to do ' display/i $pc ' when stepping by machine instructions. This makes GDB automatically display the next instruction to be executed, each time your program stops.


1 Answers

Prior to GCC 5 (1), I don't know of a way to run arbitrary machine code unless you actually enter the machine code into memory and then run it.

If you want to run code that's already in memory, you can just set the instruction pointer to the start, a breakpoint at the end, then go. Then, after the breakpoint, change the instruction pointer back to its original value.

But I can't actually see the use case for this. That doesn't mean there isn't one, just that anything you can do by running code, you can also achieve by directly modifying the registers, flags, memory and so forth.

For example, the command:

info registers

will dump the current values of the registers while:

set $eax = 42

will change the eax register to 42.

You can also change memory in this way:

set *((char*)0xb7ffeca0) = 4

This writes a single byte to memory location 0xb7ffeca0 and you can also use that same method to store wider data types.


(1) GCC 5 allows you to compile and execute arbitrary code with the compile code command, as documented here.

like image 191
paxdiablo Avatar answered Sep 27 '22 02:09

paxdiablo