print contents of std::vector<std::string> in lldb - c++

I want to print all the strings in a vector. When I do
(lldb) p myVector
(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >) $30 = size=65 {
[0] = Summary Unavailable
[1] = Summary Unavailable
[2] = Summary Unavailable
[3] = Summary Unavailable
[4] = Summary Unavailable
[5] = Summary Unavailable
[6] = Summary Unavailable
[7] = Summary Unavailable
[8] = Summary Unavailable
[9] = Summary Unavailable
[10] = Summary Unavailable
[11] = Summary Unavailable
...
However, printing individual elements work e.g.,
p myVector[5].c_str()
(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >::value_type *) $31 = 0x0000600000210280 "-D__STDC_CONSTANT_MACROS"
I'd like to print all elements at once as there are quite a few.

Add -fstandalone-debug to the compiler command line.
I have this set on my cmake for the debug build:
set(CMAKE_CXX_FLAGS_DEBUG_INIT "-ggdb3 -O0 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fstandalone-debug ${SANITIZE_OPTS}" CACHE STRING "Initial debug flags")

So i was able to solve this by using lldb's expr evaluation
(lldb) expr -- for (int x = 0; x < myVector.size(); x++) printf("arg[%d] = %s\n", x, myVector[x].c_str())
# prints:
arg[0] = /Library/Developer/CommandLineTools/usr/bin/c++
arg[1] = --driver-mode=g++
arg[2] = -DGTEST_HAS_RTTI=0
arg[3] = -D__STDC_CONSTANT_MACROS
arg[4] = -D__STDC_FORMAT_MACROS
arg[5] = -D__STDC_LIMIT_MACROS
...

Related

Terminal use gdb to debug .cpp files, (gdb)p command

When I use gdb to debug, I just want to use '(gdb)p x' to print the value of x, but the result is very complicated. Terminal command like this:
(gdb) p word
Output:
$2 = {<std::__1::__basic_string_common<true>> = {<No data fields>}, static __short_mask = 1,
static __long_mask = 1,
__r_ = {<std::__1::__compressed_pair_elem<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__rep, 0, false>> = {__value_ = {{__l = {__cap_ = 1970239750,
__size_ = 0, __data_ = 0x0}, __s = {{__size_ = 6 '\006', __lx = 6 '\006'},
__data_ = "you", '\000' <repeats 19 times>}, __r = {__words = {1970239750, 0,
0}}}}}, <std::__1::__compressed_pair_elem<std::__1::allocator<char>, 1, true>> = {<std::__1::allocator<char>> = {<No data fields>}, <No data fields>}, <No data fields>},
static npos = 18446744073709551615}
What I really want is __data_ = "you" this segment only.
How can I get a simple result like this:
$2 = "you"
Can someone help me? Very appreciate that.
How can I get a simple result like this:
That's what GDB pretty-printers are for.
It looks like you are using libc++ (and probably Clang).
From libc++ documentation:
GDB does not support pretty-printing of libc++ symbols by default. However, libc++ does provide pretty-printers itself. Those can be used as:
$ gdb -ex "source <libcxx>/utils/gdb/libcxx/printers.py" \
-ex "python register_libcxx_printer_loader()" \
<args>
You can put the source <libcxx>/utils/gdb/libcxx/printers.py and the python register_libcxx_printer_loader() commands into your ~/.gdbinit, and then things will just work™.

Export OpenGL kernel code with Apache TVM in Colab (X11 related error)

I have problem with exporting OpenGL kernel code with Apache TVM - Open deep learning compiler stack for CPUs, GPUs, and specialized accelerators in Google Colaboratory.
It can reproduce on Build TVM.ipynb.
To enable Xvrf, I installed PyVirtualDisplay.
%%bash
sudo apt-get update
sudo apt-get install -y -qq llvm-6.0 libglfw3-dev libtinfo-dev libffi-dev zlib1g-dev xvfb x11-utils
pip install pyvirtualdisplay
git clone --recursive https://github.com/dmlc/tvm
cd tvm
mkdir -p build
cp cmake/config.cmake build
sed -i -e 's/USE_OPENGL OFF/USE_OPENGL ON/g' build/config.cmake
sed -i -e 's/USE_CUDA OFF/USE_CUDA ON/g' build/config.cmake
sed -i -e 's/USE_CUDNN OFF/USE_CUDNN ON/g' build/config.cmake
sed -i -e 's/USE_LLVM OFF/USE_LLVM ON/g' build/config.cmake
cd build
cmake ..
make -j4
cd ..
cd python; python setup.py install; cd ..
cd topi/python; python setup.py install; cd ../..
cd nnvm/python; python setup.py install; cd ../..
# Start virtual display
from pyvirtualdisplay import Display
display = Display(visible=0, size=(1024, 768))
display.start()
import os
os.environ["DISPLAY"] = ":" + str(display.display) + "." + str(display.screen)
import tvm
import numpy as np
nn = 1024
n = tvm.var('n')
n = tvm.convert(nn)
m = n
l = n
A = tvm.placeholder((n, l), name='A', dtype='int32')
B = tvm.placeholder((m, l), name='B', dtype='int32')
k = tvm.reduce_axis((0, l), name='k')
C = tvm.compute((n, m), lambda ii, jj: tvm.sum(A[ii, k] * B[jj, k], axis=k),
name='CC')
s = tvm.create_schedule(C.op)
s[C].opengl()
print(tvm.lower(s, [A, B, C], simple_mode=True))
f = tvm.build(s, [A, B, C], "opengl", name="gemm")
print("------opengl code------")
print(f.imported_modules[0].get_source(fmt="gl"))
ctx = tvm.opengl()
n, m, l = nn, nn, nn
a_np = np.random.uniform(low=0, high=10, size=(n, l)).astype(A.dtype)
b_np = np.random.uniform(low=0, high=10, size=(m, l)).astype(B.dtype)
a = tvm.nd.array(a_np, ctx)
b = tvm.nd.array(b_np, ctx)
c = tvm.nd.array(np.zeros((n, m), dtype=C.dtype), ctx)
f(a, b, c)
tvm.testing.assert_allclose(c.asnumpy(), np.dot(a_np, b_np.T))
I expect the OpenGL code with f.imported_modules[0].get_source(fmt="gl") but it failed due to the following X11 related error.
// attr [compute(CC, 0x4768580)] opengl_stage_scope = ""
produce CC {
// attr [iter_var(threadIdx.x, , threadIdx.x)] thread_extent = 1048576
glsl_texture_store(CC, 0)
for (k, 0, 1024) {
glsl_texture_store(CC, (CC[threadIdx.x] + (A[(((threadIdx.x/1024)*1024) + k)]*B[(((threadIdx.x % 1024)*1024) + k)])))
}
}
---------------------------------------------------------------------------
TVMError Traceback (most recent call last)
<ipython-input-2-3a202d10b787> in <module>()
23 print(tvm.lower(s, [A, B, C], simple_mode=True))
24
---> 25 f = tvm.build(s, [A, B, C], "opengl", name="gemm")
26 print("------opengl code------")
27 print(f.imported_modules[0].get_source(fmt="gl"))
6 frames
/usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/_ffi/_cython/base.pxi in tvm._ffi._cy3.core.CALL()
TVMError: Traceback (most recent call last):
[bt] (8) /usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(tvm::codegen::BuildOpenGL(tvm::Array<tvm::LoweredFunc, void>)+0x1c3) [0x7f3742ec4573]
[bt] (7) /usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(tvm::runtime::OpenGLModuleCreate(std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, tvm::runtime::OpenGLShader, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, tvm::runtime::OpenGLShader> > >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, tvm::runtime::FunctionInfo, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, tvm::runtime::FunctionInfo> > >)+0xeb) [0x7f374364b7fb]
[bt] (6) /usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(tvm::runtime::OpenGLModuleNode::OpenGLModuleNode(std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, tvm::runtime::OpenGLShader, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, tvm::runtime::OpenGLShader> > >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, tvm::runtime::FunctionInfo, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, tvm::runtime::FunctionInfo> > >)+0x92) [0x7f374364b1d2]
[bt] (5) /usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(tvm::runtime::gl::OpenGLWorkspace::Global()+0x55) [0x7f37436438d5]
[bt] (4) /usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(tvm::runtime::gl::OpenGLWorkspace::OpenGLWorkspace()+0x4d) [0x7f3743640b7d]
[bt] (3) /usr/lib/x86_64-linux-gnu/libglfw.so.3(glfwInit+0x2d) [0x7f3725a7ab3d]
[bt] (2) /usr/lib/x86_64-linux-gnu/libglfw.so.3(+0xb858) [0x7f3725a80858]
[bt] (1) /usr/lib/x86_64-linux-gnu/libglfw.so.3(+0x59f7) [0x7f3725a7a9f7]
[bt] (0) /usr/local/lib/python3.6/dist-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(tvm::runtime::gl::GlfwErrorCallback(int, char const*)+0x2f9) [0x7f374363b7c9]
File "/content/tvm/src/runtime/opengl/opengl_device_api.cc", line 82
Error: [65544] X11: RandR gamma ramp support seems broken
I solved this issue partially using FCRC 2019 TVM binaries.

using C++ code in R

I'm experimenting the Rcpp & inline packages to speed up my computation..
I want to know how to make C++ CODE (not a c++ function) work
with these packages?
Here is an example I tried which consists of building a dynamic array called 'tableau' and display the result. I both tried the 'cppFunction' and 'cxxfunction' but no success...
Can someone gives me a hint?
require(inline);require(Rcpp)
src='vector < vector < int > > tableau (
{
{1,2,3,42},
{0,2,3},
{11,12}
}
);
return tableau;
'
cppFunction(src)
Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput, :
Error 1 occurred building shared library.
In addition: Warning message:
No function found for Rcpp::export attribute at file7bc1b0f5993.cpp:5
R has no idea what to do with a <vector <vector <int>>.
To return a list, you have to use the type List and NumericVector:
src = 'List tableau() {
NumericVector v1 = NumericVector::create(1,2,3,42);
NumericVector v2 = NumericVector::create(0,2,3);
NumericVector v3 = NumericVector::create(11,12);
return List::create(v1, v2, v3);
}'
createTableau <- cppFunction(src)
createTableau()
## [[1]]
## [1] 1 2 3 42
##
## [[2]]
## [1] 0 2 3
##
## [[3]]
## [1] 11 12
You should really read at least some of the documentation. Here's a good place to start: Rcpp Tutorial

How can clang avoid to analyze the builtin type/function in C/C++?

/usr/lib/gcc/x86_64-redhat-linux/4.8.2/include/stdarg.h:40:9: error: unknown type name '__builtin_va_list'; did you mean '__builtin_va_list'?
typedef __builtin_va_list __gnuc_va_list;
^
note: '__builtin_va_list' declared here
When I try to use the RecursiveASTVisitor to visit the AST, I got the above problem. And at the end, the program abort with the following message.
type: Typedef of __gnuc_va_list size CItutorial5: /home/lotay/libs/clang/llvm/include/llvm/Support/Casting.h:239: typename cast_retty<X, Y *>::ret_type llvm::cast(Y *) [X = clang::ConstantArrayType, Y = const clang::Type]: Assertion `isa<X>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
Aborted (core dumped)
Really thanks for help
and I have the langoptions with following confs.
langOpts.GNUMode = 1 ;
langOpts.CXXExceptions = 1 ;
langOpts.RTTI = 1 ;
langOpts.Bool = 1 ;
langOpts.CPlusPlus = 1 ;
langOpts.WChar = 1 ;

gdb: Cast memory address to an STL object

I have a memory address that i know is an STL object. Say the address is 0x603340, and I know there is a map there
How do I display the contents of this memory as said object from gdb?
I tried this:
p ('std::map<std::string, std::string*, std::less<std::string>, std::allocator<std::pair<std::string const, std::string*> > >'*) 0x603340
which gets me:
No symbol "std::map<std::string, std::string*, std::less<std::string>, std::allocator<std::pair<std::string const, std::string*> > >" in current context.
Any idea what am I doing wrong? Thanks.
I have taken this as a first step: simple stl containers inspectors for gdb. I did not manage to use pmap directly but I made use of it and create my own scrupt my_pmap:
>cat my.txt
define my_pmap
set $my_obj = *((std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::string*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::string*> > >*) $arg0)
print $my_obj
set $i = 0
set $node = $my_obj._M_t._M_impl._M_header._M_left
set $end = $my_obj._M_t._M_impl._M_header
set $tree_size = $my_obj._M_t._M_impl._M_node_count
while $i < $tree_size
set $value = (void *)($node + 1)
printf "elem[%u].left: ", $i
p *( std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)$value
set $value = $value + sizeof(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)
printf "elem[%u].right: ", $i
p **((std::basic_string<char, std::char_traits<char>, std::allocator<char> >**)$value)
if $node._M_right != 0
set $node = $node._M_right
while $node._M_left != 0
set $node = $node._M_left
end
else
set $tmp_node = $node._M_parent
while $node == $tmp_node._M_right
set $node = $tmp_node
set $tmp_node = $tmp_node._M_parent
end
if $node._M_right != $tmp_node
set $node = $tmp_node
end
end
set $i=$i+1
end
printf "Map size = %u\n", $tree_size
end
Then I wrote a short C++ test program:
#include <map>
#include <string>
#include <iostream>
int main()
{
std::string local_s1 = "Local1";
std::string local_s2= "Local2";
typedef std::map<std::string, std::string*> my_type_t;
my_type_t my_map;
my_map.insert(std::make_pair("Key-string-1", &local_s1));
my_map.insert(std::make_pair("Key-string-2", &local_s2));
my_map.insert(std::make_pair("Key-string-3", &local_s2));
return 0;
}
And then I tested my script:
>gdb -q ./a.out
Reading symbols from /import/home/sergey.kurenkov/src/linux.x64.6.0/tests/test.gdb_map/a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x400c7c: file main.cpp, line 7.
Starting program: /import/home/sergey.kurenkov/src/linux.x64.6.0/tests/test.gdb_map/a.out
Temporary breakpoint 1, main () at main.cpp:7
7 std::string local_s1 = "Local1";
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6.x86_64
(gdb) source my.txt
(gdb) n
8 std::string local_s2= "Local2";
(gdb)
11 my_type_t my_map;
(gdb)
12 my_map.insert(std::make_pair("Key-string-1", &local_s1));
(gdb)
13 my_map.insert(std::make_pair("Key-string-2", &local_s2));
(gdb)
14 my_map.insert(std::make_pair("Key-string-3", &local_s2));
(gdb)
16 return 0;
(gdb) p &my_map
$1 = (my_type_t *) 0x7fffffffe040
(gdb) my_pmap 0x7fffffffe040
elem[0].left: $2 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x603088 "Key-string-1"}}
elem[0].right: $3 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x603028 "Local1"}}
elem[1].left: $4 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x6030f8 "Key-string-2"}}
elem[1].right: $5 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x603058 "Local2"}}
elem[2].left: $6 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x603168 "Key-string-3"}}
elem[2].right: $7 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x603058 "Local2"}}
Map size = 3
(gdb)