Skip to content

m3gdb: build and work on current Linux (glibc 2.36, gcc 12) - #1250

Merged
hosking merged 16 commits into
modula3:masterfrom
mikanystrom:m3gdb-modern-linux
Nov 22, 2026
Merged

hosking merged 16 commits into
modula3:masterfrom
mikanystrom:m3gdb-modern-linux

Conversation

@mikanystrom

Copy link
Copy Markdown
Contributor

m3gdb has bit-rotted on current Linux. It no longer compiles against current glibc, can't open current system libraries, and crashes or gives wrong results in several places. This series gets it building and working again on AMD64_LINUX, for programs from both the GCC backend and the C backend, dynamically or statically linked.

Tested on Debian 12 (gcc 12.2, glibc 2.36) with the d5.11.9 compiler. m3gdb was built both through cm3 in m3-sys/m3gdb and with configure/make directly. Every commit builds on its own.

Commits

Build and platform

  1. Build with current glibc. paddr_t is gone and the ps_* signatures in <proc_service.h> changed. The i386 changes mirror the amd64 ones but are untested.
  2. Open current shared libraries. BFD rejected the SHT_RELR section in glibc ≥ 2.36 libraries ("not in executable format"). Also silences warnings for newer x86-64 relocation types.
  3. Skip DWARF compilation units of unsupported versions. The C parts of libm3core are DWARF 5 with current gcc. One such unit made gdb give up on the whole objfile, including the Modula-3 stabs, so RT0.Typecell wasn't found and objects couldn't be printed. Versions 2 and 4 are still read, which keeps the DWARF 4 support from 7cb2fa6.
  4. Tolerate TD_NOCAPAB from libthread_db. It used to leave the program stopped at __nptl_create_event.
  5. Restore the CFA rule on DW_CFA_restore_state. This old gdb bug meant there was no backtrace out of abort(), so none for any Modula-3 runtime error, and none from threads blocked in glibc.

Crashes and wrong results in the Modula-3 support

  1. Linespecs: catch_errors() returned pointers through an int, which crashed on LP64. Separately, a discarded SKIP_PROLOGUE result made arguments show garbage at function breakpoints.

  2. struct block: the M3 body_block and the C++ namespace pointer shared a union, so gdb crashed whenever the current language was C or C++.

  3. Compiler-kind detection printed its "Detected compiler kind" messages with error(), which aborted whatever command triggered it. In the "can't detect" case it also aborted every later command (e.g. bt after frame #0).

  4. Sets smaller than a word (e.g. SET OF a 7-element enumeration) printed as {}.

  5. Conditional breakpoints on M3 locations: break Main.P if n = 2 failed outright. Conditions were also parsed in the current language: before run that is C, where n = 2 is an assignment. They're now parsed in the location's language, and info breakpoints prints them in it.

  6. SIG64 in statically linked programs: the runtime's GC-suspend signal stopped the program, because handle SIG64 nostop was only set when libm3core.so was loaded.

  7. Frame arguments are printed in each frame's own language, so mixed C-backend/runtime backtraces print correctly.

  8. Default demangling style gnu-v3: the "auto" style tried the g++ 2.x scheme, which turned the C backend's Main__Fib into Main(int, bool).

  9. Docs (src/index.html): the -no-pie requirement, what to expect from the GCC and C backends, condition syntax, and the correct set language Modula-3.

Testing

Programs must be linked with -no-pie (cm3 -DPOSITION_INDEPENDENT_EXECUTABLE=-no-pie); gdb 6.4 predates PIE support.

  • Feature tests (88 checks, both backends):
    • values of all the common types: subranges, LONGINT, REAL/LONGREAL/EXTENDED, enums, sets, packed records, multi-dimensional and enum-indexed arrays, REF/NIL, procedure values, TEXT, opaque branded objects, interface variables, VAR parameters
    • calling M3 procedures from gdb, := assignment, M3 operators
    • finish, conditional breakpoints, nested procedures with up-level variables, hardware watchpoints
    • breakpoints in forked threads and in libm3
    • backtraces for NIL dereference, subscript range, ASSERT and NARROW failures
  • m3tests sweep: every p0/p1/p2 program with a simple m3makefile (240) plus r0 (5), built statically with both backends. Each is run under m3gdb: break in the main body, bt, info locals, several next/step, finish, then run to the end. Out of 490 runs:
    • 444 pass: no gdb error, the breakpoint is hit, and program output is identical to a run without gdb
    • 16 fail exactly as they do without gdb
    • 8 skipped because they run > 20 s on their own
    • 22 don't build: compiler or test issues, not m3gdb
    • no gdb failures

Known limitations (not addressed here)

  • C backend debug info:
    • object types are emitted without the method pointer and inherited fields, so print *self is wrong
    • values are shown as C, with generated names (n_L_71)
    • conditions use C syntax (==)
  • Expression parser gaps: it doesn't yet support set constructors or IN ("Not yet implemented").
  • Selecting the C backend: the installed cm3.cfg makes M3_BACKEND_MODE readonly, so it currently takes a separate config file. Making the backend and C debug flags settable from the command line would be a possible follow-up.
  • Bugs found in the C backend by the sweep (separate from m3gdb):
    • p053, p160, p232: generated C++ doesn't compile ("'SET' was not declared")
    • p109: an invalid cast
    • p140: crashes at run time (NIL dereference)
    • p268, p270, p277: the compiler fails
    • (the GCC backend handles all of these)

🤖 Generated with Claude Code

mikanystrom and others added 15 commits November 12, 2026 09:32
glibc no longer defines paddr_t, and its <proc_service.h> now declares
ps_get_thread_area and the ps_p*read/write functions with psaddr_t
arguments.  Define paddr_t as psaddr_t when <proc_service.h> is used,
include gregset.h on that path too (gdb_gregset_t was undeclared), and
fix the ps_get_thread_area signatures in gdb and gdbserver.

Tested on AMD64_LINUX (Debian 12, gcc 12, glibc 2.36).  The i386
changes mirror the amd64 ones but are untested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
glibc 2.36 libc.so.6, libm.so.6 and ld-linux-x86-64.so.2 contain a
SHT_RELR section, which this BFD rejected, so gdb reported them as
"not in executable format" and had no symbols for them.  Treat
SHT_RELR as an ordinary section.

Also stop printing "invalid relocation type 37" (R_X86_64_IRELATIVE)
and similar for relocation types 27..43, which postdate this BFD and
only matter to the linker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The C parts of libm3core and libm3 are compiled with the C compiler's
default debug format, DWARF 5 with gcc >= 11.  A single such unit made
dwarf2_build_psymtabs error out for the whole objfile, so the Modula-3
stabs in the same library were never read: RT0.Typecell could not be
found and objects could not be printed.

Skip compilation units whose version the reader does not handle
(anything but 2 or 4), with one warning per objfile, and read the
rest.  Skipping per unit rather than per objfile matters for
statically linked programs, where the runtime's DWARF 5 units sit in
the same executable as the program's own (e.g. C backend, DWARF 2)
debug info.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Current glibc's libthread_db returns TD_NOCAPAB for per-thread event
enabling.  This was a hard error, raised while handling the first
thread-creation event, leaving the inferior stopped at
__nptl_create_event.  The process-wide event mask is sufficient, so
ignore TD_NOCAPAB and only warn for other errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two bugs in the Modula-3 part of decode_line_1:

- parse_expression and evaluate_expression were called through
  catch_errors, whose int result was cast back to a pointer.  On LP64
  hosts this truncates pointers above 4GB; with a PIE gdb (gcc's
  default) "break Main__Proc" crashed.  Use TRY_CATCH wrappers that
  return the pointer.

- find_m3_function_start_sal discarded the result of SKIP_PROLOGUE and,
  unlike find_function_start_sal, did not advance to the next line when
  left in mid-line.  With current gcc the breakpoint then sat before
  the arguments were stored, and the arguments showed garbage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
struct block's language_specific was a union of cplus_specific.namespace
and m3_specific.body_block.  m3-lang.c sets body_block on Modula-3
blocks, which C/C++ lookups (block_scope, used whenever the current
language is C or C++, e.g. in code from the C backend) then read as a
struct block_namespace_info *, crashing gdb.

Make it a struct so the fields don't overlap, declare m3_specific
unconditionally so all translation units agree on the layout, and
initialize it in allocate_block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
print_frame_args used the current language, so a backtrace that mixes
C/C++ frames (e.g. from the C backend) with Modula-3 frames (the
runtime) printed some arguments with the wrong language, e.g.
"Invalid C/C++ type code 29 in symbol table".  Temporarily switch to
the function's language while printing its arguments, as later gdb
versions do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Describe the -no-pie requirement, what to expect with the GCC and C
backends, and how to get DWARF 2 from the C compiler for the C
backend.  Fix the language name ("set language Modula-3"; "m3" is
not accepted).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
m3_ascertain_compiler_kind reported what it detected with error(),
which throws: the command that first triggered detection was aborted,
and in the "Can't detect compiler kind" case the result was assigned
after the error() and so never cached.  Detection then re-ran and threw
on every call, which made e.g. backtraces stop silently after the first
frame in programs without Modula-3 markers (plain C, or a statically
linked C-backend program).  Print the messages instead and cache the
result before returning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
DW_CFA_remember_state saved only the register rules, not the CFA rule
(which DWARF 3 includes in the saved state).  gcc and glibc use
remember_state / epilogue / restore_state in most functions with an
early return; after restore_state the CFA kept the epilogue's value, so
unwinding from such a function failed.  In particular there was no
backtrace out of abort(), i.e. for any Modula-3 runtime error, nor from
threads blocked in glibc.  Save and restore the CFA rule too, as later
gdb versions do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The set printer iterated TYPE_LENGTH / sizeof (long) words, so a set
smaller than a word (e.g. SET OF a 7-element enumeration, 1 byte)
printed as {}.  Round the word count up and extract only the bits
present in a partial last word.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
m3_decode_linespec required the whole rest of the line to consist of
identifiers, dots and blanks, so "break Main.P if n = 2" was not
recognized as a Modula-3 location; it then fell through to the C++
compound-name code and failed.  Pass it only the text up to an "if"
or "thread" keyword and leave the rest to the breakpoint command.

Likewise, the fallback expression-based path in decode_line_1 parsed
and consumed the rest of the line; restrict it to the first token.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Breakpoint conditions were parsed in the current language.  Before the
program runs that is C, so "break Main.P if n = 2" parsed "n = 2" as
a C assignment (failing with "Invalid cast" when evaluated, but in
general it could modify the variable).  Parse the condition in the
source language of the breakpoint location when the language is
"auto", as later gdb versions do.

Also print expressions in the language they were parsed in, so that
"info breakpoints" shows a Modula-3 condition correctly while the
current language is C.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The pthread runtime uses SIGRTMAX (SIG64 on Linux) to suspend threads
for the garbage collector.  m3gdb sets "handle SIG64 nostop noprint
pass" when libm3core.so is loaded, which never happens when the runtime
is linked into the executable (build_standalone), so such programs
stopped on every GC.  Also set it when the inferior is created and the
executable itself contains the pthread runtime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The "auto" demangling style also tries the g++ 2.x scheme, which
misreads Modula-3 names produced by the C backend: Main__Fib parses as
"Main(int, bool)", so procedure Fib was shown as Main in backtraces.
Nothing has emitted the 2.x scheme since gcc 3.0; default to gnu-v3.
"set demangle-style auto" restores the old behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@VictorMiasnikov

VictorMiasnikov commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Does i386_Linux really have problem? Or it is timeouts, etc?

@hosking hosking left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Mika. This is a careful series, and I'm happy for it to go in. I went through every commit. These are the ones I checked most closely:

  • Restoring the CFA on DW_CFA_restore_state matches the DWARF spec (the CFA rule is part of the remembered state), and later gdb versions fixed it the same way.
  • Changing block.h from a union to a struct is safe: both places that allocate a block initialise body_block (allocate_block explicitly, mdebugread through xzalloc).
  • Skipping DWARF units by version accepts exactly what partial_read_comp_unit_head already does (2 and 4), so nothing that used to be read is skipped. It also handles 64-bit initial lengths.
  • The TRY_CATCH wrappers and the bounded M3 linespec fit how break_command_1 takes the if condition, and the language switches are restored through cleanups, including on error.
  • Set printing: besides rounding up, it now steps by the target's word size instead of the host's sizeof (long).

Two small, optional points:

  1. Now that the "Detected compiler kind …" messages are printed with printf_filtered rather than raised with error(), every session will print one. They might be better under info_verbose, or dropped.
  2. The unconditional typedef psaddr_t paddr_t; in gdb_proc_service.h could clash with an older libc that still defines paddr_t. It's fine on anything current.

@VictorMiasnikov, about I386_LINUX: those two failures are from the CI run on 13 November, in the CMake bootstrap step. That was a CI toolchain problem, since fixed on master by #1275 and #1283. It has nothing to do with this PR. CI doesn't build m3gdb at all, so the testing described above is the evidence for this change.

@hosking
hosking merged commit 7809d28 into modula3:master Nov 22, 2026
11 checks passed
@VictorMiasnikov

Copy link
Copy Markdown
Contributor

} about I386_LINUX: those two failures are from the CI run on 13 November, in the CMake bootstrap step.

Yes: I see

@hosking

hosking commented Nov 23, 2026

Copy link
Copy Markdown
Member

Mika, a question about m3gdb, since you brought it back to life in #1250. We're planning how debugging will work once MSIR becomes the only lowering (it produces C or LLVM IR, so the GCC backend and its stabs eventually go). The plan is to rely on stock lldb and current gdb, reading the DWARF that MSIR already emits: records, arrays, open-array dopes, enumerations and objects with their inherited fields. Modula-3-specific printing (TEXT, open arrays, an object's dynamic type, REFANY) would come from lldb data formatters and gdb Python pretty-printers shipped with cm3. m3gdb itself would stay as it is for GCC-backend builds.

What stock debuggers won't have is m3gdb's Modula-3 expression evaluator. Which m3gdb features do you actually rely on? If there's something beyond printing values, such as M3 expressions in conditions or calling procedures from the debugger, we'd like to know before we decide what the scripts need to cover.

🤖 Generated with Claude Code

@hosking

hosking commented Nov 23, 2026

Copy link
Copy Markdown
Member

One refinement to the plan above: rather than writing the Modula-3 printing in Python, we'd write it in Modula-3, in the runtime, in the spirit of Ramsey and Hanson's ldb. The runtime already knows every traced object's type (typecode, typecell, and the full description RTTipe gives pickles), so a formatter in m3core can print any reference completely. lldb and gdb would call it through thin shims in the stopped process, so one formatter serves both debuggers. Plain DWARF printing remains for core files.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants