[lldb-dev] Accessing attached process environment variables with SBAPI

Ivan Hernandez via lldb-dev lldb-dev at lists.llvm.org
Fri Jan 28 16:35:48 PST 2022


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() {
  raise(SIGSTOP);
  printf("%s\n", getenv("PRINT_ME"));
}
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-dev/attachments/20220128/e1b42b06/attachment-0001.html>


More information about the lldb-dev mailing list