This is the mail archive of the
systemtap@sourceware.org
mailing list for the systemtap project.
Re: Printing binary data in embedded C
- From: Martin Hunt <hunt at redhat dot com>
- To: Perry Cheng <perryche at us dot ibm dot com>
- Cc: systemtap at sources dot redhat dot com
- Date: Tue, 01 Jan 2008 16:09:54 -0500
- Subject: Re: Printing binary data in embedded C
- Organization: Red Hat Inc.
- References: <OFB235EE6D.09ADED41-ON852573B9.001A2026-852573B9.001AA10C@us.ibm.com>
On Fri, 2007-12-21 at 23:50 -0500, Perry Cheng wrote:
> I've been trying to convert some of my existing scripts from printing
> ascii data to binary data to improve probe speed. I've stated using the
> %b modifier via _stp_printf with success. However, my data naturally
> comes in as a char[] of known length and printing it one word at a time
> with %4b doesn't seem like the best way. On the other hand, I can't use a
> bulk function like _stp_print since it interprets the buffer as a C string
> and will misinterpret my binary 0's as a string terminator. Looking at
> the implementation of _stp_print, it's clear that this is an easy feature
> to add.
>
> Is there an existing function that I have missed or is there a simple
> workaround?
Yes. _stp_reserve_bytes() reserves space in the output buffer. Then you
can memcpy into it.
ptr = _stp_reserve_bytes(len);
memcpy(ptr, data, len);
> If not, how fragile is it for me to clone _stp_print and then
> replace the call to strlen with the my buffer length?
That would be equivalent but might break in the future if we change the
internals.
Martin