[patch] Avoid crash when connecting to gdbserver

Hi All,
I noticed a crash when I connect to gdbserver without first loading a file. This is on x86_64 with Ubuntu 12.04.

./lldb
(lldb) gdb-remote 10000
Segmentation fault (core dumped)

The attached patch adds a check to avoid the crash. Does it look ok?

Regards,
Abid

crash.patch (661 Bytes)

I am trying to build lldb on a Ubuntu 12.04 x86_64 box. I have a self-hosted clang 3.3 toolchain successfully built and installed on this box. So, I thought it would be simple just add a symlink lldb to the lldb-3.3-src into the llvm/tools/, redo the configure, make, and make install would do it.

Not really!

zackp@nb1:~/Downloads/clang3.3/build$ …/llvm/configure --prefix=/usr --enable-optimized --disable-assertions --enable-targets=host-only --enable-cxx11=yes --enable-libcpp=yes --with-extra-ld-options=“-lc++abi”
zackp@nb1:~/Downloads/clang3.3/build$ make -j 2
[…]
llvm[4]: Compiling Mangled.cpp for Release build
/home/zackp/Downloads/clang3.3/llvm-3.3.src/tools/lldb/source/Core/Mangled.cpp:218:21: error: use of undeclared identifier ‘free’
free (demangled_name);
^
1 error generated.
make[4]: *** [/home/zackp/Downloads/clang3.3/build/tools/lldb/source/Core/Release/Mangled.o] Error 1
make[4]: *** Waiting for unfinished jobs…
make[4]: Leaving directory /home/zackp/Downloads/clang3.3/build/tools/lldb/source/Core' make[3]: *** [all] Error 1 make[3]: Leaving directory /home/zackp/Downloads/clang3.3/build/tools/lldb/source’
make[2]: *** [all] Error 1
make[2]: Leaving directory /home/zackp/Downloads/clang3.3/build/tools/lldb' make[1]: *** [all] Error 1 make[1]: Leaving directory /home/zackp/Downloads/clang3.3/build/tools’
make: *** [all] Error 1

Questions:
0. why in a C++ code, the C free function is used?

  1. Why when using it, the #include is nowhere in the source?

Regards,

– Zack

0. why in a C++ code, the C free function is used?

Because it's calling into a C API (cxxabi). demangled_name comes from a
call to abi::__cxa_demangle(), which presumably calls malloc to allocate
its return value.

1. Why when using it, the #include <cstdlib> is nowhere in the source?

Mangled.cpp:25 is:
#include <stdlib.h>

Hi Stefanus,

Thanks for your response?

  1. why in a C++ code, the C free function is used?

Because it’s calling into a C API (cxxabi). demangled_name comes from a
call to abi::__cxa_demangle(), which presumably calls malloc to allocate
its return value.

OK. Got that.

  1. Why when using it, the #include is nowhere in the source?

Mangled.cpp:25 is:
#include <stdlib.h>

No. This is incorrect. With lldb 3.3 official release tar ball, the #include that you mentioned is not there.

zackp@nb1:~/Downloads/clang3.3/lldb-3.3.src/source/Core$ grep stdlib.h Mangled.cpp
zackp@nb1:~/Downloads/clang3.3/lldb-3.3.src/source/Core$

I patched it as follows:

11 #if defined(APPLE)
12 #define USE_BUILTIN_LIBCXXABI_DEMANGLER 1
13 #endif
14
15 #if defined(USE_BUILTIN_LIBCXXABI_DEMANGLER)
16 #include “lldb/Core/cxa_demangle.h”
17 #else
18 // FreeBSD9-STABLE requires this to know about size_t in cxxabi.h
19 #include
20 #include <cxxabi.h>
21 #endif
22
23 #include
24 #include “llvm/ADT/DenseMap.h”

My personal style is that if I code in C++, I use as much as possible C++ coding convention.

Regards,

– Zack
[…]

Looks good. Commit when you can.

Greg,
Thanks for the review. I don't have commit access so please commit on my behalf.

Thanks,
Abid

Thanks for the patch - I committed it for you.

-Ed