WSL: [WSL1] Cannot debug anything after upgrading to 22.04
Version
Microsoft Windows [Version 10.0.19044.1526]
WSL Version
- WSL 2
- WSL 1
Kernel Version
Linux version 4.4.0-19041-Microsoft (Microsoft@Microsoft.com) (gcc version 5.4.0 (GCC) ) #1237-Microsoft Sat Sep 11 14:32:00 PST 2021
Distro Version
Linux version 4.4.0-19041-Microsoft (Microsoft@Microsoft.com) (gcc version 5.4.0 (GCC) ) #1237-Microsoft Sat Sep 11 14:32:00 PST 2021
Other Software
GNU gdb (Ubuntu 11.1-0ubuntu2) 11.1 gcc version 11.2.0 (Ubuntu 11.2.0-7ubuntu2)
Repro Steps
Consider the following C file:
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}
- build the program with proper flags
- debug it with gdb, and try to insert breakpoints
Trying to debug it results in the following;

Trying to debug the same program makes LLDB hang.
Expected Behavior
GDB and LLDB should have worked
Actual Behavior
GDB doesn’t work (see strace), LLDB hangs.
Diagnostic Logs
(did not follow fork - caused program startup failure)
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 12
- Comments: 23 (2 by maintainers)
Are you enabled jammy-proposed in sources.list ?
Yes. Binary patch is really hack, so you have to find the correct offset for each different binary . for
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1, the new patch offset is0x335C7D, and the one-line patch command isecho -en '\x90\x90' | sudo dd of=/usr/bin/gdb count=2 bs=1 conv=notrunc seek=$((0x335C7D))I suppose there is no generic command, but there is generic method, as shown below.
Download gdb_12.0.90-0ubuntu1_amd64.deb and its debug info gdb-dbgsym_12.1-0ubuntu1~22.04_amd64.ddeb, then do binary reversing engineering (with a decompiler).
Find
linux_nat_target::xfer_partialfunction (its location is different from each binary), see decompiled pseudo code here:Have a look at the disassembly code:
The hack method is forcing code to jump to
inf_ptrace_target::xfer_partialeven if theobjectmatchscase TARGET_OBJECT_MEMORY, and it just needs to patch the two-byte instructionjz short loc_335CD8at0x335C7Dtonop ; nop(aka “\x90\x90”).For
gdb12.0.90-0ubuntu1from Ubuntu 22.04 (download link), a temporary solution is patching two bytes in/usr/bin/gdbwhich offset at0x335badfrom\x74\x59to\x90\x90(or simply use this command:echo -ne '\x90\x90' | sudo dd of=/usr/bin/gdb seek=$((0x335bad)) bs=1 count=2 conv=notrunc), this will re-enable the ptrace fallback and the patched binary should work well.However, I hope this issue keeps open, until WSL1 add support of
/proc/<PID>/memfeature. The file is really important in Linux /proc Filesystem, but it is still not solved after 6 years since year 2016 when #547 opens !I installed GDB from Ubuntu Support Team PPA (add that repo then
sudo apt update && sudo apt dist-upgrade) and the error doesn’t happen anymore.The real reason for this issue is that WSL1 lacks
/proc/*/memfiles (as well as/proc/*/task/*/memfiles).A tracer can read memory of tracee by either reading
/proc/<PID>/memfile or usingptrace(PTRACE_PEEKTEXT, ...) / ptrace(PTRACE_PEEKDATA, ...) syscall, and old version gdb will fallback to useptracewhen it fails to read/proc/<PID>/mem.However, in this commit on 1 Jul 2021, palves says:
Gdb disables such fallback and always assumes
/proc/<PID>/memis available, but WSL1 does not has this file, so gdb on Ubuntu 22.04 cannot access memory under WSL1.Related issue: #547
@craigloewen-msft Notice this is also an issue in WSL 2. I have it, and a few others above pointed out it also happens to them.
Im using wsl2 right now with the same issue, this is really annoying and tricky, I tried to use older-version of gdb(12.0->11.2), but the same problem exists, I truly hope wsl will sooner or later fix this problem but Im not very positive about this, as far as I know this is not a new problem. Maybe using virtual machine will be end of journey.
Update : This problem is fundamentally due to the newer version of gdb only support /proc/PID/mem while the older version supports ptrace as well, so if you really need gdb, I highly recommend you roll back your system version, for example from Ubuntu 22.04LTS to Ubuntu 20.04LTS, Im using Arch and roll back my system version doesn’t work while for Ubuntu it is useful, so the problem I guess is somehow related to the system as well, Im just confused
This worked like a charm for me.
It works! Thank you so much.
Sorry for that. I miss the
conv=notruncargument forddcommand. The correct command should beecho -ne '\x90\x90' | sudo dd of=/usr/bin/gdb seek=$((0x335bad)) bs=1 count=2 conv=notrunc. (Before this, you should re-install gdb to recover the original/usr/bin/gdbfile (e.g. bysudo apt-get install --reinstall gdb) )Ran into this yesterday, agree this is pretty horrible, ended up using a different workaround that worked for my limited needs. Took a dump in an old debugger that couldn’t read the symbols, opened the dump in a new debugger.
Unfortunately we’re short on people. @craigloewen-msft for visibility
Seeing the same problems as everyone else for GDB 12.1 on WSL1 Ubuntu 22.04 LTS.
I didn’t want to patch my GDB using
ddso I ended up just downloading the GDB 13.2 tarball and running the good oldOf course, the downside is that
aptis not aware of this GDB, which lives in/usr/local/binfor me, but it works fine.