Accessing attached process environment variables with SBAPI

Hi all,

I’m trying to read the value of an environment variable that a process was launched with but to which lldb attached to after it launched. SBEnvironment looked interesting but I tried using

script print(lldb.debugger.GetSelectedTarget().GetEnvironment().Get("PRINT_ME")

and that prints ‘None’.

I am able to get the value using

script addr = lldb.debugger.GetSelectedTarget().EvaluateExpression("(char *)getenv(\"PRINT_ME\")").GetValueAsUnsigned()

script err = lldb.SBError()

script print(lldb.debugger.GetSelectedTarget().GetProcess().ReadCStringFromMemory(addr, 1024, err))

but that seems like overkill for reading an environment variable. Is there a better way to do this that I’m missing?

I’m using the following program to quickly test things:

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
int main() {
printf("%s\n", getenv("PRINT_ME"));

The SBEnvironment classes and the setting for the environment are currently used just for launching processes. lldb doesn’t keep track of the “live” state of the environment in a process - which can change as the program runs. It would certainly be useful to have a “printenv” function in lldb that fetched the current process environment, however. Please file an enhancement request or propose a patch.