From patchwork Wed Sep 28 08:09:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyi Liu X-Patchwork-Id: 12991746 X-Patchwork-Delegate: bpf@iogearbox.net 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 6370AC6FA82 for ; Wed, 28 Sep 2022 08:15:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232024AbiI1IPr (ORCPT ); Wed, 28 Sep 2022 04:15:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234037AbiI1IOk (ORCPT ); Wed, 28 Sep 2022 04:14:40 -0400 Received: from AUS01-SY4-obe.outbound.protection.outlook.com (mail-sy4aus01olkn2179.outbound.protection.outlook.com [40.92.62.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2A7926F0 for ; Wed, 28 Sep 2022 01:14:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jXLl0B9DMH408/yqfLRqLv2167FuxnukRcEYq9Ht/fgrQZ5XF4mGERg8NGoBD3Y9rbovMTv6UPnwj8hcEBp2AN7pK03SZc0uhIy8P1EFHgVs67dl5dODyYI69oKaDXnjr7dJuvmzP6zZUMsGahxF7E7r/zORmuq831H6btNyW0HI6c+poILmaRKJE649CT1metRfZtgrK9g4KvcYY5HjUjL59N2vXyytT/038PUw5HVmNG6FJ1xL+PRUnb7W2lV9Oywvtr7nNJCl1g7sUFFAamGJJ2m+nPleDRCjRk+Hi3M2OInR309qFQOr3IjSPkzpEUKfaWx+zhjVhmRvvNc09A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=+wePPD4qJcSp+mxjHTndjA1aGiCNU2q5+oYdrxsDivA=; b=df21JUHQ66NkBK0Z/xDvpNk2UhEXnOjGCDWDhBlJeYXH5Hm0VFpiHBpM/TrztqINyL6ecitB1SYqKN1euqvxQTlPLqZ+6skCjL5kVn+mKr+rLTMsaYc9WnT8oKDfwAx2wgfHfsx0ti5GuQQo5DIoly/wWyFWA72u2SlbAVRFmJ8U8Lvf84J5pgobGHB8ODOJvlL23HcXo909X5Q4DM65P+DYFOg0P8wfYTj/tOotVROWNn39gAyMMQnPl1oX8+vXegZoMRbPiZD23Cww2ZfqHRymMVbTCfbm4Y8GZ4gzKg2BOkpo/pBkn0dnz8OBKeU6F+JP5aL449ZsmdRSRxGbhQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=+wePPD4qJcSp+mxjHTndjA1aGiCNU2q5+oYdrxsDivA=; b=lkZbDRWboUKM3RinEFh4f4SABLMwp96GJUNyWhhrqCfm6hN2h0LllmfhG5dFbUJigEoL/+ULq59zA1zE9eyqVV3aPNxsbks5HsxUg73jm41/kFIxGhHUrElQeWtV7dGhkB3fkOLHg6BiAsT0JR/QwsfQgdkzfldTTQiM0jtUolY36oKDyQV/TgK4INZPmXtDC2Fp9Gf81diguKqhFbRApvngfQlKrnMwGdwpNKJsKl4+RUCNGyjpavX8R2+UihWQhKmd/q2paT9Dnu4TCsithplqPeDvZ3XZmDBYA+NorFryixOZ6qNCX8SNsgLogR0ILZJCi57gQS1rQ9+0IbdUnA== Received: from SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:ac::13) by SY6P282MB3943.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:1d7::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.17; Wed, 28 Sep 2022 08:14:32 +0000 Received: from SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM ([fe80::549a:65d7:eae8:3983]) by SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM ([fe80::549a:65d7:eae8:3983%7]) with mapi id 15.20.5654.026; Wed, 28 Sep 2022 08:14:32 +0000 From: Tianyi Liu To: quentin@isovalent.com Cc: bpf@vger.kernel.org, Tianyi Liu Subject: [PATCH v2] bpftool: Fix error message of strerror Date: Wed, 28 Sep 2022 16:09:32 +0800 Message-ID: X-Mailer: git-send-email 2.37.3 X-TMN: [bK8hZrl4+twDVatS9ESbTejAKC88PYyx0BIl6r4xI/lnCbLwWZOqjQ==] X-ClientProxiedBy: SI2PR04CA0011.apcprd04.prod.outlook.com (2603:1096:4:197::10) To SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:ac::13) X-Microsoft-Original-Message-ID: <20220928080932.1456867-1-i.pear@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SY4P282MB1084:EE_|SY6P282MB3943:EE_ X-MS-Office365-Filtering-Correlation-Id: 9bbaefd4-dc04-4ca7-6631-08daa1297940 X-MS-Exchange-SLBlob-MailProps: ZLYX9kf+sFYZRJuE9Nv631LMiEgNb1ZKKj4PYAvPtcynu0Hra/Ljnj2q6P0szow9mY7eQspRYQtc2Z2Df/OVInaRRcIV+xBNx3FE6r6+EDheqnVmpXJ7iVB54zyfzcUVlPKF+9vUaPEogFzAByccGdvBJfUyz55EOE08EFtUsWnEPTW9K15y+jZNuO34YPOlafZo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G+/di/gfOao0VQELzms63L7zF7UhmlaqaV2G/h34ktVyeC/xuoF0JspsVYmTGKFiQAA1jc6gRSwTkmxEom+7tMUmydXSQz2W7dewf27Dy+G6WbzrPAG7Uo4wjIaL63lg/5jkoFqTiJ+MbWT5GmxBqhVDSK3CVjqcHDAKCf940wwhuwuXCWwh7JHsG1xCv9A7FA5u/yGLBB4R0BJm6Ps0qxr5ssEbx6/I+VNSLQeozWPxwyDxm9JSPiMkd6huU7YxRrYnokmm+VkUy8s7rcDu/DhjQnTDtz/qj86C7QRiQBYoVsrPDglHQqqrmXgHXphQ2lLDSzkUCpMqk0iFGq8XqN//Jdm4AzERnRUibe9B+k7UbSvROe87+wjFme2tX26KtV29zb43nryQEvwYesC6V7mbHo8IRlg9xmiC81EkERpn4cJql/m0FErvBOuFkxSdC8ErrD1bjvC87sxaz6omswtLTxzZkGhUdj29CVFtgxhpGMl8VN5MKbw2KjUtscoeZk+jN0ME9iJX8= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9bbaefd4-dc04-4ca7-6631-08daa1297940 X-MS-Exchange-CrossTenant-AuthSource: SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Sep 2022 08:14:32.8684 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SY6P282MB3943 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net strerror() expects a positive errno, however variable err will never be positive when an error occurs. This causes bpftool to output too many "unknown error", even a simple "file not exist" error can not get an accurate message. This patch fixed all "strerror(err)" patterns in bpftool. Specially in btf.c#L823, hashmap__append() is an internal function of libbpf and will not change errno, so there's a little difference. Some libbpf_get_error() calls are kept for return values. Changes since v1: https://lore.kernel.org/bpf/SY4P282MB1084B61CD8671DFA395AA8579D539@SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM/ Check directly for NULL values instead of calling libbpf_get_error(). Signed-off-by: Tianyi Liu Reviewed-by: Quentin Monnet --- tools/bpf/bpftool/btf.c | 11 +++++------ tools/bpf/bpftool/gen.c | 4 ++-- tools/bpf/bpftool/map_perf_ring.c | 7 +++---- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c index 0744bd115..933177bdd 100644 --- a/tools/bpf/bpftool/btf.c +++ b/tools/bpf/bpftool/btf.c @@ -640,10 +640,9 @@ static int do_dump(int argc, char **argv) btf = btf__parse_split(*argv, base ?: base_btf); err = libbpf_get_error(btf); - if (err) { - btf = NULL; + if (!btf) { p_err("failed to load BTF from %s: %s", - *argv, strerror(err)); + *argv, strerror(errno)); goto done; } NEXT_ARG(); @@ -688,8 +687,8 @@ static int do_dump(int argc, char **argv) btf = btf__load_from_kernel_by_id_split(btf_id, base_btf); err = libbpf_get_error(btf); - if (err) { - p_err("get btf by id (%u): %s", btf_id, strerror(err)); + if (!btf) { + p_err("get btf by id (%u): %s", btf_id, strerror(errno)); goto done; } } @@ -825,7 +824,7 @@ build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type, u32_as_hash_field(id)); if (err) { p_err("failed to append entry to hashmap for BTF ID %u, object ID %u: %s", - btf_id, id, strerror(errno)); + btf_id, id, strerror(-err)); goto err_free; } } diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index 7070dcffa..cf8b4e525 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -1594,14 +1594,14 @@ static int do_object(int argc, char **argv) err = bpf_linker__add_file(linker, file, NULL); if (err) { - p_err("failed to link '%s': %s (%d)", file, strerror(err), err); + p_err("failed to link '%s': %s (%d)", file, strerror(errno), errno); goto out; } } err = bpf_linker__finalize(linker); if (err) { - p_err("failed to finalize ELF file: %s (%d)", strerror(err), err); + p_err("failed to finalize ELF file: %s (%d)", strerror(errno), errno); goto out; } diff --git a/tools/bpf/bpftool/map_perf_ring.c b/tools/bpf/bpftool/map_perf_ring.c index 6b0c41015..309d5a1e6 100644 --- a/tools/bpf/bpftool/map_perf_ring.c +++ b/tools/bpf/bpftool/map_perf_ring.c @@ -195,10 +195,9 @@ int do_event_pipe(int argc, char **argv) opts.map_keys = &ctx.idx; pb = perf_buffer__new_raw(map_fd, MMAP_PAGE_CNT, &perf_attr, print_bpf_output, &ctx, &opts); - err = libbpf_get_error(pb); - if (err) { + if (!pb) { p_err("failed to create perf buffer: %s (%d)", - strerror(err), err); + strerror(errno), errno); goto err_close_map; } @@ -213,7 +212,7 @@ int do_event_pipe(int argc, char **argv) err = perf_buffer__poll(pb, 200); if (err < 0 && err != -EINTR) { p_err("perf buffer polling failed: %s (%d)", - strerror(err), err); + strerror(errno), errno); goto err_close_pb; } }