debugging ex_bad_access with gdb in c++ - macos

I'm compiling code from the command line with g++ on Mac OSX and have an error when I run my code that results in an EXC_BAD_ACCESS
I've seen that the most helpful way to debug this kind of error is with Zombie objects that don't deallocate when released and then complain when code tries to release them.
However, it seems that NSZombie options are available in Xcode/Objective C.
So my question is there any way to use this functionality/equivalent in programs simply compiled code like
g++ file1.cpp -g -o executable
debugged with
gdb executable
Thanks.

Zombie objects are a concept of Objective-C, not C++, and relate to reference counting issues, which C++ doesn't use (unless you count smart pointers and Enabling Zombies won't help you with that anyway).
So, to answer your question; No, there is no way to use that functionality.
If you want to find the issue then you'll need to use a debugger.

Related

GDB or Radare2?

Should I use GDB or Radare2 for reversing an executable(I am a beginner)?
I try to programming in C and I got a SegFault. I want to Reverse Engineer it to get experience in Assembly and see where I get the SegFault.
For debugging an executable you built from source yourself, GDB is intended as a debugger. You can use layout reg to get a disassembly + registers view which can help understanding segfaults, if looking at C variables didn't help.
Debug info from compiling with gcc -g means you don't need to reverse-engineer anything, just use a normal debugger. But to get experience in asm, using a debugger both ways (source view and asm view) can help you understand how the compiler used certain asm instructions to implement each C statement. So you definitely want a debugger that can take advantage of debug info. There are some GUI GDB front-ends, like https://www.gdbgui.com that can be easier to use than command-line GDB.
But see also How to remove "noise" from GCC/clang assembly output? for more about seeing how C compiles to asm.
I haven't used radare2. I assume it has features that are good for intentionally-obfuscated executables without source, which is the opposite of what you have from compiling your own C programs with a normal compiler.
I would recommend Radare2 because it's clearer than GDB and easier for beginners ;)

Clang Mac OS X Maveric, not supporting fsanitize=undefined

I'm getting a segfault in my C program, and I thought I'd use Clangs help in figuring out why, as Valgrind isn't supported (yet).
I try to compile my program with -fsanitize=undefined to activiate runtime checks for undefined behaviour. All I'm getting is an error message which reads something like: "fsanitize doesn't support option 'undefined'". I've also tried using -fsanitize=address with the same result :/
Anyone know why this is?
It seems like -fsanitize is not supported by apple's CLang.
If you go here http://llvm.org/releases/download.html#3.4 you can get the prebuild binaries of clang, that do recognize this option.
However this is harder because then you have to use a Makefile,
IF by chance you use cmake, then you can just easily go to use makefiles.
OR replace the binaries in the Xcode.app/Contents/Developer/Toolchains/.../bin
but I don't know what can happen if you replace Apples binaries, do a backup before to be able to restore.
Honestly, I'm not myself confortable about replacing apple's binaries, because they probably added something in the clang to make it work with Xcode nicely.
Again, the GPL policy of GCC had this advantage that apple could not modify it.
-> another solution is to develop on linux,
The port to windows of CLang is on its way, but not ready yet, maybe in 3.5.
Best

safeseh gs on g++

I want to compile a c++ file. I'm following a tutorial, in this tutorial, the file is compiled with Visual Studio, and I don't have it. I want to do the same with g++. In the tutorial, use the /GS and /SafeSEH from VS. I want to know how compile my file with those flags with g++ compiler.
Sorry if my question is simple, I never used g++ before. And sorry for my english. Thank you.
So first things first:
/GS actually performs "buffer security checks" which attempt to
automatically detect buffer overflows and terminate your application
before such overflows can be exploited.
/SAFESEH embeds a list of all the structured exception handlers
inside the executable so that the operating system can detect if
unauthorized handlers have been installed in an attempt to hijack
execution.
Now to answer your question: g++ implements a feature similar to /GS. You can compile your code with -fstack-protector-all to enable it. If you are curious you can look at the gcc manpages or use google for more details. g++ doesn't implement structured exception handling (it's a Microsoft extension) and so there's nothing similar to /SAFESEH in g++.
The good news are that unless the example you are working through is designed to demonstrate the extra protection /GS and /SAFESEH afford, compiling the code without them shouldn't be a problem at all. I'd be willing to bet that you can ignore those two options and just compile without them and things will be fine.
If you are interested in getting Visual Studio, you can get the free "Express" versions from Microsoft. Check out the website, here: http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-products
I hope this helps.

how to obtain VC++ compiler-style help on GCC G++ compile/link errors (on linux)

I'm using VC++ as professional developer for more than 10 years and it has been good to me, now I'm trying to broaden my horizons and learn C++ development on Linux.
On Windows things are simple, VC++ does it all (editing, project management, help, debugging), but on linux things are different, you have assemble your development environment from different tools.
I'm still trying to tie things together, and one thing I still haven't figured out is how to decipher GCC (G++) errors when compiling/linking C++ apps on Linux (although I realize GCC is multi-platform, I'll refer to my linux experience here only).
In VC++, things are very clear: If during compilation VC++'s compiler encounters error in program, it will create new entry in 'output' window with the 'compiler error ID'. Example:
c:\projectA\fileB.cpp(38) : error C2228: left of '.cout' must have class/struct/union
From here, you can click on the line in question in 'output' window, press F1, and 'Microsoft Document Browser' app will start (if it wasn't started already), which will load MSDN help file describing compile error connected to the compiler error ID (in example it's C2228), usually with sample you can check out to figure out what's wrong with your code. If you don't have MDB installed, you can always search on the web for C2228 and get the same help page, optionally finding other people's web pages describing their experience with this error.
The same thing is with linking, you'll get 'linker error ID' (e.g. LNK1123), which you can use to find help either locally or on web.
Try as I might, I can't find this kind of functionality in GCC's G++. All I can see is bunch of less experienced GCC developers asking another bunch of more experienced GCC developers to analyze their code based on descriptive compiler/linker errors with no associated error IDs.
Is there tool(set) that provides VC++ compiler-style help on GCC G++ compile/link errors for linux?
You may try to use qtcreator. At least it can show the errors in a more comprehensive way comparable to the VC++, that is, it can locate the error position and highlight the error line and variables.
If you can an alternative might be to use Clang instead. It gives much better error messages than g++. It compiles most code these days (but it still a work in progress). Highly recommended.
Alternatively (as another poster has mentioned) you could use an IDE such as Eclipse to capture the error messages, though I don't think that adds anything beyond taking you to the line number on double-click.

Using GDB without debugging symbols on x86?

How do I use GDB to debug a program which do not have debugging symbols on a 32-bit x86 processor? Inspecting the function arguments, local variables, resolving pointers would be useful to know how to do.
The intention is not really to use this for reverse engineering, as I'm sometimes just too lazy to install the debugging symbols and would be great to know how to get some basic information out of gdb.
To start out, you can do;
gdb "whatever"
break __libc_start_main
r
that will setup a breakpoint in libc's crt0 code and allow you to break before main, even if the target binary is totally stripped.
That will get you to a running state at a breakpoint before most user code. You can then single step, dissasemble, dump memory etc... to your heart's content.
This works on all platforms, the fact your asking about IA-32 / x86 does not matter.
Without debugging symbols, you can only debug at the ASM level. Ok you get a bit more information, but you're not going to get very far unless you understand a bit of ASM and the code the compiler generates. This will let you do a simple inspection of local variables etc if you know what you're doing.
If you have the source, it's going to be far easier just to recompile it.
All you can do is look at registers and the contents of the stack - you'll have to do everything by inferring what things are used for, as Draemon mentions.
Well, the absolutely most important thing is that you be able to unwind the stack. There are three ways this can be ensured:
Build debugging symbols with -g
On systems that do C++ exception unwinding via tables (probably anything ELF these days?), the -funwind-tables flag will tell it to generate such tables regardless of language, and GDB can use these tables (at least, with x86 linux it can).
Or, failing those, at least make sure that -fomit-frame-pointer isn't enabled

Resources