Skip to content

Add line number breakpoints to ocamldebug#14350

Closed
joelreymont wants to merge 1 commit intoocaml:trunkfrom
joelreymont:ocamldebug-line-break
Closed

Add line number breakpoints to ocamldebug#14350
joelreymont wants to merge 1 commit intoocaml:trunkfrom
joelreymont:ocamldebug-line-break

Conversation

@joelreymont
Copy link
Copy Markdown

@joelreymont joelreymont commented Nov 12, 2025

Implements line number breakpoint support for ocamldebug, allowing breakpoints to be set at function entry points using line numbers.

Features:

  • New Module:line syntax (e.g., "break Foo:42")

Testing:

  • Automated tests in testsuite/tests/tool-debugger/line-breakpoints/

Implements line number breakpoint support for ocamldebug, allowing
breakpoints to be set at function entry points using line numbers.

Features:
- Inclusive event lookup that includes Event_pseudo function entries
- New Module:line syntax (e.g., "break Foo:42")
- Maintains backward compatibility with existing @ syntax
- Test suite integrated into OCaml's testsuite

Implementation:
- New event_at_pos_inclusive and event_near_pos_inclusive functions
- Updated breakpoint command handlers to use inclusive lookups
- Added Module:line parser rule in debugger_parser.mly
- Uses existing all_events_by_module hashtable (converted to array as needed)

Testing:
- Automated tests in testsuite/tests/tool-debugger/line-breakpoints/
- Tests use TEST_BELOW format for line number stability
@joelreymont joelreymont force-pushed the ocamldebug-line-break branch from 508e76d to 92dd495 Compare November 12, 2025 10:16
@joelreymont
Copy link
Copy Markdown
Author

Assuming

❯ cat manual_test.ml
(* Simple test program for line breakpoints *)

let add x y =           (* Line 3 *)
  x + y                 (* Line 4 *)

let factorial n =       (* Line 6 *)
  let rec loop acc n =  (* Line 7 *)
    if n <= 1 then      (* Line 8 *)
      acc               (* Line 9 *)
    else                (* Line 10 *)
      loop (acc * n) (n - 1)  (* Line 11 *)
  in
  loop 1 n              (* Line 13 *)

let fibonacci n =       (* Line 15 *)
  let rec fib a b n =   (* Line 16 *)
    if n = 0 then a     (* Line 17 *)
    else fib b (a + b) (n - 1)  (* Line 18 *)
  in
  fib 0 1 n             (* Line 20 *)

let () =
  Printf.printf "add 3 5 = %d\n" (add 3 5);
  Printf.printf "factorial 5 = %d\n" (factorial 5);
  Printf.printf "fibonacci 10 = %d\n" (fibonacci 10);
  ()

You can then do

❯ ./runtime/ocamlrun ./ocamlc -g -I stdlib -use-runtime ./runtime/ocamlrun manual_test.ml -o manual_test
❯ CAML_LD_LIBRARY_PATH=otherlibs/unix:otherlibs/str:otherlibs/systhreads \
  ./runtime/ocamlrun ./debugger/ocamldebug -I stdlib -I otherlibs/unix manual_test
        OCaml Debugger version 5.5.0+dev0-2025-04-28

(ocd) break Manual_test:3
Loading program... done.
Breakpoint 1 at 3:188756: file manual_test.ml, line 4, characters 3-8
(ocd) break Manual_test:6
Breakpoint 2 at 3:188708: file manual_test.ml, line 7, characters 3-209
(ocd) break Manual_test:15
Breakpoint 3 at 3:188592: file manual_test.ml, line 16, characters 3-139
(ocd) info breakpoints
Num    Address  Where
  1 3:    188756  file manual_test.ml, line 4, characters 3-8
  2 3:    188708  file manual_test.ml, line 7, characters 3-209
  3 3:    188592  file manual_test.ml, line 16, characters 3-139
(ocd) run
Time: 18 - pc: 3:188756 - module Manual_test
Breakpoint: 1
4   <|b|>x + y                 (* Line 4 *)

@lthls
Copy link
Copy Markdown
Contributor

lthls commented Nov 12, 2025

The AI-generated summary of this PR is mostly useless. Could you clarify what this PR was meant to achieve, beyond allowing one to write break Module:linenum instead of break @ Module linenum ?

@joelreymont
Copy link
Copy Markdown
Author

I couldn't find the option to break @ Module linenum so I thought the functionality was missing.

Is this a facepalm moment?

@lthls
Copy link
Copy Markdown
Contributor

lthls commented Nov 12, 2025

Well, the code itself looks like you needed some kind of support for stopping at function entry points; it would be nice to know if that is an issue (that your PR fixes) or not.

@joelreymont
Copy link
Copy Markdown
Author

joelreymont commented Nov 12, 2025

That's an unintended side effect, I didn't mean that specifically.

@joelreymont
Copy link
Copy Markdown
Author

Looks like this PR is useless in the end since stopping at line numbers is already implemented.

Should I narrow it down to, for example, stopping at function entry points if the compiler has emitted that information?

@gasche
Copy link
Copy Markdown
Member

gasche commented Nov 12, 2025

Do you have an example where doing this would improve on the user-observable behavior?

@joelreymont
Copy link
Copy Markdown
Author

Do you have an example where doing this would improve on the user-observable behavior?

I do not if you are asking me.

@nojb
Copy link
Copy Markdown
Contributor

nojb commented Nov 12, 2025

There doesn't seem to be anything new in this PR that doesn't exist already. So closing. Thanks.

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.

6 participants