How to call assembly in gdb?

前端 未结 2 1476
再見小時候
再見小時候 2020-12-02 00:16

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

2条回答
  •  时光取名叫无心
    2020-12-02 00:50

    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.

提交回复
热议问题