From owner-svn-src-all@freebsd.org Wed May 31 16:24:03 2017 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 69B0FB7DDCB; Wed, 31 May 2017 16:24:03 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 2A33D82232; Wed, 31 May 2017 16:24:03 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v4VGO2NE023169; Wed, 31 May 2017 16:24:02 GMT (envelope-from hselasky@FreeBSD.org) Received: (from hselasky@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v4VGO2Th023168; Wed, 31 May 2017 16:24:02 GMT (envelope-from hselasky@FreeBSD.org) Message-Id: <201705311624.v4VGO2Th023168@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: hselasky set sender to hselasky@FreeBSD.org using -f From: Hans Petter Selasky Date: Wed, 31 May 2017 16:24:02 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r319341 - head/sys/compat/linuxkpi/common/include/linux X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 31 May 2017 16:24:03 -0000 Author: hselasky Date: Wed May 31 16:24:02 2017 New Revision: 319341 URL: https://svnweb.freebsd.org/changeset/base/319341 Log: Implement print_hex_dump(), print_hex_dump_bytes() and printk_ratelimited() in the LinuxKPI. While at it fix the inclusion guard of printk.h to be similar to the rest of the LinuxKPI header files. MFC after: 1 week Sponsored by: Mellanox Technologies Modified: head/sys/compat/linuxkpi/common/include/linux/printk.h Modified: head/sys/compat/linuxkpi/common/include/linux/printk.h ============================================================================== --- head/sys/compat/linuxkpi/common/include/linux/printk.h Wed May 31 16:08:30 2017 (r319340) +++ head/sys/compat/linuxkpi/common/include/linux/printk.h Wed May 31 16:24:02 2017 (r319341) @@ -2,7 +2,7 @@ * Copyright (c) 2010 Isilon Systems, Inc. * Copyright (c) 2010 iX Systems, Inc. * Copyright (c) 2010 Panasas, Inc. - * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. + * Copyright (c) 2013-2017 Mellanox Technologies, Ltd. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,9 +28,11 @@ * * $FreeBSD$ */ -#ifndef _FBSD_PRINTK_H_ -#define _FBSD_PRINTK_H_ +#ifndef _LINUX_PRINTK_H_ +#define _LINUX_PRINTK_H_ +#include + /* GID printing macros */ #define GID_PRINT_FMT "%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x" #define GID_PRINT_ARGS(gid_raw) htons(((u16 *)gid_raw)[0]), htons(((u16 *)gid_raw)[1]),\ @@ -38,4 +40,76 @@ htons(((u16 *)gid_raw)[4]), htons(((u16 *)gid_raw)[5]),\ htons(((u16 *)gid_raw)[6]), htons(((u16 *)gid_raw)[7]) -#endif /* _FBSD_PRINTK_H */ +enum { + DUMP_PREFIX_NONE, + DUMP_PREFIX_ADDRESS, + DUMP_PREFIX_OFFSET +}; + +static inline void +print_hex_dump(const char *level, const char *prefix_str, + const int prefix_type, const int rowsize, const int groupsize, + const void *buf, size_t len, const bool ascii) +{ + typedef const struct { long long value; } __packed *print_64p_t; + typedef const struct { uint32_t value; } __packed *print_32p_t; + typedef const struct { uint16_t value; } __packed *print_16p_t; + const void *buf_old = buf; + int row; + + while (len > 0) { + if (level != NULL) + printf("%s", level); + if (prefix_str != NULL) + printf("%s ", prefix_str); + + switch (prefix_type) { + case DUMP_PREFIX_ADDRESS: + printf("[%p] ", buf); + break; + case DUMP_PREFIX_OFFSET: + printf("[%p] ", (const char *)((const char *)buf - + (const char *)buf_old)); + break; + default: + break; + } + for (row = 0; row != rowsize; row++) { + if (groupsize == 8 && len > 7) { + printf("%016llx ", ((print_64p_t)buf)->value); + buf = (const uint8_t *)buf + 8; + len -= 8; + } else if (groupsize == 4 && len > 3) { + printf("%08x ", ((print_32p_t)buf)->value); + buf = (const uint8_t *)buf + 4; + len -= 4; + } else if (groupsize == 2 && len > 1) { + printf("%04x ", ((print_16p_t)buf)->value); + buf = (const uint8_t *)buf + 2; + len -= 2; + } else if (len > 0) { + printf("%02x ", *(const uint8_t *)buf); + buf = (const uint8_t *)buf + 1; + len--; + } else { + break; + } + } + printf("\n"); + } +} + +static inline void +print_hex_dump_bytes(const char *prefix_str, const int prefix_type, + const void *buf, size_t len) +{ + print_hex_dump(NULL, prefix_str, prefix_type, 16, 1, buf, len, 0); +} + +#define printk_ratelimited(...) do { \ + static linux_ratelimit_t __ratelimited; \ + if (linux_ratelimited(&__ratelimited)) \ + printk(__VA_ARGS__); \ +} while (0) + +#endif /* _LINUX_PRINTK_H_ */