[lldb-dev] Accessing attached process environment variables with SBAPI
Jim Ingham via lldb-dev
lldb-dev at lists.llvm.org
Mon Jan 31 10:03:07 PST 2022
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.
Jim
> On Jan 28, 2022, at 4:35 PM, Ivan Hernandez via lldb-dev <lldb-dev at lists.llvm.org> wrote:
>
> 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"));
> }
> ```
> _______________________________________________
> lldb-dev mailing list
> lldb-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-dev
More information about the lldb-dev
mailing list