Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2344915ybl; Mon, 20 Jan 2020 00:58:22 -0800 (PST) X-Google-Smtp-Source: APXvYqx0HwhZ3E3SS8FFnIaLmGg5cpZjteP9dAHzZH1BQL4ofhrrqHVnINacc7zir69eoYFveIKb X-Received: by 2002:a9d:d06:: with SMTP id 6mr12570703oti.176.1579510701815; Mon, 20 Jan 2020 00:58:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579510701; cv=none; d=google.com; s=arc-20160816; b=MBOZO4Mk/C4qJ5z97wzp1c5rueniFvobaNxYfu2y65VyAd/9UJDx6qSNjBUMymCYXG TAOjEcgxMlnZprJ8V/g9qTCCqBOvYBFkVy1jBtnO2BegEBS2ufb4UP4kVJW6372mng+e 7gfdvPOXOHuk7SVrnSadfCb5xHunPCSV8I7Aa9zeSf+6myIRDwRu9nfxfKuDG3Nf4QyJ 0Swh7NEQEcPbVJq9KpNkzPOWP3nYZ69Za9xpdL7E6jU+zu9VYVq5B1HqiEOb3X0hNH4W QBy4Wlwy1h/10/E4B8WhmSHOP5Tgapr+mPzIYUzPMcrtgBi/SgTR1vyxF0Imi3lEzkrv n0pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ocFAZJ2ll4TZiJ2JJ6JB+yI+UQqQ4kjFaIMZfYGjH5c=; b=UCC066MOSG+5LqEDwOTVHEtP8EN/j/NKW67tpqADYyEy9729oPTEsIqsMp97YUn+08 zmWD5vxAT7AopAnfeOLHywIExu3HypvAd+mDxeRzewXfzFeojXcy+yr2uGihwDNzXnyP er064Z+9McwqAZAe6TBZ6btOHt7uLsLjWZiakNum7IqDfu2E09R/hVwFks4pPSDqAgCd y7NVjFR3W1d5QVqo2CDvUolQ7D4I14rE/zTGpOqh4vdx7HQqMeZWAdZ82AZFf/g2s7L0 EyyJUSWY3nLqqbtN3RqeGxNd/PEsF+b/gCkQTr/xMBBsK7e+lAi7z/340x1zrMf2dVak pBgg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l3si17548473oih.0.2020.01.20.00.58.09; Mon, 20 Jan 2020 00:58:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727107AbgATIzn (ORCPT + 99 others); Mon, 20 Jan 2020 03:55:43 -0500 Received: from metis.ext.pengutronix.de ([85.220.165.71]:59849 "EHLO metis.ext.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726650AbgATIzm (ORCPT ); Mon, 20 Jan 2020 03:55:42 -0500 Received: from pty.hi.pengutronix.de ([2001:67c:670:100:1d::c5]) by metis.ext.pengutronix.de with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1itSqH-0002Vq-QC; Mon, 20 Jan 2020 09:55:29 +0100 Received: from ukl by pty.hi.pengutronix.de with local (Exim 4.89) (envelope-from ) id 1itSqE-0006rz-Rh; Mon, 20 Jan 2020 09:55:26 +0100 From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= To: Petr Mladek , Steven Rostedt , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: linux-kernel@vger.kernel.org, kernel@pengutronix.de Subject: [PATCH 1/2] printf: add support for printing symbolic error names from numbers Date: Mon, 20 Jan 2020 09:55:07 +0100 Message-Id: <20200120085508.25522-2-u.kleine-koenig@pengutronix.de> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20200120085508.25522-1-u.kleine-koenig@pengutronix.de> References: <20200120085508.25522-1-u.kleine-koenig@pengutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::c5 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is an extension of the ability introduced in commit 57f5677e535b ("printf: add support for printing symbolic error names") that made %pe consume an error valued pointer and emit a symbolic error name. Here the same is done for numbers: printk("%de\n", -EIO); now emits "-EIO\n". To keep printk flexible enough to emit an 'e' after a number the character ` can be used which is just swallowed by *printf and ends interpreting the format code. So printk("%d`e\n", -5); emits "-5e\n". (Note that the implementation of ` isn't complete, it only works for numbers for now. It might make sense to implement the same for %s and %p.) For non-error valued numbers %de falls back to emit the plain number (as %d would do). Some runtime tests are added to cover %de. Signed-off-by: Uwe Kleine-König --- lib/test_printf.c | 8 ++++++++ lib/vsprintf.c | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/test_printf.c b/lib/test_printf.c index 2d9f520d2f27..a18a7742d5fe 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -628,6 +629,8 @@ static void __init errptr(void) { test("-1234", "%pe", ERR_PTR(-1234)); + test("-4321", "%de", -4321); + test("-5e", "%d`e", -5); /* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */ BUILD_BUG_ON(IS_ERR(PTR)); @@ -641,6 +644,11 @@ errptr(void) test("[-EIO ]", "[%-8pe]", ERR_PTR(-EIO)); test("[ -EIO]", "[%8pe]", ERR_PTR(-EIO)); test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER)); + + test("-ERESTARTSYS", "%de", -ERESTARTSYS); +#else + + test("-" __stringify(ERESTARTSYS), "%de", -ERESTARTSYS); #endif } diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 7c488a1ce318..1bcd1ce2c319 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2637,7 +2637,39 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) num = va_arg(args, unsigned int); } - str = number(str, end, num, spec); + if (*fmt != 'e') { + str = number(str, end, num, spec); + } else { + unsigned long num_err = 0; + + fmt++; + + /* + * error values are negative numbers near zero. + * If num represents such a number, it must be + * big (as it is unsigned), otherwise print it + * as an ordinary number. + */ + if (num > (unsigned long long)-UINT_MAX) + num_err = num; + + if (IS_ENABLED(CONFIG_SYMBOLIC_ERRNAME) && + IS_ERR_VALUE(num_err)) + str = err_ptr(str, end, ERR_PTR(num), spec); + else + str = number(str, end, num, spec); + } + + if (*fmt == '`') + /* + * When a format specifier is followed by `, + * this ends parsing. This way a string can be + * printed that has an int followed by a literal + * 'e' (using "%d`e") which otherwise (i.e. by + * using "%de") would be interpreted as request + * to format the int as error code. + */ + ++fmt; } } -- 2.24.0