How to show an environment variable (that I did not set) in lldb? - environment-variables

I would like to inspect the defined environment variables in various moments during the run of a program.
settings show target.env-vars only shows me env-vars I've set in lldb, not all the environment variables available to the running process.
How do I read the value of an environment variable that is available to the running process (either because the program was started with that variable or the variable was defined during the run) in lldb?

lldb doesn't keep track of the environment actually in the program, but at least on most Unix systems you can access it through the environ global variable, so something like this will work:
(lldb) expr int idx = 0; while (1) { char *str = ((char **)environ)[idx]; if (str == (char *) NULL) break; else printf("%d: %s\n", idx++, str); }
If you do this a lot, then you can put:
command alias print_real_env expr int idx = 0; while (1) { char *str = ((char **)environ)[idx]; if (str == (char *) NULL) break; else printf("%d: %s\n", idx++, str); }
in your ~/.lldbinit file, and then just run print_real_env.

Related

flex atoi(yytext) does not assign value to a variable

I am making simple lexer using flex. I want to read yytext value and save it as an integer in variable t, but when I compile it it shows me following error:
error: stray ‘\35’ in program
t = atoi(yytext);
Here is the code:
%{
#include "global.h"//contains stdlib
int t=0;
%}
DIGIT [0-9]
%%
{DIGIT} {
printf("found an integer, = %d \n", atoi( yytext));//this compiles without errors
t = atoi(yytext); //here I have error
//...rest of code
}
%%
main(){
yylex();
}
Typically:
error: stray ‘\35’ in program
are connect to the use of wrong quoation marks "
Example:
`a` ‘a’ instead of 'a'
”a“ ... instead of "a"
See if this appears in your "global.h"

`lldb` gives random output on same function call [duplicate]

I have an universal iOS app targeting iOS SDK 6.1, and the compiler is set to Apple LLVM compiler 4.2. When I place a breakpoint in my code and run the following I get weird results for sin(int).
For reference, sin(70) = 0.7739 (70 is in radians).
(lldb) p (double)sin(70)
(double) $0 = -0.912706376367676 // initial value
(lldb) p (double)sin(1.0)
(double) $1 = 0.841470984807897 // reset the value sin(int) will return
(lldb) p (double)sin(70)
(double) $2 = 0.841470984807905 // returned same as sin(1.0)
(lldb) p (double)sin(70.0)
(double) $3 = 0.773890681557889 // reset the value sin(int) will return
(lldb) p (double)sin(70)
(double) $4 = 0.773890681558519
(lldb) p (double)sin((float)60)
(double) $5 = -0.304810621102217 // casting works the same as appending a ".0"
(lldb) p (double)sin(70)
(double) $6 = -0.30481062110269
(lldb) p (double)sin(1)
(double) $7 = -0.304810621102223 // every sin(int) behaves the same way
Observations:
The first value for sin(int) in a debug session is always -0.912706376367676.
sin(int) will always return the same value that was returned from the last executed sin(float).
If I replace p with po, or expr (e.g. expr (double)sin(70)), I get the same exact results.
Why is the debugger behaving like this?
Does this mean that I should type cast every single parameter each time I call a function?
Some more interesting behavior with NSLog:
(lldb) expr (void)NSLog(#"%f", (float)sin(70))
0.000000 // new initial value
(lldb) expr (void)NSLog(#"%f", (float)sin(70.0))
0.773891
(lldb) expr (void)NSLog(#"%f", (float)sin(70))
0.000000 // does not return the previous sin(float) value
(lldb) p (double)sin(70)
(double) $0 = 1.48539705402154e-312 // sin(int) affected by sin(float) differently
(lldb) p (double)sin(70.0)
(double) $1 = 0.773890681557889
(lldb) expr (void)NSLog(#"%f", (float)sin(70))
0.000000 // not affected by sin(float)
You're walking into the wonderful world of default argument promotions in C. Remember, lldb doesn't know what the argument types or return type of sin() is. The correct prototype is double sin (double). When you write
(lldb) p (float) sin(70)
there are two problems with this. First, you're providing an integer argument and the C default promotion rules are going to pass this as an int, a 4-byte value on the architectures in question. double, besides being 8-bytes, is an entirely different encoding. So sin is getting garbage input. Second, sin() returns a double, or 8-byte on these architectures, value but you're telling lldb to grab 4 bytes of it and do something meaningful. If you'd called p (float)sin((double)70) (so only the return type was incorrect) lldb would print a nonsensical value like 9.40965e+21 instead of 0.773891.
When you wrote
(lldb) p (double) sin(70.0)
you fixed these mistakes. The default C promotion for a floating point type is to pass it as a double. If you were calling sinf(), you'd have problems because the function expected only a float.
If you want to provide lldb with a proper prototype for sin() and not worry about these issues, it is easy. Add this to your ~/.lldbinit file,
settings set target.expr-prefix ~/lldb/prefix.h
(I have a ~/lldb directory where I store useful python files and things like this) and ~/lldb/prefix.h will read
extern "C" {
int strcmp (const char *, const char *);
void printf (const char *, ...);
double sin(double);
}
(you can see that I also have prototypes for strcmp() and printf() in my prefix file so I don't need to cast these.) You don't want to put too many things in here - this file is prepended to every expression you evaluate in lldb and it will slow your expression evaluations down if you put all the prototypes in /usr/include in there.
With that prototype added to my target.expr-prefix setting:
(lldb) p sin(70)
(double) $0 = 0.773890681557889

Use of context in C++ API

I have the following program , which transforms a string into a Boolean formula (string_to_formula), where I am defining expr_vector b(c). This code works, but I am not being able to reason about the context. What is the function of a context? Is there any way we can define the variable b just once? Why do we need to send the context to the function? And can this code be written in a more succinct way?
int main() { try {
context c;
expr form(c);
form = string_to_formula("x1x00xx011",c);
expr form1(c);
form1 = string_to_formula("1100x1x0",c);
solver s(c);
s.add(form && form1);
s.check();
model m = s.get_model();
cout << m << "\n";
}
expr string_to_formula(string str, context& c )
{
expr_vector b(c) ;
for ( unsigned i = 0; i < str.length(); i++)
{ stringstream b_name;
b_name << "b_" << i;
b.push_back(c.bool_const(b_name.str().c_str()));
}
expr formula(c);
formula = c.bool_val(true);
for( unsigned i = 0 ; i < str.length() ; ++i )
{ char element = str.at(i) ;
if ( element == '1' )
formula = formula && ( b[i] == c.bool_val(true) ) ;
else if ( element == '0' )
formula = formula && ( b[i] == c.bool_val(false) ) ;
else if ( element == 'x' )
continue;
}
return formula;
}
The context object is relevant for multi-threaded programs.
Each execution thread can have its own context, and they can be accessed without using any form of synchronization (e.g., mutexes).
Each expression belongs to a single context. We cannot use the same expression in two different contexts, but we can copy them from one context to another.
In Z3, expressions are maximally shared. For example, if we have an expressions such as (f T T) where T is a big term, then internally Z3 has only one copy of T. For implementing this feature, we use a hashtable. The hashtable is stored in the context.
If we use the same context C in two different execution threads, Z3 will probably crash due to race conditions updating C.
If your program has only one execution thread, you can avoid "moving" the context around by having a global variable.
The idea of context/manager is present in many libraries. For example, in CUDD (BDD library), they have a DdManager. In the script language Lua, they have a lua_State. These are all instances of the same idea.

Traversing Z3_ast tree in C/C++

In short, I need to be able to traverse Z3_ast tree and access the data associated with its nodes. Cannot seem to find any documentation/examples on how to do that. Any pointers would be helpful.
At length, I need to parse smt2lib type formulae into Z3, make some variable to constant substitutions and then reproduce the formula in a data structure which is compatible with another unrelated SMT sovler (mistral to be specific, I don't think details about mistral are important to this question but funnily enough it does not have a command line interface where I can feed it text formulae. It just has a C API). I have figured that to generate the formula in mistral's format, I would need to traverse the Z3_ast tree and reconstruct the formula in the desired format. I cannot seem to find any documentation/examples that demonstrate how to do this. Any pointers would be helpful.
Consider using the C++ auxiliary classes defined at z3++.h. The Z3 distribution also includes an example using these classes. Here is a small code fragment that traverses a Z3 expression.
If your formulas do not contain quantifiers, then you don't even need to handle the is_quantifier() and is_var() branches.
void visit(expr const & e) {
if (e.is_app()) {
unsigned num = e.num_args();
for (unsigned i = 0; i < num; i++) {
visit(e.arg(i));
}
// do something
// Example: print the visited expression
func_decl f = e.decl();
std::cout << "application of " << f.name() << ": " << e << "\n";
}
else if (e.is_quantifier()) {
visit(e.body());
// do something
}
else {
assert(e.is_var());
// do something
}
}
void tst_visit() {
std::cout << "visit example\n";
context c;
expr x = c.int_const("x");
expr y = c.int_const("y");
expr z = c.int_const("z");
expr f = x*x - y*y >= 0;
visit(f);
}

printout lexemes and tokens of a C code with using flex

i'm trying to print out lexemes and tokens with using lexical analyzer "flex" and the problem is i can find lexemes and can just print tokens not lexemes. this is the simple code which i use as you can see below
%{
#include<stdio.h>
char RW[] = "RESERVE_WORD";
%}
int [i][n][t]
%%
int printf("%s --> %s\n", yylex(), RW);
.|\n { /* Ignore all other */}
%%
int main(int argc, char *argv[]) {
yyin = fopen(argv[1], "r");
yylex();
fclose(yyin);
return 0;
}
when i make a lexical analysis this yylex() function returns "null" and it says
example5.l:8:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat].
i will be glad if you can help me. and thanks anyway
ok i handled the problem. so the thing is we should use yytext variable which contains the last token of the lexical analyzer as a string. In Addition, yylex() function will return either the value of the next token or a number <= 0 indicating EOF.

Resources