Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I reconstruct C++ source code from debug binaries?

I have a C++ application compiled in debug (using MinGW and Qt) but I've lost some major changes because someone in my team forgot to commit his changes in the source control manager and overwrote the source code with other changes.

When I run the program in debug (in Qt Creator) I can set a break point in main and then see the source code.

Is there a way to reconstruct all the source file lost using only the debug binaries? Either manually or automatically.

Thanks!

like image 974
Etienne Savard Avatar asked Jun 18 '10 17:06

Etienne Savard


2 Answers

When I run the program in debug (in Qt Creator) I can set a break point in main and then see the source code.

Really? Find out where your debugger is getting the source code from, and copy it from there.

It's more likely that your debugger is just grabbing a file on your system with the same name/path as the original filename (perhaps a more recent version, or an old version, etc) and things just happen to line up.

You can not truly regenerate the original source form a compiled binary, because the transformation from C++ source to a compiled binary is not a 1 to 1 relationship. There are many (infinitely...) different source files which will compile to the same binary. There is no way to know from looking at a binary what the original source looked like.

There are tools which can generate something which resembles a C++ source file, but more than likely it'll look nothing like your original source.

like image 188
Terry Mahaffey Avatar answered Oct 05 '22 22:10

Terry Mahaffey


I think there is a small chance you can actually recover source, but not from the binary itself: If you are really, really desperate, you can use a search application (e.g. Agent Ransack) and search your entire drive for string patterns that you know exist in the source code. In particular, search in your pagefile (pagefile.sys) if you have one - there might be some chance it's buried there somewhere.

I have tried this method once when I was really really desperate, but my situation then was a bit different and more favourable for "search and recover" because I lost it when somehow the IDE crash and the entire source file is lost (!!!! nasty surprise!!!)

like image 44
joejoe Avatar answered Oct 06 '22 00:10

joejoe