Blob Blame History Raw
From 8806a21b612d76c8732d0df2ebaf52b62166ce51 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=81kos=20Uzonyi?= <uzonyi.akos@gmail.com>
Date: Sat, 13 Jun 2020 18:18:31 +0200
Subject: [PATCH 119/138] print_fields.h: add PRINT_FIELD_LEN macro

* print_fields.h (PRINT_FIELD_LEN): New macro.
* net.c: (print_get_linger, print_get_ucred, print_tpacket_stats):
Rewrite using PRINT_FIELD_LEN.
---
 net.c          | 90 ++++++----------------------------------------------------
 print_fields.h | 15 ++++++++++
 2 files changed, 23 insertions(+), 82 deletions(-)

diff --git a/net.c b/net.c
index a58fa92..9ea34b2 100644
--- a/net.c
+++ b/net.c
@@ -579,24 +579,8 @@ print_get_linger(struct tcb *const tcp, const kernel_ulong_t addr,
 	if (umoven_or_printaddr(tcp, addr, len, &linger))
 		return;
 
-	if (len < sizeof(linger.l_onoff)) {
-		tprints("{l_onoff=");
-		print_quoted_string((void *) &linger.l_onoff,
-				    len, QUOTE_FORCE_HEX);
-	} else {
-		PRINT_FIELD_D("{", linger, l_onoff);
-
-		if (len > offsetof(struct linger, l_linger)) {
-			len -= offsetof(struct linger, l_linger);
-			if (len < sizeof(linger.l_linger)) {
-				tprints(", l_linger=");
-				print_quoted_string((void *) &linger.l_linger,
-						    len, QUOTE_FORCE_HEX);
-			} else {
-				PRINT_FIELD_D(", ", linger, l_linger);
-			}
-		}
-	}
+	PRINT_FIELD_LEN("{", linger, l_onoff, len, PRINT_FIELD_D);
+	PRINT_FIELD_LEN(", ", linger, l_linger, len, PRINT_FIELD_D);
 	tprints("}");
 }
 
@@ -617,38 +601,9 @@ print_get_ucred(struct tcb *const tcp, const kernel_ulong_t addr,
 	if (umoven_or_printaddr(tcp, addr, len, &uc))
 		return;
 
-	if (len < sizeof(uc.pid)) {
-		tprints("{pid=");
-		print_quoted_string((void *) &uc.pid,
-				    len, QUOTE_FORCE_HEX);
-	} else {
-		PRINT_FIELD_D("{", uc, pid);
-
-		if (len > offsetof(struct ucred, uid)) {
-			len -= offsetof(struct ucred, uid);
-			if (len < sizeof(uc.uid)) {
-				tprints(", uid=");
-				print_quoted_string((void *) &uc.uid,
-						    len, QUOTE_FORCE_HEX);
-			} else {
-				PRINT_FIELD_UID(", ", uc, uid);
-
-				if (len > offsetof(struct ucred, gid) -
-					  offsetof(struct ucred, uid)) {
-					len -= offsetof(struct ucred, gid) -
-					       offsetof(struct ucred, uid);
-					if (len < sizeof(uc.gid)) {
-						tprints(", gid=");
-						print_quoted_string((void *) &uc.gid,
-								    len,
-								    QUOTE_FORCE_HEX);
-					} else {
-						PRINT_FIELD_UID(", ", uc, gid);
-					}
-				}
-			}
-		}
-	}
+	PRINT_FIELD_LEN("{", uc, pid, len, PRINT_FIELD_D);
+	PRINT_FIELD_LEN(", ", uc, uid, len, PRINT_FIELD_UID);
+	PRINT_FIELD_LEN(", ", uc, gid, len, PRINT_FIELD_UID);
 	tprints("}");
 }
 
@@ -688,38 +643,9 @@ print_tpacket_stats(struct tcb *const tcp, const kernel_ulong_t addr,
 	if (umoven_or_printaddr(tcp, addr, len, &stats))
 		return;
 
-	if (len < sizeof(stats.tp_packets)) {
-		tprints("{tp_packets=");
-		print_quoted_string((void *) &stats.tp_packets,
-				    len, QUOTE_FORCE_HEX);
-	} else {
-		PRINT_FIELD_U("{", stats, tp_packets);
-
-		if (len > offsetof(struct tp_stats, tp_drops)) {
-			len -= offsetof(struct tp_stats, tp_drops);
-			if (len < sizeof(stats.tp_drops)) {
-				tprints(", tp_drops=");
-				print_quoted_string((void *) &stats.tp_drops,
-						    len, QUOTE_FORCE_HEX);
-			} else {
-				PRINT_FIELD_U(", ", stats, tp_drops);
-
-				if (len > offsetof(struct tp_stats, tp_freeze_q_cnt) -
-					  offsetof(struct tp_stats, tp_drops)) {
-					len -= offsetof(struct tp_stats, tp_freeze_q_cnt) -
-					       offsetof(struct tp_stats, tp_drops);
-					if (len < sizeof(stats.tp_freeze_q_cnt)) {
-						tprints(", tp_freeze_q_cnt=");
-						print_quoted_string((void *) &stats.tp_freeze_q_cnt,
-								    len,
-								    QUOTE_FORCE_HEX);
-					} else {
-						PRINT_FIELD_U(", ", stats, tp_freeze_q_cnt);
-					}
-				}
-			}
-		}
-	}
+	PRINT_FIELD_LEN("{", stats, tp_packets, len, PRINT_FIELD_U);
+	PRINT_FIELD_LEN(", ", stats, tp_drops, len, PRINT_FIELD_U);
+	PRINT_FIELD_LEN(", ", stats, tp_freeze_q_cnt, len, PRINT_FIELD_U);
 	tprints("}");
 }
 #endif /* PACKET_STATISTICS */
diff --git a/print_fields.h b/print_fields.h
index 02c56bf..70dbbff 100644
--- a/print_fields.h
+++ b/print_fields.h
@@ -277,4 +277,19 @@
 			       (size_), (hwtype_));			\
 	} while (0)
 
+# define PRINT_FIELD_LEN(prefix_, where_, field_, 			\
+			len_, print_func_, ...)				\
+	do {								\
+		unsigned int start = offsetof(typeof(where_), field_);	\
+		unsigned int end = start + sizeof(where_.field_);	\
+		if (len_ >= end) {					\
+			print_func_(prefix_, where_, field_,		\
+					##__VA_ARGS__);			\
+		} else if (len_ > start) {				\
+			tprintf("%s%s=", prefix_, #field_);		\
+			print_quoted_string((void *)&where_.field_,	\
+					len_ - start, QUOTE_FORCE_HEX);	\
+		}							\
+	} while (0)
+
 #endif /* !STRACE_PRINT_FIELDS_H */
-- 
2.1.4