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

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