microsoft / microsoft/vscode-cpptools

Debugger: error when debug code in "-gdb-set follow-fork-mode child" with popen

Open
#8,109 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

debugger more info needed
Dominant language
TypeScript
Stars
6.2k
Forks
1.7k
Avg merge
14h 46m
Merged PRs (30d)
61

Description

Bug type: Debugger

Describe the bug

  • OS and Version: Debian 9.13
  • VS Code Version: 1.60.0
  • C/C++ Extension Version: v1.6.0
  • Other extensions you installed (and if the issue persists after disabling them):
  • A clear and concise description of what the bug is.

Try to debug forked process by using this command:

                {
                    "description": "The new process is debugged after a fork. The parent process runs unimpeded.",
                    "text": "-gdb-set follow-fork-mode child",
                    "ignoreFailures": true
                }

However, the program being debugged has popen() that creates several threads, which crashes the debug process.

I guess vscode might "set follow-fork-mode child" right before calling fork() and immediately unset it after fork().

To Reproduce
Please include a code sample and launch.json configuration.
Steps to reproduce the behavior:

  1. Touch main.c
  2. Write the code in the file:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    printf("start popen\n");
    popen("echo hello", "r");
    printf("start fork\n");

    int pid = fork();

    if (pid== 0)
        printf("Hello from Child!\n");
  
    // parent process because return value non-zero.
    else
        printf("Hello from Parent!\n");

    return 0;
}

  1. Generate default launch.json and task.json
  2. Modify launch.json as:
{
    // 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": "gcc-6 - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "The new process is debugged after a fork. The parent process runs unimpeded.",
                    "text": "-gdb-set follow-fork-mode child",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc-6 build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}
  1. Set some breakpoints from the start of main and debug step-by-step.
  2. Keep next-ing until debug process dies unnaturally.

Additional context
If applicable, please include logging by adding "logging": { "engineLogging": true, "trace": true, "traceResponse": true } in your launch.json
Add any other context about the problem here including log or error messages in your Debug Console or Output windows.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the main.c reproducer and the launch.json setupCommands using -gdb-set follow-fork-mode child, then enable engineLogging, trace, and traceResponse. Investigate the debugger behavior around popen(), fork(), and the child process; done means debugging the child no longer causes the debug process to die unexpectedly.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, typescript
Domain
devtools, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.