LLDB C++ debugging - c++

I am new to LLDB and I am working with various std::vectors in my code, however when I try to print the values of a vector or to query the size of my vector with something like expr '(int)myVector[0]' or expr '(int)myVector.size()' the debugger prints values that have nothing to do with the values I know there are in the vector.
As I'm learning to debug with command line and LLDB, I'm sure I'm missing something here, can anyone spot my error or give some advise?
EDIT Forgot to say that I'm under OS X Mavericks with the latest command-line tools installed.

I found the answer myself. Apparently the overloaded operators like [] are not allowed since they are inlined, see this question for a better explanation on that.
Moreover, I don't know why did I put single quotes for the statement I wanted to evaluate (I'm pretty sure I saw it in other place ... what do they actually mean in LLDB?) like so expr 'printf("Hey")'
So, taking out the quotes and using the answer in the cited question it suffices with something like
expr (int) myVector.__begin_[0]
to get the single value of a position in the vector.

Use p myVector or po myVector. These will print out the contents of your vector (alongside the size) in a couple of different formats.
To print a single value from the vector, you can use something like p (int)myVector[0].

Related

Maxima: creating a function that acts on parts of a string

Context: I'm using Maxima on a platform that also uses KaTeX. For various reasons related to content management, this means that we are regularly using Maxima functions to generate the necessary KaTeX commands.
I'm currently trying to develop a group of functions that will facilitate generating different sets of strings corresponding to KaTeX commands for various symbols related to vectors.
Problem
I have written the following function makeKatexVector(x), which takes a string, list or list-of-lists and returns the same type of object, with each string wrapped in \vec{} (i.e. makeKatexVector(string) returns \vec{string} and makeKatexVector(["a","b"]) returns ["\vec{a}", "\vec{b}"] etc).
/* Flexible Make KaTeX Vector Version of List Items */
makeKatexVector(x):= block([ placeHolderList : x ],
if stringp(x) /* Special Handling if x is Just a String */
then placeHolderList : concat("\vec{", x, "}")
else if listp(x[1]) /* check to see if it is a list of lists */
then for j:1 thru length(x)
do placeHolderList[j] : makelist(concat("\vec{", k ,"}"), k, x[j] )
else if listp(x) /* check to see if it is just a list */
then placeHolderList : makelist(concat("\vec{", k, "}"), k, x)
else placeHolderList : "makeKatexVector error: not a list-of-lists, a list or a string",
return(placeHolderList));
Although I have my doubts about the efficiency or elegance of the above code, it seems to return the desired expressions; however, I would like to modify this function so that it can distinguish between single- and multi-character strings.
In particular, I'd like multi-character strings like x_1 to be returned as \vec{x}_1 and not \vec{x_1}.
In fact, I'd simply like to modify the above code so that \vec{} is wrapped around the first character of the string, regardless of how many characters there may be.
My Attempt
I was ready to tackle this with brute force (e.g. transcribing each character of a string into a list and then reassembling); however, the real programmer on the project suggested I look into "Regular Expressions". After exploring that endless rabbit hole, I found the command regex_subst; however, I can't find any Maxima documentation for it, and am struggling to reproduce the examples in the related documentation here.
Once I can work out the appropriate regex to use, I intend to implement this in the above code using an if statement, such as:
if slength(x) >1
then {regex command}
else {regular treatment}
If anyone knows of helpful resources on any of these fronts, I'd greatly appreciate any pointers at all.
Looks like you got the regex approach working, that's great. My advice about handling subscripted expressions in TeX, however, is to avoid working with names which contain underscores in Maxima, and instead work with Maxima expressions with indices, e.g. foo[k] instead of foo_k. While writing foo_k is a minor convenience in Maxima, you'll run into problems pretty quickly, and in order to straighten it out you might end up piling one complication on another.
E.g. Maxima doesn't know there's any relation between foo, foo_1, and foo_k -- those have no more in common than foo, abc, and xyz. What if there are 2 indices? foo_j_k will become something like foo_{j_k} by the preceding approach -- what if you want foo_{j, k} instead? (Incidentally the two are foo[j[k]] and foo[j, k] when represented by subscripts.) Another problematic expression is something like foo_bar_baz. Does that mean foo_bar[baz], foo[bar_baz] or foo_bar_baz?
The code for tex(x_y) yielding x_y in TeX is pretty old, so it's unlikely to go away, but over the years I've come to increasing feel like it should be avoided. However, the last time it came up and I proposed disabling that, there were enough people who supported it that we ended up keeping it.
Something that might be helpful, there is a function texput which allows you to specify how a symbol should appear in TeX output. For example:
(%i1) texput (v, "\\vec{v}");
(%o1) "\vec{v}"
(%i2) tex ([v, v[1], v[k], v[j[k]], v[j, k]]);
$$\left[ \vec{v} , \vec{v}_{1} , \vec{v}_{k} , \vec{v}_{j_{k}} ,
\vec{v}_{j,k} \right] $$
(%o2) false
texput can modify various aspects of TeX output; you can take a look at the documentation (see ? texput).
While I didn't expect that I'd work this out on my own, after several hours, I made some progress, so figured I'd share here, in case anyone else may benefit from the time I put in.
to load the regex in wxMaxima, at least on the MacOS version, simply type load("sregex");. I didn't have this loaded, and was trying to work through our custom platform, which cost me several hours.
take note that many of the arguments in the linked documentation by Dorai Sitaram occur in the reverse, or a different order than they do in their corresponding Maxima versions.
not all the "pregexp" functions exist in Maxima;
In addition to this, escaping special characters varied in important ways between wxMaxima, the inline Maxima compiler (running within Ace editor) and the actual rendered version on our platform; in particular, the inline compiler often returned false for expressions that compiled properly in wxMaxima and on the platform. Because I didn't have sregex loaded on wxMaxima from the beginning, I lost a lot of time to this.
Finally, the regex expression that achieved the desired substitution, in my case, was:
regex_subst("\vec{\\1}", "([[:alpha:]])", "v_1");
which returns vec{v}_1 in wxMaxima (N.B. none of my attempts to get wxMaxima to return \vec{v}_1 were successful; escaping the backslash just does not seem to work; fortunately, the usual escaped version \\vec{\\1} does return the desired form).
I have yet to adjust the code for the rest of the function, but I doubt that will be of use to anyone else, and wanted to be sure to post an update here, before anyone else took time to assist me.
Always interested in better methods / practices or any other pointers / feedback.

Debugger problem regarding to how to see all the value in the dynamic array in c++

I need to understand the other people's code by debugging through all the code, in the meantime, I can't change or add any code. The one major problem comes up. That is When I want to see the all the value in a dynamic array by adding it to Watch, it would not give me what I expect to. I am only able to see first value of the them. Such as:
In addition, when I add it to Watch, it would show no operator "[]" matches these operands.
How can i address this problem?
you can follow this link, it shows you how to use feature called the “size specifier”to display the array elements:
https://blogs.msdn.microsoft.com/habibh/2009/06/05/size-specifier-how-to-display-a-cc-pointer-as-an-array-in-the-visual-studio-debugger/

Debugging with gdb - (gdb) "x/s pointer" output

I am trying to debug using gdb. I got that if you want output in string you have to use "x/s Ptr". It works fine some time. But many times I am getting either Null value i.e. " " or some random numeric values. My file has 10000 lines of codes. :-p Please find some gdb output. For e.g.
krb5_get_credentials_for_user (context=0x59c00eb0, options=4, ccache=0x5a001d40, in_creds=0x5ab022a8, subject_cert=0x0,
out_creds=0x5ab02378) at test_abc.c:696
(gdb) x/s 0x59c00eb0
0x59c00eb0: "$\247\016\227"
(gdb) x/s 0x5ab022a8
0x5ab022a8: ""
Could someone please tell me how I can solve this prob? Thanks in advance!
But many times I am getting either Null value i.e. " " or some random numeric values.
There is nothing wrong with what you show. It's just that the memory location you are examining isn't pointing to a string (0x59c00eb0) or is pointing to an empty string (0x5ab022a8).
You didn't present any evidence that these locations should be pointing to a string, and in fact, as (now deleted) comment showed context points to struct _krb5_context, which contains magic number as the first member. Therefore, you should use x/w to examine it.
... fine some times. But many times I am getting either Null value i.e. " "
or some random numeric...
Been there, done that. Allow me to encourage you to be creative.
I sometimes create a function (call it foo? bar? show? dump?), that is not used by the program being debugged. The function is often c-style (because gdb seems to understand that better, and simpler to invoke), global scope, simple. The temporary install of this function close (in the same file?) to what you want to improve the visibility of sometimes helps.
I can then invoke this function using the gdb p command, such as
gdb> p foo
it is possible to pass parameters to foo, but if I'm touching the code to debug something, I usually make foo more capable ... when no parameters it does one thing. Or perhaps use an int parameter (bar(7)) that switches to show more or differently.
Experiment.
This is not typical, and I suspect better knowledge of gdb might be worth the effort, if I could remember it to the next time I need it. Sometimes gdb just doesn't understand, and I can't figure out why. Other times, I get away with adding a pointer and trying to print that:
gdb> p *foobar

An easy way to watch a vector element in debugger

I'm using Visual Studio 2013 and in its long history it was never able to show a vector element in debugger, complaining with no operator "[]" matches these operands message. I know that there is a workaround requiring typing v.operator[](n), but this is not acceptable for me. I want to hover the cursor above v[n] and see its value or at the most select or cut and paste v[n] to see the value. Is it possible with other Windows C++ IDEs?
I know that all elements of vector are shown in Autos and Locals windows, but my vectors are too long for this to be practical.
Just prefix each [] with _Myfirst in the Watch field:
YourVector._Myfirst[n]
Trick here:
Say you have an std::vector<int> v; and you want to see in the watch v[23] or maybe v[23]..v[23+n] do this:
Add the variable to the watch windows.
Add ,! after the name of the variable (ex: v,!) this indicate VS that you want to turn off debugger visualization.
Expand vector members until you see _Myfirst, _Mylast and _Myend. Add _Myfirst to the watch. This is the pointer to the beginning of the vector memory.
Erase v,! from the watch if you want.
To _Myfirst element added to the watch add at the end + offset, count where offset is the vector index you want to see first in the watch and count is the numbers of element of the vector you want to see. Would be something like this: (*((std::_Vector_val<std::_Simple_types<int> >*)(&(*((std::_Vector_alloc<0,std::_Vec_base_types<int,std::allocator<int> > >*)(&(v)))))))._Myfirst + 23, 100. This let you see 100 elements of the vector starting in position 23 (yes I known it's large the _Myfirst element). You could specify offset and count using variables (ex: to match an expression in the code like v[n] use as offset n and count whatever you want, constant or variable.
Some info about Debugging Tips and Tricks, Going Native Episode 28 from minute 17 have some goodies, the evaluation expression could be in comments. Example you have some code.
v[n] = ... + pre_calculate(v[n]) + ...
// You could put a comment like this:
// (*((std::_Vector_val<std::_Simple_types<int> >*)(&(*((std::_Vector_alloc<0,std::_Vec_base_types<int,std::allocator<int> > >*)(&(v)))))))._Myfirst + n, 100
// And when you hover the mouse over the selected expression, you see the evaluation. Much better I think.
In MSVC's implementation of the standard library, _M_start, _M_finish, and _M_end_of_storage are public members in _Vector_base that can be used.
vector._M_start[n]
This method will be valid for any C++ IDE youre using; First you have to know the vector member name wich stores the first element.
(In borland 6 c++) you can do this by inspecting the vector once you have already added to the watches window. In Visual it is called different.
Then you have to add the following syntax in watches:
nameVector.memberWichPointsToTheFirstElement[startIndex],numElementsDesiredToDisplay.
And your done. But when the vector is inside an instance, it will not show anything saying: side effects are not allowed.

How do I know when a variable is accessed within my code?

I'm using VS2008 to write a program. There's one specific line in my code that causes a numerical error. It is:
Qp[j] = (Cp - Cm)/(Bp + Bm);
Qp is a std::vector. When I comment this line out, the numerical error disappears. I am going through my code line by line to find all the places that access Qp[j]. I was wondering if there was a feature in VS2008 or a linux program that wraps around the executable that can identify every line of code that reads from that section of memory (the specific element in the vector)?
I tried searching online but the keywords I used brought up results relating to global variables.
--- EDIT
Hi all. To those have responded, thank you. Just to clarify my question:
Imagine I have a vector with 5 elements. I'd like to know all the places in my code that use the value stored in element 3 at any point in time during execution. Is there an easy way to do this?
I am not sure if I understand you correctly, but if you comment out that line and the code works then maybe the problem is that line, and you don't need to check others lines.
Maybe in your case you get in the situation where Bp+Bm = 0 (division by zero error).
Qp may not have as many elements as the index j, check the size of Qp.