简体   繁体   中英

is there a way change a member variable in release code on linux while the program is running?

I wrote a program which solves some kind of optimization problem. I have been running it with increasingly lower thresholds and I think I hit some kind of barrier since my program can't get better results. It has been running for about 5 days...

However, I have some code in the program which saves its data if it is better than the threshold. Since I have no interest in running it for 5 days again I want to do it while the program is running, but sadly I didn't compile it with -g (I am using gcc). What I am trying to do is change the threshold value to a higher value.

I did manage to connect to it using gdb, and see a stack frame. I am now inside a method, and I want to get access to the "this" pointer in order to change the threshold parameter. I tried examining values around the function's address but nothing makes sense... How do I find out where "this" points to?

Thanks.

First, you don't need to restart the program in order to debug it.

You should rebuild it using exactly the same sources and flags you used originally and add -g . This will give you an executable that contains debug info, but otherwise is (almost) identical to the original binary. The output from nm a.out and nm a.out.debug should be very close (some minor differences are likely to be present, but will likely not affect debugging). Now run gdb a.out.debug <pid> and you should be able to do source-level debugging, and adjust your threshold.

If this fails, you can still do what you want, but it will be harder: you'll have to do that at the assembly level. You can run the a.out.debug (start a new task), and see in GDB disas output how parameters are passed to your routine, and how the threshold is accessed. You can then go back to your original executable, and observe essentially the same things happening there. Once you know where in memory the threshold is located, you'll be able to adjust it in GDB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM