Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variables disappear when the program forks when debugging a C++ program #13051

Open
barondim opened this issue Dec 13, 2024 · 0 comments
Labels
debugger help wanted Can be fixed in the public (open source) repo.

Comments

@barondim
Copy link

Environment

  • OS and version: Ubuntu 20.04.5 LTS (x64 5.15.0-56-generic)
  • VS Code: 1.95.3 (Commit: microsoft/vscode@f1a4fb1)
  • C/C++ extension: v1.22.11
  • OS and version of remote machine (if applicable): N/A
  • GDB / LLDB version: GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2

Bug Summary and Steps to Reproduce

Bug Summary: Environment variables disappear when the program forks when debugging a C++ program

VScode gets rid of environment variables when I debug a C++ programs which forks.

Here is an example with a 2 files project :

hello1.cpp

#include <boost/process.hpp>


namespace ps = boost::process;

int main() {
  std::string command = "/home/user/MyProject/hello2";
  ps::child c(command);

  c.wait();

  return 0;
}

hello2.cpp

#include <iostream>


int main() {
  std::cout << "Hello world" << std::endl;

  const char* myVarEnv = std::getenv("FOO");
  std::cout << "myVarEnv = " << myVarEnv << std::endl;

  return 0;
}

to build hello1.cpp and hello2.cpp :

g++ -g hello1.cpp -o hello1 -lpthread
g++ -g hello2.cpp -o hello2

What is astonishing is when I debug hello1 I can see my FOO environment variable is correctly defined :

-exec show environment FOO
FOO = foo

Then I switch the follow-fork-mode to child :

-exec set follow-fork-mode child

But when I fork in hello2, gdb can't find the FOO environment variable anymore :

-exec show environment FOO
Environment variable "FOO" not defined.

but oddly enough, the program hello2 manages to print the environment variable correctly :

myVarEnv = foo

Moreover, if I debug this program with gdb, gdb can find the FOO environment variable perfectly in both hello1 and hello2 :

(gdb) show environment FOO     
FOO = foo

Since, VScode just uses gdb, that means VScode should be able to find the FOO environment normally, right ?

Debugger Configurations

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "/home/user/MyProject/hello1",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [
        {"name": "FOO", "value": "foo"}
      ],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
          {
              "description": "Enable pretty-printing for gdb",
              "text": "-enable-pretty-printing",
              "ignoreFailures": true
          },
          {
              "description": "Set Disassembly Flavor to Intel",
              "text": "-gdb-set disassembly-flavor intel",
              "ignoreFailures": true
          }
      ]
    }
  ]
}

Debugger Logs

Hello world
myVarEnv = foo
[1] + Done

Other Extensions

No response

Additional Information

No response

@sean-mcmanus sean-mcmanus added debugger help wanted Can be fixed in the public (open source) repo. labels Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
debugger help wanted Can be fixed in the public (open source) repo.
Projects
None yet
Development

No branches or pull requests

2 participants