Home > Net >  can I edit lines of code using gdb and is it also possible to save to actual source file and header
can I edit lines of code using gdb and is it also possible to save to actual source file and header

Time:10-31

I have this program called parser I compiled with -g flag this is my makefile

parser: header.h parser.c
    gcc -g header.h parser.c -o parser

clean:
    rm -f parser a.out

code for one function in parser.c is

int _find(char *html , struct html_tag **obj)
{
  char temp[strlen("<end") 1];
  memcpy(temp,"<end",strlen("<end") 1);
  ...
  ...
  .
  return 0;

}

What I like to see when I debug the parser or something can I also have the capability to change the lines of code after hitting breakpoint and while n through the code of above function. If its not the job of gdb then is there any opensource solution to actually changing code and possible saving so when I run through the next statement in code then changed statement before doing n (possible different index of array) will execute, is there any opensource tool or can it be done in gdb do I need to do some compiling options.

I know I can assign values to variables at runtime in gdb but is this it? like is there any thing like actually also being capable of changing soure

CodePudding user response:

Most C implementations are compiled. The source code is analyzed and translated to processor instructions. This translation would be difficult to do on a piecewise basis. That is, given some small change in the source code, it would be practically impossible to update the executable file to represent those changes. As part of the translation, the compiler transforms and intertwines statements, assigns processor registers to be used for computing parts of expressions, designates places in memory to hold data, and more. When source code is changed slightly, this may result in a new compilation happening to use a different register in one place or needing more or less memory in a particular function, which results in data moving back or forth. Merging these changes into the running program would require figuring out all the differences, moving things in memory, rearranging what is in what processor register, and so on. For practical purposes, these changes are impossible.

GDB does not support this.

(Apple’s developer tools may have some feature like this. I saw it demonstrated for the Swift programming language but have not used it.)

  • Related