summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJacob Erlbeck <jerlbeck@sysmocom.de>2014-02-28 15:14:40 +0100
committerJacob Erlbeck <jerlbeck@sysmocom.de>2014-03-04 13:30:12 +0100
commitbaa225ed867fa7357ad05b35b90c9ecb7192f189 (patch)
treec314c21f96ee6490fb4d3a9063e72eb81dca50c3
parent10f0bdecad8d711ccc5fcc04bb0be0adf11a7902 (diff)
msgb: Add msgb_hexdump() function
This function works like osmo_hexdump() and returns a static buffer containing hex bytes along with markers for the layers. Note that it uses osmo_hexdump() internally, thus a call to msgb_hexdump() invalidates the buffer that has been returned by an earlier call to osmo_hexdump(). In short: don't mix them in a single call printf(). Sponsored-by: On-Waves ehf
-rw-r--r--include/osmocom/core/msgb.h1
-rw-r--r--src/msgb.c49
2 files changed, 50 insertions, 0 deletions
diff --git a/include/osmocom/core/msgb.h b/include/osmocom/core/msgb.h
index fe2733b7..5d4bd84b 100644
--- a/include/osmocom/core/msgb.h
+++ b/include/osmocom/core/msgb.h
@@ -73,6 +73,7 @@ extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
extern struct msgb *msgb_dequeue(struct llist_head *queue);
extern void msgb_reset(struct msgb *m);
uint16_t msgb_length(const struct msgb *msg);
+extern const char *msgb_hexdump(const struct msgb *msg);
#ifdef MSGB_DEBUG
#include <osmocom/core/panic.h>
diff --git a/src/msgb.c b/src/msgb.c
index 359a545b..b2fe1d2c 100644
--- a/src/msgb.c
+++ b/src/msgb.c
@@ -153,4 +153,53 @@ void msgb_set_talloc_ctx(void *ctx)
tall_msgb_ctx = ctx;
}
+/*! \brief Return a (static) buffer containing a hexdump of the msg
+ * \param[in] msg message buffer
+ * \returns a pointer to a static char array
+ */
+const char *msgb_hexdump(const struct msgb *msg)
+{
+ static char buf[4100];
+ int buf_offs = 0;
+ int nchars;
+ const unsigned char *start = msg->data;
+ const unsigned char *lxhs[4];
+ int i;
+
+ lxhs[0] = msg->l1h;
+ lxhs[1] = msg->l2h;
+ lxhs[2] = msg->l3h;
+ lxhs[3] = msg->l4h;
+
+ for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
+ if (!lxhs[i])
+ continue;
+
+ if (lxhs[i] < msg->data)
+ goto out_of_range;
+ if (lxhs[i] > msg->tail)
+ goto out_of_range;
+ nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
+ "%s[L%d]> ",
+ osmo_hexdump(start, lxhs[i] - start),
+ i+1);
+ if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
+ return "ERROR";
+
+ buf_offs += nchars;
+ start = lxhs[i];
+ }
+ nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
+ "%s", osmo_hexdump(start, msg->tail - start));
+ if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
+ return "ERROR";
+
+ return buf;
+
+out_of_range:
+ nchars = snprintf(buf, sizeof(buf) - buf_offs,
+ "!!! L%d out of range", i+1);
+ return buf;
+}
+
/*! @} */