Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60122C6FD1C for ; Thu, 9 Mar 2023 02:38:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229887AbjCICiE (ORCPT ); Wed, 8 Mar 2023 21:38:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229905AbjCICh7 (ORCPT ); Wed, 8 Mar 2023 21:37:59 -0500 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2911A1A4A9 for ; Wed, 8 Mar 2023 18:37:56 -0800 (PST) Received: by mail-wm1-x334.google.com with SMTP id k37so239026wms.0 for ; Wed, 08 Mar 2023 18:37:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678329474; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=TXL7RZERqmJzSobCVKRWDL9NAZQE0OJJ5vuFdJHuRnc=; b=ZYU+Sb2tQjjifOx7Oqtj7ZDNib0alWBWvEgmvZ0qkrVD3lchav+vO+nVx5QbIq3XP7 mIW+zxWiNxuENT+uwoI/ndW7C+4u/I5mlwMXiox7puwclxcE7pu1nqdSuwsY43aIZxwy 8Awz0bjQlf3ZLlxZTukHSWp5DbKdWCXeH/zDO5B4DMuPRQNTi+uX1UGJSXyHza2pf4fM 9xnIxZbUFEvcADgMj3BgzFz/yYps5YWnBXwpx3MyS3AgXpBkpSQLyZG/UyCUgslzTewn n8SV5ENCBL2ojwXGYil2eBK41oDVQxIgh1A5B5FvrEZWTd5Lb9I8qCrnW3xQ4Cpnlw1s m+tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678329474; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TXL7RZERqmJzSobCVKRWDL9NAZQE0OJJ5vuFdJHuRnc=; b=2R8rnDtf2pVYFjtNgMqe8tYwiY8rEEqjxPAvGMS890MdgtGWdsdErjPnrh4jHE+/nt 6HL7K7fu7/Dcad7QI3jbHzMNEO/XYMz7R5rSOcLZkiqQMpuIDTfpOUTM9/qUr2Ojnlzf W6LBNlGV1mTA590OSRimDIgvXb8G5KCseOP4d77o+1PI0fNQaAMpjiiMDoTgpXic25Nt O3aTmxv0I8ePkZ6pUx1nARlKXG11nBTKRMceE8ddBiZ5fMkmO+m+S5QiU7ujNcN4k5i3 zZaspmnIZ3B5tnbjkvTDP+owz2g2JTDoQp1BZ2/NxjjPeVn8j0OppGDTMufmToYyEpRI KlsA== X-Gm-Message-State: AO0yUKWGZUMBH9nBgCMfzfDTapaP1eVuCFDMmNiz/NsKH2nwpnoorJL+ E1nD9OlGziXWzfnAhkNnO6I5iSnEAwPYEZezY+6TDg== X-Google-Smtp-Source: AK7set/q1INFZHRH5R1oph46r7oD8URJVmpPe1C0vjxVg86ZYtihS6TiUen+iaaRNGpIbtK17gQYlhdZIe3WNnue1Wg= X-Received: by 2002:a05:600c:a39a:b0:3df:d852:ede2 with SMTP id hn26-20020a05600ca39a00b003dfd852ede2mr4078693wmb.0.1678329474477; Wed, 08 Mar 2023 18:37:54 -0800 (PST) MIME-Version: 1.0 References: <20230308203952.3060546-1-rmoar@google.com> <20230308203952.3060546-3-rmoar@google.com> In-Reply-To: <20230308203952.3060546-3-rmoar@google.com> From: David Gow Date: Thu, 9 Mar 2023 10:37:42 +0800 Message-ID: Subject: Re: [PATCH v4 3/3] kunit: fix bug of extra newline characters in debugfs logs To: Rae Moar Cc: brendanhiggins@google.com, dlatypov@google.com, skhan@linuxfoundation.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha-256; boundary="0000000000002b633905f66e88e9" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --0000000000002b633905f66e88e9 Content-Type: text/plain; charset="UTF-8" On Thu, 9 Mar 2023 at 04:40, Rae Moar wrote: > > Fix bug of the extra newline characters in debugfs logs. When a > line is added to debugfs with a newline character at the end, > an extra line appears in the debugfs log. > > This is due to a discrepancy between how the lines are printed and how they > are added to the logs. Remove this discrepancy by checking if a newline > character is present before adding a newline character. This should closely > match the printk behavior. > > Add kunit_log_newline_test to provide test coverage for this issue. (Also, > move kunit_log_test above suite definition to remove the unnecessary > declaration prior to the suite definition) > > As an example, say we add these two lines to the log: > > kunit_log(..., "KTAP version 1\n"); > kunit_log(..., "1..1"); > > The debugfs log before this fix: > > KTAP version 1 > > 1..1 > > The debugfs log after this fix: > > KTAP version 1 > 1..1 > > Signed-off-by: Rae Moar > Reviewed-by: David Gow > --- > > Changes from v3 -> v4: > - Slight formatting change in test.h regarding "\" placement. > Thanks. This series looks good-to-go to me now! Cheers, -- David > Changes from v2 -> v3: > - Changes to commit message. > > Changes from v1 -> v2: > - Changed the way extra newlines are removed. Instead of removing extra > newline characters, add a newline if one is not present. This is a bit > cleaner. > - Note: I looked into using KERN_CONT to match the printk behavior to > vsnprintf but this could cause issues with KTAP printing on the same line > as interrupting kernel messages. I also looked at just adding KERN_CONT > functionality to kunit_log and I did get this to work but it was a bit > messy because it required a few calls to kunit_log_newline in > kunit_run_tests. If this is very desired functionality, happy to add this > to version 3. > > include/kunit/test.h | 2 +- > lib/kunit/kunit-test.c | 35 +++++++++++++++++++++++------------ > lib/kunit/test.c | 18 ++++++++++++++++++ > 3 files changed, 42 insertions(+), 13 deletions(-) > > diff --git a/include/kunit/test.h b/include/kunit/test.h > index 0668d29f3453..9721584027d8 100644 > --- a/include/kunit/test.h > +++ b/include/kunit/test.h > @@ -420,7 +420,7 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...); > #define kunit_log(lvl, test_or_suite, fmt, ...) \ > do { \ > printk(lvl fmt, ##__VA_ARGS__); \ > - kunit_log_append((test_or_suite)->log, fmt "\n", \ > + kunit_log_append((test_or_suite)->log, fmt, \ > ##__VA_ARGS__); \ > } while (0) > > diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c > index 4df0335d0d06..b63595d3e241 100644 > --- a/lib/kunit/kunit-test.c > +++ b/lib/kunit/kunit-test.c > @@ -443,18 +443,6 @@ static struct kunit_suite kunit_resource_test_suite = { > .test_cases = kunit_resource_test_cases, > }; > > -static void kunit_log_test(struct kunit *test); > - > -static struct kunit_case kunit_log_test_cases[] = { > - KUNIT_CASE(kunit_log_test), > - {} > -}; > - > -static struct kunit_suite kunit_log_test_suite = { > - .name = "kunit-log-test", > - .test_cases = kunit_log_test_cases, > -}; > - > static void kunit_log_test(struct kunit *test) > { > struct kunit_suite suite; > @@ -481,6 +469,29 @@ static void kunit_log_test(struct kunit *test) > #endif > } > > +static void kunit_log_newline_test(struct kunit *test) > +{ > + kunit_info(test, "Add newline\n"); > + if (test->log) { > + KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(test->log, "Add newline\n"), > + "Missing log line, full log:\n%s", test->log); > + KUNIT_EXPECT_NULL(test, strstr(test->log, "Add newline\n\n")); > + } else { > + kunit_skip(test, "only useful when debugfs is enabled"); > + } > +} > + > +static struct kunit_case kunit_log_test_cases[] = { > + KUNIT_CASE(kunit_log_test), > + KUNIT_CASE(kunit_log_newline_test), > + {} > +}; > + > +static struct kunit_suite kunit_log_test_suite = { > + .name = "kunit-log-test", > + .test_cases = kunit_log_test_cases, > +}; > + > static void kunit_status_set_failure_test(struct kunit *test) > { > struct kunit fake; > diff --git a/lib/kunit/test.c b/lib/kunit/test.c > index 811fcc376d2f..e2910b261112 100644 > --- a/lib/kunit/test.c > +++ b/lib/kunit/test.c > @@ -108,6 +108,22 @@ static void kunit_print_test_stats(struct kunit *test, > stats.total); > } > > +/** > + * kunit_log_newline() - Add newline to the end of log if one is not > + * already present. > + * @log: The log to add the newline to. > + */ > +static void kunit_log_newline(char *log) > +{ > + int log_len, len_left; > + > + log_len = strlen(log); > + len_left = KUNIT_LOG_SIZE - log_len - 1; > + > + if (log_len > 0 && log[log_len - 1] != '\n') > + strncat(log, "\n", len_left); > +} > + > /* > * Append formatted message to log, size of which is limited to > * KUNIT_LOG_SIZE bytes (including null terminating byte). > @@ -135,6 +151,8 @@ void kunit_log_append(char *log, const char *fmt, ...) > vsnprintf(log + log_len, min(len, len_left), fmt, args); > va_end(args); > > + /* Add newline to end of log if not already present. */ > + kunit_log_newline(log); > } > EXPORT_SYMBOL_GPL(kunit_log_append); > > -- > 2.40.0.rc0.216.gc4246ad0f0-goog > --0000000000002b633905f66e88e9 Content-Type: application/pkcs7-signature; name="smime.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime.p7s" Content-Description: S/MIME Cryptographic Signature MIIPnwYJKoZIhvcNAQcCoIIPkDCCD4wCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg ggz5MIIEtjCCA56gAwIBAgIQeAMYYHb81ngUVR0WyMTzqzANBgkqhkiG9w0BAQsFADBMMSAwHgYD VQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMzETMBEGA1UEChMKR2xvYmFsU2lnbjETMBEGA1UE AxMKR2xvYmFsU2lnbjAeFw0yMDA3MjgwMDAwMDBaFw0yOTAzMTgwMDAwMDBaMFQxCzAJBgNVBAYT AkJFMRkwFwYDVQQKExBHbG9iYWxTaWduIG52LXNhMSowKAYDVQQDEyFHbG9iYWxTaWduIEF0bGFz IFIzIFNNSU1FIENBIDIwMjAwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCvLe9xPU9W dpiHLAvX7kFnaFZ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