Background
I’m running an x86/amd64 Debian 11 environment inside Docker on my MacBook Air with an M1 chip (Apple Silicon). I want to use gdb
to debug programs compiled for amd64, but I’ve encountered issues.
After some research, I found several resources discussing how to debug x86 programs on macOS with Docker and Rosetta:
- Unable to debug amd64 binaries on apple silicon · Issue #6921 · docker/for-mac · GitHub
- Unable to Debug on Apple M1/M2 when using linux/amd64 devcontainer · Issue #10788 · microsoft/vscode-cpptools · GitHub
I’m currently using the method described in those posts. For example, to debug a program called bbsort
, I do:
- Run the program with Rosetta debugserver:
ROSETTA_DEBUGSERVER_PORT=1234 ./bbsort &
- Launch
gdb
:
gdb ./bbsort
- Connect to the Rosetta debugserver:
target remote localhost:1234
- Set a breakpoint:
b 14
- Continue execution:
continue
The Problem
This approach works for attaching to already-running processes, but I can’t provide input via file redirection (e.g., < input.txt
) in gdb
.
For example, I have this simple test program:
// test_input.c
#include <stdio.h>
#include <string.h>
int main() {
char buffer[256];
printf("Program started. Waiting for input...\n");
fflush(stdout); // Ensure message is printed immediately
if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
// Remove trailing newline
buffer[strcspn(buffer, "\n")] = 0;
printf("Program received: \"%s\"\n", buffer);
} else {
printf("Program received no input or an error occurred.\n");
}
fflush(stdout);
printf("Program finished.\n");
fflush(stdout);
return 0;
}
After compiling it into an amd64 binary named test_input_x86_64
, I want to debug it with input redirected from a file:
// my_test_input.txt
Hello from GDB test!
What I want to do is:
gdb ./test_input_x86_64 < my_test_input.txt
But this doesn’t work as expected. When I try to debug with gdb
, the program appears to start, but it doesn’t seem to receive any input from my_test_input.txt
.
Here is part of the output:
2023211759 Lab2/# ROSETTA_DEBUGSERVER_PORT=1234 ./test_input_x86_64 &
[2] 179
2023211759 Lab2/# gdb ./test_input_x86_64 < my_test_input.txt
GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./test_input_x86_64...
gdb_59$ Undefined command: "Hello". Try "help".
gdb_59$ Undefined command: "Hello". Try "help".
gdb_59$ quit
2023211759 Lab2/#
Goal
How can I debug an amd64 binary running under Rosetta on a Mac M1 via Docker, and also provide input to it from a file (like gdb ./program < input.txt
), while still being able to set breakpoints and step through the program?
Any help would be greatly appreciated!