Is there a way to know whether a variable have been init?

Hi all,
I am currently using python interface to run a non-interactively script based on LLDB to trace the variable changing during each step during the program running.

C allows a variable to be declared at first without initialization. And the value of the variable after declaration could be a random value before initialized according to my test using LLDB with the following code compiled by clang+llvm.

----Example Code ----

#include <stdio.h>
#include <stdlib.h>
int g = 42;
float u = 5;
struct node {
int field;
struct node* next;
void f() {
g = g + 1;
printf(“Hello world!\n”);

int main(int argc, char argv[])
int x;
int y=10;
x = y+1;
struct node
root = (struct node*)malloc(sizeof(struct node));
root->field = 3;
root->next = (struct node*)malloc(sizeof(struct node));
root->next->field = 2;

int i = 0;
i = i + 1;
return 0;

-----End Example Code ----

My question is:
In LLDB, is there a way to check whether a variable is currently be declared, and have not been initialized? So that my non-interactively script could check this, and ignore to show it before initialized ?

Thanks very much.

Kun Ling


------------------ Original ------------------

There is no obvious and general way to know what you want to know
Consider the following

int x;
if (foo)
  x = 1;
// is x written to here or not?

If you wanted to pursue this, you could of course track the memory locations of all variables, and step through the code, and figure out when a write to those locations occur. From that point on, your variable would be initialized.
I expect this to be unusably slow for anything but very very small portions of code.

I guess the next question is "what are you really trying to do"?

Most compilers don't adequately describe variable lifetimes in the DWARF debug info. They have the option to describe them, but most just make a slot on the stack and say the variable is in scope in the address range of the lexical block that the variable is defined in. They could describe this in detail, but they just don't and there is no way to tell when they are or are not fully describing variable locations.

If you truly want to detect this stuff, you should build your program with the address sanitizer. It can catch use of uninitialized memory issues you seem to want to catch.