diff --git a/share/btrace/uprofile.bt b/share/btrace/uprofile.bt index 17ce622ee96..5978de8054e 100644 --- a/share/btrace/uprofile.bt +++ b/share/btrace/uprofile.bt @@ -1,4 +1,4 @@ -/* $OpenBSD: uprofile.bt,v 1.1 2023/05/12 14:34:00 claudio Exp $ */ +/* $OpenBSD: uprofile.bt,v 1.2 2025/10/05 22:31:54 sashan Exp $ */ /* * uprofile.bt Userland profiling (stack sampling) at 100Hz. @@ -9,8 +9,13 @@ * * The PID of the to be monitored process is the first optional argument to * btrace(8). - * This only works for a single static binary where everything was compiled - * with -fno-omit-frame-pointer. + * + * To collect profiling data for process with pid 12345 one uses uprofile.bt + * script as follows: + * btrace /usr/share/btrace/uprofile.bt 12345 + * after hitting Ctrl+C the btrace process terminates and writes collected + * profiling data to standard output. This only works for ELF objects which + * are compiled with -fno-omit-frame-pointer. */ profile:hz:100 / pid == $1 / { @[ustack] = count(); diff --git a/share/man/man4/dt.4 b/share/man/man4/dt.4 index b7a2814f117..937056e74e4 100644 --- a/share/man/man4/dt.4 +++ b/share/man/man4/dt.4 @@ -1,4 +1,4 @@ -.\" $OpenBSD: dt.4,v 1.7 2020/12/02 07:00:42 anton Exp $ +.\" $OpenBSD: dt.4,v 1.8 2025/10/05 22:31:54 sashan Exp $ .\" .\" Copyright (c) 2019 Martin Pieuchot .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: December 2 2020 $ +.Dd $Mdocdate: October 5 2025 $ .Dt DT 4 .Os .Sh NAME @@ -77,6 +77,58 @@ struct dtioc_req { uint64_t dtrq_evtflags; }; .Ed +.It Dv DTIOCRDVNODE Fa "struct *dtioc_rdvn" +Resolve the instruction pointer from the traced process to the ELF object. +.Bd -literal +struct dtioc_rdvn { + pid_t dtrv_pid; + int dtrv_fd; + caddr_t dtrv_va; + caddr_t dtrv_offset; + caddr_t dtrv_start; + size_t dtrv_len; +}; +.Ed +.Pp +The caller populates +.Fa dtrv_pid +with the process ID of the traced process and +.Fa dtrv_va +with the program counter address to look up. +.Pp +On success, the remaining members are populated. +.Fa dtrv_fd +is the file descriptor to the DSO file that +.Fa dtrv_va +belongs to. +.FA dtrv_len +is the length of the DSO file. +.Fa dtrv_offset +and +.Fa dtrv_start +are the relative offset of +.Fa dtrv_va +to the start of the text segment and the base address of that segment +respectively. +.Pp +To calculate the relative offset of +.Fa dtrv_va +in the DSO the following calculation can be used: +.Pp +.Dl iOffset = dtrv_va - dtrv_start + dtrv_offset +.Pp +The segment can be found by walking the ELF program headers and looking +for the mapping where iOffset is included in the range +[GElf_Phdr.p_offset, GElf_Phdr.p_offset + GElf_Phdr.p_filesz) +.Pp +Using the +.Vt GElf_Phdr.p_vaddr +the base address of the segment can be calculated by +.Dl lbase = dtrv_start - GElf_Phdr.p_vaddr +.Pp +For symbol lookups the offset can now be calculated as +.Dl symOffset = dtrv_va - lbase +.El .El .Sh FILES .Bl -tag -width /dev/dt -compact diff --git a/usr.sbin/btrace/btrace.8 b/usr.sbin/btrace/btrace.8 index 6d5172b82c9..4ac55ca7824 100644 --- a/usr.sbin/btrace/btrace.8 +++ b/usr.sbin/btrace/btrace.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: btrace.8,v 1.10 2025/05/18 20:09:58 schwarze Exp $ +.\" $OpenBSD: btrace.8,v 1.11 2025/10/05 22:31:54 sashan Exp $ .\" .\" Copyright (c) 2019 Martin Pieuchot .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: May 18 2025 $ +.Dd $Mdocdate: October 5 2025 $ .Dt BTRACE 8 .Os .Sh NAME @@ -23,7 +23,6 @@ .Sh SYNOPSIS .Nm btrace .Op Fl lnv -.Op Fl p Ar elffile .Ar programfile | Fl e Ar program .Op Ar argument ... .Sh DESCRIPTION @@ -56,12 +55,6 @@ List all available probes. .It Fl n No action. Parse the program and then exit. -.It Fl p Ar elffile -Load symbols from the -.Ar elffile -to convert -.Va ustack -addresses into function names. .It Fl v Verbose mode. Causes diff --git a/usr.sbin/btrace/btrace.c b/usr.sbin/btrace/btrace.c index dcc7237865c..2a95de74ef2 100644 --- a/usr.sbin/btrace/btrace.c +++ b/usr.sbin/btrace/btrace.c @@ -1,4 +1,4 @@ -/* $OpenBSD: btrace.c,v 1.98 2025/09/22 07:49:43 sashan Exp $ */ +/* $OpenBSD: btrace.c,v 1.99 2025/10/05 22:31:54 sashan Exp $ */ /* * Copyright (c) 2019 - 2023 Martin Pieuchot @@ -230,7 +230,7 @@ main(int argc, char *argv[]) __dead void usage(void) { - fprintf(stderr, "usage: %s [-lnv] [-p elffile] " + fprintf(stderr, "usage: %s [-lnv] " "programfile | -e program [argument ...]\n", getprogname()); exit(1); }