Article View: muc.lists.freebsd.current
Article #22LLVM "continuous" coverage profiling hack^Wpatch
From: Poul-Henning Kamp
Date: Mon, 12 Oct 2026 19:16
Date: Mon, 12 Oct 2026 19:16
172 lines
8710 bytes
8710 bytes
------- =_aaaaaaaaaa0
Content-Type: text/plain; charset="us-ascii"
Content-ID: <29609.1791832574.1@critter.freebsd.dk>
INTRO:
======
Coverage profiling is where the compiler adds counters in the code so
one can tell which bits got executed how many times.
People should use it much more than they do.
Normally, the counters are written to a file by an atexit(3) handler
but that goes haywire in pretty much any unplain program.
The solution is to mmap(2) the memory for the counters, so that no
matter how the process expires, the counts are recorded for later
analysis.
For this to work, the counters must be page aligned in the ELF file.
CURRENT STATE OF AFFAIRS:
=========================
Somebody has hacked support for mmap(2) counters into LLVM on Apple
platforms, and they use an Apple-only lld(1) argument ("-sectalign")
to tell the loader to align the relevant ELF-sections.
They also added a comment to the effect that
"Support for Linux may be mostly complete but requires
testing, and support for Windows may require more extensive
changes: please get involved if you are interested in porting
this feature."
And seem to have left it there.
THE PATCH:
==========
I dont know what the proper way to implement this in FreeBSD is,
but the attached hack^Wpatch makes it work for me.
It uses the same code-path as Apple platforms for the mmap(2)'ery
and hardcodes the ELF segment names into lld.
The patch should be a no-op unless profiling is enabled.
I dont know if this patch is suitable as a local patch for FreeBSD
or if there is any reason to attempt to upstream it, and I dont
have the time spend to find out. If somebody wants to pick it
up, be my guest.
HOWTO:
======
Apply patch, recompile clang and lld. (or world).
Compile your code with
-fprofile-instr-generate -fcoverage-mapping
Create a directory for the count files:
rm -rf /tmp/GC
mkdir -p /tmp/GC
Set the environment var to point to that directory:
export LLVM_PROFILE_FILE=/tmp/GC/%p%c.profraw
The '%c' is the magic bit which enables "continous" aka mmap(2) mode.
Now run the program(s) you want tested.
./a.out
Then merge all the count files:
llvm-profdata merge -sparse /tmp/GC/* -o /tmp/gc.profdata
The data can be visualized with:
llvm-cov show ./a.out -instr-profile=/tmp/gc.profdata
Enjoy,
Poul-Henning
--
Poul-Henning Kamp | UNIX since Zilog Zeus 3.20
phk@FreeBSD.ORG | TCP/IP since RFC 956
FreeBSD committer | BSD since 4.3-tahoe
Never attribute to malice what can adequately be explained by incompetence.
------- =_aaaaaaaaaa0
Content-Type: text/x-diff; charset="us-ascii";
name="llvm_continous_coverage.patch"
Content-Description: llvm_continous_coverage.patch
Content-Disposition: attachment; filename="llvm_continous_coverage.patch"
Content-Transfer-Encoding: quoted-printable
diff --git a/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingBu=
ffer.c b/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingBuffer=
.c
index b406e8db74f3..1a18ab9fff0f 100644
--- a/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingBuffer.c
+++ b/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingBuffer.c
@@ -137,7 +137,7 @@ static uint64_t calculateBytesNeededToPageAlign(uint64=
_t Offset) {
}
=
static int needsCounterPadding(void) {
-#if defined(__APPLE__)
+#if defined(__APPLE__) || defined(__FreeBSD__)
return __llvm_profile_is_continuous_mode_enabled();
#else
return 0;
diff --git a/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingFi=
le.c b/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingFile.c
index 354f21b78615..34f4271b489b 100644
--- a/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingFile.c
+++ b/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingFile.c
@@ -96,7 +96,7 @@ static int ProfileMergeRequested =3D 0;
static int getProfileFileSizeForMerging(FILE *ProfileFile,
uint64_t *ProfileFileSize);
=
-#if defined(__APPLE__)
+#if defined(__APPLE__) || defined(__FreeBSD__)
static const int ContinuousModeSupported =3D 1;
static const int UseBiasVar =3D 0;
static const char *FileOpenMode =3D "a+b";
diff --git a/contrib/llvm-project/lld/ELF/OutputSections.cpp b/contrib/llv=
m-project/lld/ELF/OutputSections.cpp
index 1020dd9f2569..bd386786f11a 100644
--- a/contrib/llvm-project/lld/ELF/OutputSections.cpp
+++ b/contrib/llvm-project/lld/ELF/OutputSections.cpp
@@ -71,8 +71,19 @@ void OutputSection::writeHeaderTo(typename ELFT::Shdr *=
shdr) {
OutputSection::OutputSection(Ctx &ctx, StringRef name, uint32_t type,
uint64_t flags)
: SectionBase(Output, ctx.internalFile, name, type, flags, /*link=3D*=
/0,
- /*info=3D*/0, /*addralign=3D*/1, /*entsize=3D*/0),
- ctx(ctx) {}
+ /*info=3D*/0, /*addralign=3D*/1, /*entsize=3D*/0), // H=
ERE PHK
+ ctx(ctx) {
+
+ // "Continous" coverage profiling require these sections to be
+ // page aligned so they can be mmap(2)'ed.
+ if (name =3D=3D "__llvm_prf_cnts") {
+ addralign =3D ctx.arg.maxPageSize;
+ } else if (name =3D=3D "__llvm_prf_data") {
+ addralign =3D ctx.arg.maxPageSize;
+ } else if (name =3D=3D "__llvm_prf_vnds") {
+ addralign =3D ctx.arg.maxPageSize;
+ }
+}
=
uint64_t OutputSection::getLMA() const {
return ptLoad ? addr + ptLoad->lmaOffset : addr;
------- =_aaaaaaaaaa0--
--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Technical Details
Message-ID:
<6acd31fe.28cf9.7bd07c28@critter.freebsd.dk>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!3.eu.feeder.erje.net!feeder.erje.net!news.szaf.org!news.karotte.org!news.space.net!news.muc.de!.POSTED.news.muc.de!not-for-mailShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!3.eu.feeder.erje.net!feeder.erje.net!news.szaf.org!news.karotte.org!news.space.net!news.muc.de!.POSTED.news.muc.de!not-for-mail From: Poul-Henning Kamp <phk@phk.freebsd.dk> Newsgroups: muc.lists.freebsd.current Subject: LLVM "continuous" coverage profiling hack^Wpatch Date: Mon, 12 Oct 2026 19:16:14 +0000 Organization: Newsgate at muc.de e.V. Sender: owner-freebsd-current@FreeBSD.org Approved: news-admin@muc.de Distribution: world Message-ID: <6acd31fe.28cf9.7bd07c28@critter.freebsd.dk> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Injection-Info: news.muc.de; posting-host="news.muc.de:193.149.48.2"; logging-data="99240"; mail-complaints-to="news-admin@muc.de" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1791832600; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:list-id:list-help:list-owner: list-unsubscribe:list-subscribe:list-post; bh=y7NH8qKh5CA1R+xCvkPHPG9mqK+r8nbYyzlMwbxF34c=; b=KYvd4xmmmTrRf4W0KEcCC/ugYJySLtxoaSUwcDAbZfVdOOVjORcmTEByZrVRRfzdK2rIBe 9ML9ZvF1NhrsLAKAKQThikpkEnopKBapqLCWHpSndytEcRXDsu6RXn1OJxFus29jJ9ZI48 ZM5/ObRsuBN78P0EC+eIyb0oq43yufn6LNbF1f7rr5CM9iawdQXM2zjkiKjR9Yy1o2e7hz +LnHSDxWYYYKnTHwzgUAZlBOB+PcyAxCUsiWFEZSE6z2zBhhhK7iRgrEF3TirrbahGutz0 4BHUaxyglqkcDE962c3XUFUmZqFYyp8C6IBbtxOf+FXUkxZCd2FCuBUkuPRttw== X-Original-To: current@mlmmj.nyi.freebsd.org Authentication-Results: mx1.freebsd.org; dkim=none; spf=pass (mx1.freebsd.org: domain of phk@critter.freebsd.dk designates 130.225.244.222 as permitted sender) smtp.mailfrom=phk@critter.freebsd.dk; dmarc=none Precedence: list Content-ID: <29609.1791832574.0@critter.freebsd.dk> X-Spamd-Bar: / X-Spamd-Result: default: False [0.50 / 15.00]; HFILTER_FROMHOST_NORESOLVE_MX(0.50)[s2gw.ddhf.dk]; FORGED_SENDER(0.30)[phk@phk.freebsd.dk,phk@critter.freebsd.dk]; R_SPF_ALLOW(-0.20)[+mx]; MIME_GOOD(-0.10)[multipart/mixed,text/plain,text/x-diff]; R_DKIM_NA(0.00)[]; ARC_NA(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; FREEFALL_USER(0.00)[phk]; ASN(0.00)[asn:1835, ipnet:130.225.0.0/16, country:EU]; MISSING_XM_UA(0.00)[]; MIME_TRACE(0.00)[0:+,1:+,2:+]; MLMMJ_DEST(0.00)[current@freebsd.org]; MID_RHS_MATCH_FROMTLD(0.00)[]; RCVD_TLS_LAST(0.00)[]; FROM_NEQ_ENVFROM(0.00)[phk@phk.freebsd.dk,phk@critter.freebsd.dk]; FROM_HAS_DN(0.00)[]; DMARC_NA(0.00)[freebsd.dk]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[current@freebsd.org]; HAS_ATTACHMENT(0.00)[] X-Rspamd-Queue-Id: 4j3Rym6g95z4l3R X-Newsgate-Id: f02c70b8849f+ X-No-Archive: Yes