Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp68042imm; Wed, 5 Sep 2018 15:07:52 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaUphEzz3uOkyU0qdsIdcd/n4eIReE3lGUc8Ql1p6Y6RIZQ61/JopsQJoip1eL1kFbbIwVo X-Received: by 2002:a17:902:598d:: with SMTP id p13-v6mr18398957pli.171.1536185272706; Wed, 05 Sep 2018 15:07:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536185272; cv=none; d=google.com; s=arc-20160816; b=yzuCuls/bDBZBTSK0NCLAXNeJX13tKiNWT+LYY6ov2YHiXpO0Y85TVFZGTSG2dGjAD NIYSeG/jj2VrExEo4z7KJ0oPL+2rMMFrrdWNHUyoYnIFW/PNR4KxBaG3SA6jMylx0ul5 ddwHiLpuJ1n+cfzNPg1ypiJYHi+WxH9IoDqqPMD5H/1f3Ia+jzU07n0aW4iChxhb48IP xcAuKrgEKZE/wL4uFyyK1EKEmf/pquhSYkOLDlVB+FKWT0Z1TnnWmkhLhllOI4LkZ463 GS+8tMLb5+YO9HwGs98zGAFh1j4fimrI7k/mjMX7oLCvkv42LpioAUkZ6NAkwdOQo3gA hiNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=8qBYlyC8Y/yvoFBTFr/uPNcGe/Mmx39YH/r1d8bCbK8=; b=s27QiO3ApQt4mBrTW7lZogVeBNun3tcNLJGQZ07T19xCngJQ8kXVs2IU+66VOFemzu 1rr8Ebw5VcpzqpueDq9pbn1+mfPdoO55yAUTIuxpEf6BuJjGtotwWxBYqL5MpYxnPAsg yGxfPKujPmxFSi3QuYedanuwfjSCWNqhRivnQXWRlP4ssudUntaNpWd5Axs6MFM/7Zs1 X0jkG5i9C8NpO5OcuePe1aZWOfiVuwPTfLACFW1XmsvtfqAZrpX7EP2c2nwtBiiwqMgp 1WiNw9hkKwHtHlWpEnUStpMpwwtl5mzYQsJV21tFCml92JyVBpcYVqqjTvy494HC7Gx2 qvGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JQ24M+Al; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z16-v6si3262526pgi.252.2018.09.05.15.07.37; Wed, 05 Sep 2018 15:07:52 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=default header.b=JQ24M+Al; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728317AbeIFChx (ORCPT + 99 others); Wed, 5 Sep 2018 22:37:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:52220 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727518AbeIFChw (ORCPT ); Wed, 5 Sep 2018 22:37:52 -0400 Received: from jouet.infradead.org (unknown [179.97.41.186]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E1CB42077C; Wed, 5 Sep 2018 22:05:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1536185141; bh=a7oOcxPOzk3lqwGzNf6UGNgUyfS2zAJ1sZCQuCsuF60=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JQ24M+Al9KYgfRDjD8ECId4c/dXAYZjqpQ7TkR2S2R/8XoH1rBqIR63XISaVWdJQH /stRM1a1VCKY1Rb6MKWoHsg9HuuLqpo3kEVJTXWG52n0m+mtGviNGpEGXPC2acWcK3 HEb58VzQssRBXu9C4z9dK1TQzxF40n1aNybyBfRk= From: Arnaldo Carvalho de Melo To: Ingo Molnar Cc: Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Arnaldo Carvalho de Melo , Adrian Hunter , David Ahern , Jiri Olsa , Namhyung Kim , Wang Nan Subject: [PATCH 18/77] perf trace beauty: Reorganize 'struct sockaddr *' beautifier Date: Wed, 5 Sep 2018 19:03:41 -0300 Message-Id: <20180905220440.20256-19-acme@kernel.org> X-Mailer: git-send-email 2.14.4 In-Reply-To: <20180905220440.20256-1-acme@kernel.org> References: <20180905220440.20256-1-acme@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Arnaldo Carvalho de Melo Use an array to multiplex by sockaddr->sa_family, this way adding new families gets a bit easier and tidy. Cc: Adrian Hunter Cc: David Ahern Cc: Jiri Olsa Cc: Namhyung Kim Cc: Wang Nan Link: https://lkml.kernel.org/n/tip-v3s85ra659tc40g1s1xaqoun@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/trace/beauty/sockaddr.c | 60 +++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/tools/perf/trace/beauty/sockaddr.c b/tools/perf/trace/beauty/sockaddr.c index 3944a7d54d3c..71a79f72d9d9 100644 --- a/tools/perf/trace/beauty/sockaddr.c +++ b/tools/perf/trace/beauty/sockaddr.c @@ -17,34 +17,52 @@ static const char *socket_families[] = { }; DEFINE_STRARRAY(socket_families); +static size_t af_inet__scnprintf(struct sockaddr *sa, char *bf, size_t size) +{ + struct sockaddr_in *sin = (struct sockaddr_in *)sa; + char tmp[16]; + return scnprintf(bf, size, ", port: %d, addr: %s", ntohs(sin->sin_port), + inet_ntop(sin->sin_family, &sin->sin_addr, tmp, sizeof(tmp))); +} + +static size_t af_inet6__scnprintf(struct sockaddr *sa, char *bf, size_t size) +{ + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; + u32 flowinfo = ntohl(sin6->sin6_flowinfo); + char tmp[512]; + size_t printed = scnprintf(bf, size, ", port: %d, addr: %s", ntohs(sin6->sin6_port), + inet_ntop(sin6->sin6_family, &sin6->sin6_addr, tmp, sizeof(tmp))); + if (flowinfo != 0) + printed += scnprintf(bf + printed, size - printed, ", flowinfo: %lu", flowinfo); + if (sin6->sin6_scope_id != 0) + printed += scnprintf(bf + printed, size - printed, ", scope_id: %lu", sin6->sin6_scope_id); + + return printed; +} + +static size_t af_local__scnprintf(struct sockaddr *sa, char *bf, size_t size) +{ + struct sockaddr_un *sun = (struct sockaddr_un *)sa; + return scnprintf(bf, size, ", path: %s", sun->sun_path); +} + +static size_t (*af_scnprintfs[])(struct sockaddr *sa, char *bf, size_t size) = { + [AF_LOCAL] = af_local__scnprintf, + [AF_INET] = af_inet__scnprintf, + [AF_INET6] = af_inet6__scnprintf, +}; + static size_t syscall_arg__scnprintf_augmented_sockaddr(struct syscall_arg *arg, char *bf, size_t size) { - struct sockaddr_in *sin = (struct sockaddr_in *)arg->augmented.args; + struct sockaddr *sa = (struct sockaddr *)arg->augmented.args; char family[32]; size_t printed; - strarray__scnprintf(&strarray__socket_families, family, sizeof(family), "%d", sin->sin_family); + strarray__scnprintf(&strarray__socket_families, family, sizeof(family), "%d", sa->sa_family); printed = scnprintf(bf, size, "{ .family: %s", family); - if (sin->sin_family == AF_INET) { - char tmp[512]; - printed += scnprintf(bf + printed, size - printed, ", port: %d, addr: %s", ntohs(sin->sin_port), - inet_ntop(sin->sin_family, &sin->sin_addr, tmp, sizeof(tmp))); - } else if (sin->sin_family == AF_INET6) { - struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin; - u32 flowinfo = ntohl(sin6->sin6_flowinfo); - char tmp[512]; - - printed += scnprintf(bf + printed, size - printed, ", port: %d, addr: %s", ntohs(sin6->sin6_port), - inet_ntop(sin6->sin6_family, &sin6->sin6_addr, tmp, sizeof(tmp))); - if (flowinfo != 0) - printed += scnprintf(bf + printed, size - printed, ", flowinfo: %lu", flowinfo); - if (sin6->sin6_scope_id != 0) - printed += scnprintf(bf + printed, size - printed, ", scope_id: %lu", sin6->sin6_scope_id); - } else if (sin->sin_family == AF_LOCAL) { - struct sockaddr_un *sun = (struct sockaddr_un *)sin; - printed += scnprintf(bf + printed, size - printed, ", path: %s", sun->sun_path); - } + if (sa->sa_family < ARRAY_SIZE(af_scnprintfs) && af_scnprintfs[sa->sa_family]) + printed += af_scnprintfs[sa->sa_family](sa, bf + printed, size - printed); return printed + scnprintf(bf + printed, size - printed, " }"); } -- 2.14.4