Blame SOURCES/0119-print_fields.h-add-PRINT_FIELD_LEN-macro.patch

9a4987
From 8806a21b612d76c8732d0df2ebaf52b62166ce51 Mon Sep 17 00:00:00 2001
9a4987
From: =?UTF-8?q?=C3=81kos=20Uzonyi?= <uzonyi.akos@gmail.com>
9a4987
Date: Sat, 13 Jun 2020 18:18:31 +0200
9a4987
Subject: [PATCH 119/138] print_fields.h: add PRINT_FIELD_LEN macro
9a4987
9a4987
* print_fields.h (PRINT_FIELD_LEN): New macro.
9a4987
* net.c: (print_get_linger, print_get_ucred, print_tpacket_stats):
9a4987
Rewrite using PRINT_FIELD_LEN.
9a4987
---
9a4987
 net.c          | 90 ++++++----------------------------------------------------
9a4987
 print_fields.h | 15 ++++++++++
9a4987
 2 files changed, 23 insertions(+), 82 deletions(-)
9a4987
9a4987
diff --git a/net.c b/net.c
9a4987
index a58fa92..9ea34b2 100644
9a4987
--- a/net.c
9a4987
+++ b/net.c
9a4987
@@ -579,24 +579,8 @@ print_get_linger(struct tcb *const tcp, const kernel_ulong_t addr,
9a4987
 	if (umoven_or_printaddr(tcp, addr, len, &linger))
9a4987
 		return;
9a4987
 
9a4987
-	if (len < sizeof(linger.l_onoff)) {
9a4987
-		tprints("{l_onoff=");
9a4987
-		print_quoted_string((void *) &linger.l_onoff,
9a4987
-				    len, QUOTE_FORCE_HEX);
9a4987
-	} else {
9a4987
-		PRINT_FIELD_D("{", linger, l_onoff);
9a4987
-
9a4987
-		if (len > offsetof(struct linger, l_linger)) {
9a4987
-			len -= offsetof(struct linger, l_linger);
9a4987
-			if (len < sizeof(linger.l_linger)) {
9a4987
-				tprints(", l_linger=");
9a4987
-				print_quoted_string((void *) &linger.l_linger,
9a4987
-						    len, QUOTE_FORCE_HEX);
9a4987
-			} else {
9a4987
-				PRINT_FIELD_D(", ", linger, l_linger);
9a4987
-			}
9a4987
-		}
9a4987
-	}
9a4987
+	PRINT_FIELD_LEN("{", linger, l_onoff, len, PRINT_FIELD_D);
9a4987
+	PRINT_FIELD_LEN(", ", linger, l_linger, len, PRINT_FIELD_D);
9a4987
 	tprints("}");
9a4987
 }
9a4987
 
9a4987
@@ -617,38 +601,9 @@ print_get_ucred(struct tcb *const tcp, const kernel_ulong_t addr,
9a4987
 	if (umoven_or_printaddr(tcp, addr, len, &uc))
9a4987
 		return;
9a4987
 
9a4987
-	if (len < sizeof(uc.pid)) {
9a4987
-		tprints("{pid=");
9a4987
-		print_quoted_string((void *) &uc.pid,
9a4987
-				    len, QUOTE_FORCE_HEX);
9a4987
-	} else {
9a4987
-		PRINT_FIELD_D("{", uc, pid);
9a4987
-
9a4987
-		if (len > offsetof(struct ucred, uid)) {
9a4987
-			len -= offsetof(struct ucred, uid);
9a4987
-			if (len < sizeof(uc.uid)) {
9a4987
-				tprints(", uid=");
9a4987
-				print_quoted_string((void *) &uc.uid,
9a4987
-						    len, QUOTE_FORCE_HEX);
9a4987
-			} else {
9a4987
-				PRINT_FIELD_UID(", ", uc, uid);
9a4987
-
9a4987
-				if (len > offsetof(struct ucred, gid) -
9a4987
-					  offsetof(struct ucred, uid)) {
9a4987
-					len -= offsetof(struct ucred, gid) -
9a4987
-					       offsetof(struct ucred, uid);
9a4987
-					if (len < sizeof(uc.gid)) {
9a4987
-						tprints(", gid=");
9a4987
-						print_quoted_string((void *) &uc.gid,
9a4987
-								    len,
9a4987
-								    QUOTE_FORCE_HEX);
9a4987
-					} else {
9a4987
-						PRINT_FIELD_UID(", ", uc, gid);
9a4987
-					}
9a4987
-				}
9a4987
-			}
9a4987
-		}
9a4987
-	}
9a4987
+	PRINT_FIELD_LEN("{", uc, pid, len, PRINT_FIELD_D);
9a4987
+	PRINT_FIELD_LEN(", ", uc, uid, len, PRINT_FIELD_UID);
9a4987
+	PRINT_FIELD_LEN(", ", uc, gid, len, PRINT_FIELD_UID);
9a4987
 	tprints("}");
9a4987
 }
9a4987
 
9a4987
@@ -688,38 +643,9 @@ print_tpacket_stats(struct tcb *const tcp, const kernel_ulong_t addr,
9a4987
 	if (umoven_or_printaddr(tcp, addr, len, &stats))
9a4987
 		return;
9a4987
 
9a4987
-	if (len < sizeof(stats.tp_packets)) {
9a4987
-		tprints("{tp_packets=");
9a4987
-		print_quoted_string((void *) &stats.tp_packets,
9a4987
-				    len, QUOTE_FORCE_HEX);
9a4987
-	} else {
9a4987
-		PRINT_FIELD_U("{", stats, tp_packets);
9a4987
-
9a4987
-		if (len > offsetof(struct tp_stats, tp_drops)) {
9a4987
-			len -= offsetof(struct tp_stats, tp_drops);
9a4987
-			if (len < sizeof(stats.tp_drops)) {
9a4987
-				tprints(", tp_drops=");
9a4987
-				print_quoted_string((void *) &stats.tp_drops,
9a4987
-						    len, QUOTE_FORCE_HEX);
9a4987
-			} else {
9a4987
-				PRINT_FIELD_U(", ", stats, tp_drops);
9a4987
-
9a4987
-				if (len > offsetof(struct tp_stats, tp_freeze_q_cnt) -
9a4987
-					  offsetof(struct tp_stats, tp_drops)) {
9a4987
-					len -= offsetof(struct tp_stats, tp_freeze_q_cnt) -
9a4987
-					       offsetof(struct tp_stats, tp_drops);
9a4987
-					if (len < sizeof(stats.tp_freeze_q_cnt)) {
9a4987
-						tprints(", tp_freeze_q_cnt=");
9a4987
-						print_quoted_string((void *) &stats.tp_freeze_q_cnt,
9a4987
-								    len,
9a4987
-								    QUOTE_FORCE_HEX);
9a4987
-					} else {
9a4987
-						PRINT_FIELD_U(", ", stats, tp_freeze_q_cnt);
9a4987
-					}
9a4987
-				}
9a4987
-			}
9a4987
-		}
9a4987
-	}
9a4987
+	PRINT_FIELD_LEN("{", stats, tp_packets, len, PRINT_FIELD_U);
9a4987
+	PRINT_FIELD_LEN(", ", stats, tp_drops, len, PRINT_FIELD_U);
9a4987
+	PRINT_FIELD_LEN(", ", stats, tp_freeze_q_cnt, len, PRINT_FIELD_U);
9a4987
 	tprints("}");
9a4987
 }
9a4987
 #endif /* PACKET_STATISTICS */
9a4987
diff --git a/print_fields.h b/print_fields.h
9a4987
index 02c56bf..70dbbff 100644
9a4987
--- a/print_fields.h
9a4987
+++ b/print_fields.h
9a4987
@@ -277,4 +277,19 @@
9a4987
 			       (size_), (hwtype_));			\
9a4987
 	} while (0)
9a4987
 
9a4987
+# define PRINT_FIELD_LEN(prefix_, where_, field_, 			\
9a4987
+			len_, print_func_, ...)				\
9a4987
+	do {								\
9a4987
+		unsigned int start = offsetof(typeof(where_), field_);	\
9a4987
+		unsigned int end = start + sizeof(where_.field_);	\
9a4987
+		if (len_ >= end) {					\
9a4987
+			print_func_(prefix_, where_, field_,		\
9a4987
+					##__VA_ARGS__);			\
9a4987
+		} else if (len_ > start) {				\
9a4987
+			tprintf("%s%s=", prefix_, #field_);		\
9a4987
+			print_quoted_string((void *)&where_.field_,	\
9a4987
+					len_ - start, QUOTE_FORCE_HEX);	\
9a4987
+		}							\
9a4987
+	} while (0)
9a4987
+
9a4987
 #endif /* !STRACE_PRINT_FIELDS_H */
9a4987
-- 
9a4987
2.1.4
9a4987