|
| 1 | +# Windows 10's Windows Subsystem for Linux |
| 2 | +With the release of Windows 10 Creators Update, you will now be able to use Visual Studio Code and the Microsoft C/C++ extension to debug your `Windows Subsystem for Linux (WSL)` [Bash on Ubuntu](https://msdn.microsoft.com/en-us/commandline/wsl/about) projects. |
| 3 | + |
| 4 | +Code can be written on Windows itself using VSCode and debugged through `bash.exe` to the Bash on Windows layer. |
| 5 | + |
| 6 | +**NOTE: Creator's Update is required due to bugfixes within the subsystem that we rely on to provide debugging. Debugging using a previous version of WSL is unsupported and likely will not work.** |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | +* [Windows 10 Creators Update with Windows Subsystem for Linux and Bash](https://msdn.microsoft.com/en-us/commandline/wsl/install_guide) installed. |
| 10 | +* Install g++/gcc and gdb within `WSL` to allow compiling and debugging. You can use the package manager to do this. For example, to install g++, you can run `sudo apt install g++` in the Bash window. |
| 11 | +* [Visual Studio Code](https://code.visualstudio.com) + Microsoft C/C++ extension for VSCode. |
| 12 | + |
| 13 | +## How-To |
| 14 | +To debug, commands will be routed from Windows through `bash.exe` to set up debugging. Because our extension runs as a 32-bit process, it will need to use the `C:\Windows\SysNative` folder to access the `bash.exe` executable that is normally in `C:\Windows\System32`. We will be using the `"pipeTransport"` ability within the extension to do debugging and `"sourceFileMap"` to map the source from the subsystem's paths back to Windows path. |
| 15 | + |
| 16 | +**NOTE: Applications will need to be compiled in the `Windows Subsystem for Linux (WSL)` prior to debugging.** |
| 17 | + |
| 18 | +### Example `launch.json` for Launching |
| 19 | + |
| 20 | +In the following example, I have a local drive, `Z:\` that has my source code within windows for an app called kitchensink. I have set up the `"program"` and `"cwd"` paths to point to the directory within `WSL`. I have set up the `"pipeTransport"` to use `bash.exe`. I have also set up a `"sourceFileMap"` to have everything that is returned by `gdb` that starts with `/mnt/z` to point to `Z:\\` in Windows. |
| 21 | + |
| 22 | +``` |
| 23 | + { |
| 24 | + "name": "C++ Launch", |
| 25 | + "type": "cppdbg", |
| 26 | + "request": "launch", |
| 27 | + "program": "/mnt/z/Bash/kitchensink/a.out", |
| 28 | + "args": ["-fThreading"], |
| 29 | + "stopAtEntry": false, |
| 30 | + "cwd": "/mnt/z/Bash/kitchensink", |
| 31 | + "environment": [], |
| 32 | + "externalConsole": true, |
| 33 | + "windows": { |
| 34 | + "MIMode": "gdb", |
| 35 | + "setupCommands": [ |
| 36 | + { |
| 37 | + "description": "Enable pretty-printing for gdb", |
| 38 | + "text": "-enable-pretty-printing", |
| 39 | + "ignoreFailures": true |
| 40 | + } |
| 41 | + ] |
| 42 | + }, |
| 43 | + "pipeTransport": { |
| 44 | + "pipeCwd": "", |
| 45 | + "pipeProgram": "c:\\windows\\sysnative\\bash.exe", |
| 46 | + "pipeArgs": ["-c"], |
| 47 | + "debuggerPath": "/usr/bin/gdb" |
| 48 | + }, |
| 49 | + "sourceFileMap": { |
| 50 | + "/mnt/z": "z:\\" |
| 51 | + } |
| 52 | + } |
| 53 | +``` |
| 54 | + |
| 55 | +### Example `launch.json` for Attaching to an Existing Process |
| 56 | + |
| 57 | +This configuration similar to the launch process above. I have chosen to start the same application above from the Bash command line and now I want to attach to it for debugging. I have changed the `"processID"` to use the remote process picker by specifying the command `"${command:pickRemoteProcess}"` and set up the same `"sourceFileMap"`. When I press F5 to attach, I get a picker drop down showing the running processes within `WSL`. I can scroll or search for the process I want to attach to and start debugging. |
| 58 | + |
| 59 | +``` |
| 60 | + { |
| 61 | + "name": "C++ Attach", |
| 62 | + "type": "cppdbg", |
| 63 | + "request": "attach", |
| 64 | + "program": "/mnt/z/Bash/kitchensink/a.out", |
| 65 | + "processId": "${command:pickRemoteProcess}", |
| 66 | + "windows": { |
| 67 | + "MIMode": "gdb", |
| 68 | + "setupCommands": [ |
| 69 | + { |
| 70 | + "description": "Enable pretty-printing for gdb", |
| 71 | + "text": "-enable-pretty-printing", |
| 72 | + "ignoreFailures": true |
| 73 | + } |
| 74 | + ] |
| 75 | + }, |
| 76 | + "pipeTransport": { |
| 77 | + "pipeCwd": "", |
| 78 | + "pipeProgram": "c:\\windows\\sysnative\\bash.exe", |
| 79 | + "pipeArgs": ["-c"], |
| 80 | + "debuggerPath": "/usr/bin/gdb" |
| 81 | + }, |
| 82 | + "sourceFileMap": { |
| 83 | + "/mnt/z": "z:\\" |
| 84 | + } |
| 85 | + } |
| 86 | +``` |
0 commit comments