Page MenuHomeFreeBSD

linuxkpi: Add hex_dump_to_buffer()
ClosedPublic

Authored by dumbbell on Jul 26 2025, 3:53 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sat, Jan 17, 6:12 AM
Unknown Object (File)
Thu, Jan 15, 8:38 AM
Unknown Object (File)
Wed, Jan 14, 8:14 PM
Unknown Object (File)
Wed, Jan 14, 7:44 PM
Unknown Object (File)
Sat, Jan 10, 3:15 PM
Unknown Object (File)
Tue, Jan 6, 10:26 PM
Unknown Object (File)
Mon, Jan 5, 11:06 PM
Unknown Object (File)
Tue, Dec 30, 7:47 AM
Subscribers

Details

Summary

This function prints a single line of hex dump to the given line buffer.

The implementation relies on lkpi_hex_dump() to format the string. After the formatting, it trims the newline character automatically added by lkpi_hex_dump().

Sponsored by: The FreeBSD Foundation

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

It looks like Linux hex_dump_to_buffer does a hex+printable ASCII a la 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 Hello, world, although I don't think it's incredibly important for our use cases.

bz requested changes to this revision.Jul 30 2025, 9:16 AM
bz added a subscriber: bz.

It looks like Linux hex_dump_to_buffer does a hex+printable ASCII a la 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 Hello, world, although I don't think it's incredibly important for our use cases.

Yeah, we do have the ascii flag but it was never implemented (not before my reshuffling either).

sys/compat/linuxkpi/common/src/linux_compat.c
2013

you do not want to return negative I think but 0; otherwise linelen in lkpi_hex_dump may go negative or back to 0; this is likely also a problem with the other callback routines.

2015

this is not correct. written can be > context->linebuflen if the output would have been longer than the buffer size. You need another check here or rather set it accordingly .. hmm also below is wrong to the end of the func.

If I am not mistaken:

if (written <= (context->linebuflen - 1))    /* terminating \0 */
     context->written = written + 1;
else
     context->written = context->linebuflen;

context->linebuf += context->written;
context->linebuflen -= context->written;

return (context->written);

But you should double-check that given vsnprintf returns the length without the trailing \0 and I am not sure if you need to count it or not.

2044

This is not needed, is it? lkpi_hex_dump will only do 8/4/2 or 1 in every other case

2060

should we add another bool to lkpi_hex_dump whether to add the (last) \n or not?

This revision now requires changes to proceed.Jul 30 2025, 9:16 AM
dumbbell added inline comments.
sys/compat/linuxkpi/common/src/linux_compat.c
2013

You are right. I updated D51558 to handle negative return values from the callback.

2015

According to the documentation of hex_dump_to_buffer() on Linux, it has the same behaviour as snprintf(): it returns the number of bytes it would have written if the destination buffer was large enough, excluding the trailing NUL character.

I updated the patch to remove the linebuflen == 0 check at the beginning and added comments to describe that.

Is it clearer or is there still a logic issue?

2044

You are right. I removed that part.

2060

Good idea, I added D51844 to implement that separately.

According to your comments the open TODOs are done. Did you forget to push the updated version of the change?

dumbbell marked 4 inline comments as done.

Address comments from @bz

In D51559#1237814, @bz wrote:

According to your comments the open TODOs are done. Did you forget to push the updated version of the change?

I think you are right. I just updated the patch.

I think it's fine now for as much as I can currently wrap my head around it.

This revision is now accepted and ready to land.Dec 14 2025, 7:35 PM
This revision was automatically updated to reflect the committed changes.