Add GWBUF pretty-printing

The gwbuf_hexdump_pretty displays the hex contents of the buffer alongside
the human-readable version of it. The text version helps identify parts of
the buffer that contain text which makes protocol data decoding easier.
This commit is contained in:
Markus Mäkelä
2019-12-04 11:07:34 +02:00
parent e829cae8b1
commit babce13ec6
2 changed files with 83 additions and 6 deletions

View File

@ -165,7 +165,7 @@ inline bool gwbuf_is_contiguous(const GWBUF* b)
/*< True if all bytes in the buffer have been consumed */
inline bool gwbuf_link_empty(const GWBUF* b)
{
return ((char*)b->start >= (char*)b->end);
return (char*)b->start >= (char*)b->end;
}
#define GWBUF_EMPTY(b) gwbuf_link_empty(b)
@ -422,11 +422,22 @@ extern void dprintAllBuffers(void* pdcb);
/**
* Debug function for dumping buffer contents to log
*
* @see mxs::Buffer::hexdump
*
* @param buffer Buffer to dump
* @param log_level Log priority where the message is written
*/
void gwbuf_hexdump(GWBUF* buffer, int log_level);
void gwbuf_hexdump(const mxs::Buffer& buffer, int log_level);
void gwbuf_hexdump(GWBUF* buffer, int log_level = LOG_INFO);
/**
* Debug function for pretty-printing buffer contents to log
*
* @see mxs::Buffer::hexdump_pretty
*
* @param buffer Buffer to dump
* @param log_level Log priority where the message is written
*/
void gwbuf_hexdump_pretty(GWBUF* buffer, int log_level = LOG_INFO);
/**
* Return pointer of the byte at offset from start of chained buffer
@ -1213,6 +1224,24 @@ public:
}
}
/**
* Debug function for dumping buffer contents to log
*
* Prints contents as hexadecimal. Only the first 1024 bytes are dumped to avoid filling up the log.
*
* @param log_level Log priority where the message is written
*/
void hexdump(int log_level = LOG_INFO) const;
/**
* Debug function for pretty-printing buffer contents to log
*
* The output format is similar to `hexdump -C` and provides both hex and human-readable values.
*
* @param log_level Log priority where the message is written
*/
void hexdump_pretty(int log_level = LOG_INFO) const;
private:
// To prevent @c Buffer from being created on the heap.
void* operator new(size_t); // standard new