microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.52k stars 1.55k forks source link

Breakpoints are set in all files with same name #6754

Open psclkhoury opened 3 years ago

psclkhoury commented 3 years ago

When setting a breakpoint in a file with a particular name, breakpoints are set in all files with that file name at the same line.

I saw #977 where the same issue is reported, but the provided workaround does not work and there is no response anymore from the team.

This is a deal breaker for me as I cannot use the debugger at all in the current situation. The issue has been open for 3 years already. If there is no intent of providing a good fix for this, can you let us know so we can reconsider our options?

WardenGnaw commented 3 years ago

Can you fill out the debugger bug template?

**Describe the bug**
- OS and Version:
- VS Code Version:
- C/C++ Extension Version:
- Other extensions you installed (and if the issue persists after disabling them):
- A clear and concise description of what the bug is.

**To Reproduce**
*Please include a code sample and `launch.json` configuration.*
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**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.
psclkhoury commented 3 years ago

@WardenGnaw As I said in the description, this is the SAME issue as the one I referenced (#977). Maybe it's easier to reopen the other issue instead of me copy pasting stuff from there to here.

When setting a breakpoint in a file with a particular name, breakpoints are set in all files with that file name at the same line.

Reproduce

Create the following project layout:

src
 |- main.c
 |- folder
    |- main.c

That is, there are two main.c files. One directly in src:

int foo();
int main(int argc, char *argv[])
{
    int a = 0;
    a += 3;
    a *= 4;

    foo();

    return 0;
}

The other in src/folder:

int foo()
{
    int b = 14;
    int d = 13;
    b += d - b * d;
    return b + d;
}

In both files, line 5 contains executable code.

Compile from within src:

gcc main.c folder/main.c -g -O0 -o a.exe

Add launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/src/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

Set breakpoint in src/main.c at line 5. Launch debugger, it breaks in main, hit continue and it will break in foo (which it should not).

The above setup:

Breakpoints.zip

Settings

    "debug.allowBreakpointsEverywhere": true

c_cpp_properties.json is not in use and launch.json is given above.

cpptools version: 1.1.3 Version: 1.52.1 (user setup) Commit: ea3859d4ba2f3e577a159bc91e3074c5d85c0523 Date: 2020-12-16T16:34:46.910Z Electron: 9.3.5 Chrome: 83.0.4103.122 Node.js: 12.14.1 V8: 8.3.110.13-electron.0 OS: Windows_NT x64 10.0.18363

WardenGnaw commented 3 years ago

As per the comments, the fix is to add:

            "sourceFileMap":{
                "C:\\path\\to\\project\\src": {
                    "editorPath": "C:\\path\\to\\project\\src",
                    "useForBreakpoints": true
                }
            },

With engineLogging enabled, you will see that the breakpoints are binding with the full path. E.g.

1: (298) <-1011-break-insert -f main
1: (350) <-1012-break-insert -f C:/path/to/project/src/main.c:5
1: (391) <-1014-break-insert -f C:/path/to/project/src/folder/main.c:5

If not, the breakpoints will be bound by filename:line number

1: (319) <-1011-break-insert -f main
1: (368) <-1012-break-insert -f main.c:5
1: (426) <-1015-break-insert -f main.c:5
psclkhoury commented 3 years ago

@WardenGnaw I already tried that and it did not solve the issue for me. The logging you mention was also not present.

See https://github.com/microsoft/vscode-cpptools/issues/977#issuecomment-726325642 and https://github.com/microsoft/vscode-cpptools/issues/977#issuecomment-726346927.

Also as was mentioned in the other issue, this is a workaround and not a proper fix.

diyessi commented 3 years ago

I have encountered this problem in VSC since 2016 when first starting to use VSC (MacOS and Ubuntu). I don't think I have ever seen it work properly in all that time.

psclkhoury commented 3 years ago

@WardenGnaw the issue I mentioned here seems related to #4017, although I dont have relative paths.

josuegomes commented 3 years ago

Why the solution is so badly described?

  1. Edit launch.json
  2. Add the following entry
           "sourceFileMap": {
                "${workspaceFolder}": {
                    "editorPath": "${workspaceFolder}",
                    "useForBreakpoints": "true"
                }
            }
  1. If you want to add logs to confirm the configuration add this entry
            "logging": {
                "engineLogging": true
            }

For reference this is my complete launch.json (sensitive data removed):

{
    // 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": "g++ - Build and debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/app",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "make",
            "miDebuggerPath": "/usr/bin/gdb",
            "sourceFileMap": {
                "${workspaceFolder}": {
                    "editorPath": "${workspaceFolder}",
                    "useForBreakpoints": "true"
                }
            },
            "logging": {
                "engineLogging": true
            }
        }
    ]
}
geekfivestart commented 3 years ago

I encountered the same issue and solved it as @josuegomes said, but I believe this is a bug and should be fixed elegantly.

psclkhoury commented 3 years ago

@geekfivestart I tried what @josuegomes said but I still got the same error that I reported here. Maybe it's because I am running Windows?

image

This bug has been reported 4 years ago and it is still not fixed, and there is very little feedback from the team. It seems that they don't want to fix it.

josuegomes commented 3 years ago

@psclkhoury sorry to hear it didn't work for you on Windows.

psclkhoury commented 3 years ago

@WardenGnaw Any update on this?

diyessi commented 3 years ago

It was still happening to me yesterday.

psclkhoury commented 3 years ago

@WardenGnaw any update?

agosdahu commented 3 years ago

same issue debugging with an elf built in a docker environment does not seem to find proper breakpoint

sketch34 commented 1 year ago

Same issue.

GNU gdb (GDB) 12.1
Name: C/C++
Id: ms-vscode.cpptools
Description: C/C++ IntelliSense, debugging, and code browsing.
Version: 1.12.4
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools
Version: 1.73.1 (user setup)
Commit: 6261075646f055b99068d3688932416f2346dd3b
Date: 2022-11-09T04:27:29.066Z
Electron: 19.0.17
Chromium: 102.0.5005.167
Node.js: 16.14.2
V8: 10.2.154.15-electron.0
OS: Windows_NT x64 10.0.22000
Sandboxed: No

image

zrhoffman commented 1 year ago

Adding

           "sourceFileMap": {
                "${workspaceFolder}": {
                    "editorPath": "${workspaceFolder}",
                    "useForBreakpoints": "true"
                }
            }

to my launch configuration, as mentioned in https://github.com/microsoft/vscode-cpptools/issues/977#issuecomment-767445913, fixes the issue for me (on Linux). Maybe #6754 could be resolved by making this the default setting.

buzhangjiuzhou commented 1 year ago

Why the solution is so badly described?

  1. Edit launch.json
  2. Add the following entry
           "sourceFileMap": {
                "${workspaceFolder}": {
                    "editorPath": "${workspaceFolder}",
                    "useForBreakpoints": "true"
                }
            }
  1. If you want to add logs to confirm the configuration add this entry
            "logging": {
                "engineLogging": true
            }

For reference this is my complete launch.json (sensitive data removed):

{
    // 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": "g++ - Build and debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/app",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "make",
            "miDebuggerPath": "/usr/bin/gdb",
            "sourceFileMap": {
                "${workspaceFolder}": {
                    "editorPath": "${workspaceFolder}",
                    "useForBreakpoints": "true"
                }
            },
            "logging": {
                "engineLogging": true
            }
        }
    ]
}

It works for me!