Skip to content

os: add debugger_present implementation for OpenBSD - #24490

Merged
spytheman merged 1 commit into
vlang:masterfrom
lcheylus:openbsd-debugger
May 15, 2025
Merged

spytheman merged 1 commit into
vlang:masterfrom
lcheylus:openbsd-debugger

Conversation

@lcheylus

@lcheylus lcheylus commented May 15, 2025 •

Copy link
Copy Markdown
Contributor

Check if a child process could trace its parent process, if not a debugger must be present.

On OpenBSD, impossible to trace a parent process from its child. But with errors (errno), it's possible to determine if a debugger is present or not.
Possible errors for ptrace / PT_ATTACH in child process:

  • EBUSY: process already traced
  • EPERM: process is not a child of tracer (sysctl kern.global_ptrace=0)
  • EINVAL: process is an ancestor of the current process and not init (sysctl kern.global_ptrace=1)

Closes #23603


Tests OK on OpenBSD current/adm64

Code debugger_present.v to test if debugger is present or not

import os

fn main() {
        println('Call 1:')
        if os.debugger_present() {
                println('debugger here')
        } else {
                println('no debugger')
        }

        println('Call 2:')
        if os.debugger_present() {
                println('debugger here')
        } else {
                println('no debugger')
        }
}
  • Tests WITHOUT debugger:
$ ~/tmp/debugger_present
Call 1:
no debugger
Call 2:
no debugger
  • Test WITH debugger (gdb)
$ gdb ~/tmp/debugger_present
(...)
(gdb) run
Starting program: /home/fox/tmp/debugger_present 
Call 1:
[New process 84238]
debugger here
Call 2:
debugger here

Program exited normally.
(gdb) quit

Check if a child process could trace its parent process, if not a
debugger must be present.

On OpenBSD, impossible to trace a parent process from its child. But
with errors (errno), it's possible to determine if a debugger is present
or not.
Possible errors for ptrace / PT_ATTACH in child process:
  - EBUSY: process already traced
  - EPERM: process is not a child of tracer (sysctl kern.global_ptrace=0)
  - EINVAL: process is an ancestor of the current process and not
    init (sysctl kern.global_ptrace=1)

Signed-off-by: Laurent Cheylus <foxy@free.fr>
@huly-for-github

Copy link
Copy Markdown

Connected to Huly®: V_0.6-22859

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Excellent work.

@spytheman
spytheman merged commit 478607d into vlang:master May 15, 2025
@lcheylus
lcheylus deleted the openbsd-debugger branch May 15, 2025 10:15
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.

Implementation of debugger_present on OpenBSD

2 participants