[RFC PATCH 04/12] print_fields.h: add PRINT_FIELD_LEN macro

Ákos Uzonyi uzonyi.akos at gmail.com
Fri Jun 12 14:33:48 UTC 2020


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

diff --git a/net.c b/net.c
index a58fa921..4569a298 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, linger, l_onoff, len, PRINT_FIELD_D);
+	PRINT_FIELD_LEN(", ", linger, 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("{", ucred, uc, pid, len, PRINT_FIELD_D);
+	PRINT_FIELD_LEN(", ", ucred, uc, uid, len, PRINT_FIELD_UID);
+	PRINT_FIELD_LEN(", ", ucred, 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("{", tp_stats, stats, tp_packets, len, PRINT_FIELD_U);
+	PRINT_FIELD_LEN(", ", tp_stats, stats, tp_drops, len, PRINT_FIELD_U);
+	PRINT_FIELD_LEN(", ", tp_stats, stats, tp_freeze_q_cnt, len, PRINT_FIELD_U);
 	tprints("}");
 }
 #endif /* PACKET_STATISTICS */
diff --git a/print_fields.h b/print_fields.h
index 02c56bf9..2de7d613 100644
--- a/print_fields.h
+++ b/print_fields.h
@@ -277,4 +277,18 @@
 			       (size_), (hwtype_));			\
 	} while (0)
 
+# define PRINT_FIELD_LEN(prefix_, struct_t_, struct_val_, field_, len_,	\
+			print_func_, args...)				\
+do {									\
+	unsigned int start = offsetof(struct struct_t_, field_);	\
+	unsigned int end = start + sizeof(struct_val_.field_);		\
+	if (len_ >= end) {						\
+		print_func_(prefix_, struct_val_, field_, ##args);	\
+	} else if (len_ > start) {					\
+		tprintf("%s%s=", prefix_, #field_);			\
+		print_quoted_string((void *)&struct_val_.field_,	\
+				len_ - start, QUOTE_FORCE_HEX);		\
+	}								\
+} while (0);
+
 #endif /* !STRACE_PRINT_FIELDS_H */
-- 
2.27.0



More information about the Strace-devel mailing list