🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: muc.lists.freebsd.current
5 messages
5 total messages Started by Poul-Henning Kamp Mon, 12 Oct 2026 19:16
LLVM "continuous" coverage profiling hack^Wpatch
#22
Author: Poul-Henning Kamp
Date: Mon, 12 Oct 2026 19:16
172 lines
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
Re: LLVM "continuous" coverage profiling hack^Wpatch
#28
Author: Adrian Chadd
Date: Mon, 12 Oct 2026 19:34
44 lines
6499 bytes
--000000000000bf4f0e065da9ca0e
Content-Type: text/plain; charset="UTF-8"

On Mon, 12 Oct 2026 at 12:16, Poul-Henning Kamp <phk@phk.freebsd.dk> wrote:

> INTRO:
> ======
>
>    Coverage profiling is where the compiler adds counters in the code so
>    one can tell which bits got executed how many times.
>
>
>
oh this is neat! I wonder how we should run this up the flagpole at LLVM
land?


-a

--000000000000bf4f0e065da9ca0e
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><br></div><br><div class=3D"gmail_quote g=
mail_quote_container"><div dir=3D"ltr" class=3D"gmail_attr">On Mon, 12 Oct =
2026 at 12:16, Poul-Henning Kamp &lt;<a href=3D"mailto:phk@phk.freebsd.dk">=
phk@phk.freebsd.dk</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote=
" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);=
padding-left:1ex">INTRO:<br>
=3D=3D=3D=3D=3D=3D<br>
<br>
=C2=A0 =C2=A0Coverage profiling is where the compiler adds counters in the =
code so<br>
=C2=A0 =C2=A0one can tell which bits got executed how many times.<br>
<br><br></blockquote><div><br></div><div>oh this is neat! I wonder how we s=
hould run this up the flagpole at LLVM land?</div><div><br></div><div><br><=
/div><div>-a</div><div>=C2=A0</div></div></div>

--000000000000bf4f0e065da9ca0e--


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Re: LLVM "continuous" coverage profiling hack^Wpatch
#19
Author: Poul-Henning Kamp
Date: Mon, 12 Oct 2026 19:45
31 lines
3947 bytes
--------
Adrian Chadd writes:

> On Mon, 12 Oct 2026 at 12:16, Poul-Henning Kamp <phk@phk.freebsd.dk> wrote:
>
> > INTRO:
> > ======
> >
> >    Coverage profiling is where the compiler adds counters in the code so
> >    one can tell which bits got executed how many times.
> >
> >
> >
> oh this is neat! I wonder how we should run this up the flagpole at LLVM
> land?

By all means!

Maybe point out to them that the mmap(2) would work on any OS with
a working mmap(2) ?

-- 
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.


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Re: LLVM "continuous" coverage profiling hack^Wpatch
#53
Author: Enji Cooper (yaneurabeya)
Date: Mon, 19 Oct 2026 03:29
75 lines
8367 bytes
--Apple-Mail=_BE07147F-C167-4C3F-BE9E-A4FDD1733184
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8


> On Oct 12, 2026, at 12:45=E2=80=AFPM, Poul-Henning Kamp =
<phk@phk.freebsd.dk> wrote:
>=20
> --------
> Adrian Chadd writes:
>=20
>> On Mon, 12 Oct 2026 at 12:16, Poul-Henning Kamp <phk@phk.freebsd.dk> =
wrote:
>>=20
>>> INTRO:
>>> =3D=3D=3D=3D=3D=3D
>>>=20
>>>   Coverage profiling is where the compiler adds counters in the code =
so
>>>   one can tell which bits got executed how many times.
>>>=20
>>>=20
>>>=20
>> oh this is neat! I wonder how we should run this up the flagpole at =
LLVM
>> land?
>=20
> By all means!
>=20
> Maybe point out to them that the mmap(2) would work on any OS with
> a working mmap(2) ?

I=E2=80=99m a bit puzzled how this is different from the already =
documented/implemented --coverage option in compiler_rt, when combined =
with gcov and lcov. compiler_rt support has been working for almost a =
decade now on FreeBSD =E2=80=94 it=E2=80=99s just not integrated into =
the build. I tried doing it back in 2017 and ran into brick walls around =
generated objects, naming schemes, etc. I could dig up the work if =
requested.
Thanks,
-Enji=

--Apple-Mail=_BE07147F-C167-4C3F-BE9E-A4FDD1733184
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename=signature.asc
Content-Type: application/pgp-signature;
	name=signature.asc
Content-Description: Message signed with OpenPGP

-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEEkHfexGRJ3gYRdA2gGpE5DjPsNJgFAmrVjokACgkQGpE5DjPs
NJi//hAAhQCl8Q66HlZ6r4x6izFwPt0UtRb730C50/0a31A5lZpMm8GpM77KDa14
Y9RBfaHwh6Bv9BHxHNHQsA8sP3NuXr08xehyDCaAl5gzWRLSgiIajxhiizyHUW7u
8BZuwkNH9TG5X18nX+ObjbB6xWL9xuZj4bv3K7mtRLYyFkZwpaZJeoi543K2hNZp
XkWiBN3zC7qNyae89DbHVVSytU/c/wCujtHgMpW/4CI6nYmLaUtOIGu3MVB8YzIy
wiwwBdwgrH5dHiX1+XTCi1vALy7h2unBu8jVxPGpEL5iGBId+Y5bDAgHb9gcvgwm
V3h0teiah/n/kg4C3gKGoexrVt6z+ZrOCR4RNfm/lRXarIyx+QPRIx712+LQF5Sh
JtCtgjI2GCabKl6vG/fCl37V1bDmIrvtXFzsWznk1x4H7F8CELJWNJjqtHo2MKeb
mnie/Nzsi0jqWMakBOQp3TAIJdFmW126ZMPgBew/n5UAq696Iq9cxpHfJOcXexQ9
wuvRQfXHXawFVkLpRpHu24q33uo7KDpUOAilWfI5EIY3rOOisXLZcJoRlE96xiaK
YDm2eiVNbkO1H+Q7FE1K468W+2HxZGRWOXXGMYJYUF+rcYO3zN5kuwjT2cHc8onV
HE86lBKg/+11gxYY1DB1bM4mYGUQ+iUwaZ4hcWL7pHGXXKm7EU0=
=e87u
-----END PGP SIGNATURE-----

--Apple-Mail=_BE07147F-C167-4C3F-BE9E-A4FDD1733184--


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Re: LLVM "continuous" coverage profiling hack^Wpatch
#54
Author: Poul-Henning Kamp
Date: Mon, 19 Oct 2026 06:06
20 lines
3783 bytes
--------
Enji Cooper (yaneurabeya) writes:

> I'm a bit puzzled how this is different from the already 
> documented/implemented --coverage option in compiler_rt, 

It's the exact same code, but the counters are mmap(2)'ed, so they
survive core-dumps, setuid(), chroot(2) and other "weird cases"
where atexit(3) does not do what you expect.

-- 
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.


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads