OpenBSD Journal

New profiling subsystem committed to -current

Contributed by Peter N. M. Hansteen on from the make that profile a skyline dept.

A new profiling subsystem is now in OpenBSD-current, from the hands of none other than Theo de Raadt (deraadt@) himself.

A longish sequence of commits introduced the changes incrementally, with a summary as follows:

List:       openbsd-cvs
Subject:    CVS: cvs.openbsd.org: src
From:       Theo de Raadt <deraadt () cvs ! openbsd ! org>
Date:       2025-05-24 6:49:17

CVSROOT:	/cvs
Module name:	src
Changes by:	deraadt@cvs.openbsd.org	2025/05/24 00:49:17

Modified files:
	include        : unistd.h 
	sys/sys        : exec.h exec_elf.h gmon.h proc.h systm.h 
	sys/kern       : exec_elf.c init_sysent.c kern_exec.c 
	                 kern_exit.c kern_fork.c kern_pledge.c 
	                 subr_prof.c syscalls.master 

Log message:
In the old gprof profiling subsystem, the simplistic profil() syscall
told the kernel about the sample buffer, and then the normal exit-time
_mcleanup() would finalize the buffer, open()'ed a file and write out
the details.  This file opening has become increasingly impossible
because of our privsep / privdrop, chroot, setresuid uid-dropping,
pledge, unveil, and other efforts.  So people stopped using gprof.
Programs which needed profiling needed substantial mitigation removal
changes to put them under test.
In the new gprof profiling subsystem, profil() is enhanced to provide more
buffer information to the kernel, as well as better instructions for where
the file should be written.  At normal exit-time mcleanup(), after finalizing
the buffer, the process simply terminates via _exit(2), and the kernel
does all the opening and writing in a safe way.  The file is now deposited
into the starting directory, or into PROFDIR as determined at program start,
with safety issues taken into consideration.

Additional side effecs:
- The monstartup(3) interface will go away, it cannot be supported.
- profil() is now allowed in pledge "stdio", access is decided by the -pg ELF note
- gmon.out is renamed to gmon.progname.pid.out, due to pervasive multi-process
privsep practices

prompted originally by job and claudio for use in rpki-client and bgpd
advice from kettenis and claudio

There it is, profiling tailored to work well with some of the core code sanity features originated by the developers of our favorite operating system. As always, the code should be available for test in fresh snapshots, and feedback from real life testing is appreciated.


Credits

Copyright © - Daniel Hartmeier. All rights reserved. Articles and comments are copyright their respective authors, submission implies license to publish on this web site. Contents of the archive prior to as well as images and HTML templates were copied from the fabulous original deadly.org with Jose's and Jim's kind permission. This journal runs as CGI with httpd(8) on OpenBSD, the source code is BSD licensed. undeadly \Un*dead"ly\, a. Not subject to death; immortal. [Obs.]