[PATCH] Refactor common sa_handler printing code

Eugene Syromyatnikov evgsyr at gmail.com
Wed Aug 31 16:04:21 UTC 2016


* xlat/sa_handler_values.in: New file.
* signal.c: Include "xlat/sa_handler_values.h".
(get_sa_handler_str, print_sa_handler): New functions.
(SYS_FUNC(sigsetmask), SYS_FUNC(signal), decode_new_sigaction): Use them.
---
 signal.c                  |   64 ++++++++++++++++-----------------------------
 xlat/sa_handler_values.in |    4 +++
 2 files changed, 27 insertions(+), 41 deletions(-)
 create mode 100644 xlat/sa_handler_values.in

diff --git a/signal.c b/signal.c
index 5b530cf..b796b10 100644
--- a/signal.c
+++ b/signal.c
@@ -70,6 +70,7 @@
 # endif
 #endif
 
+#include "xlat/sa_handler_values.h"
 #include "xlat/sigact_flags.h"
 #include "xlat/sigprocmaskcmds.h"
 
@@ -113,6 +114,23 @@
  * Use (NSIG / 8) as a size instead.
  */
 
+static const char *
+get_sa_handler_str(unsigned long handler)
+{
+	return xlookup(sa_handler_values, handler);
+}
+
+static void
+print_sa_handler(unsigned long handler)
+{
+	const char *sa_handler_str = get_sa_handler_str(handler);
+
+	if (sa_handler_str)
+		tprints(sa_handler_str);
+	else
+		printaddr(handler);
+}
+
 const char *
 signame(const int sig)
 {
@@ -320,14 +338,7 @@ decode_old_sigaction(struct tcb *tcp, long addr)
 	 * compiler from generating code to manipulate
 	 * __sa_handler we cast the function pointers to long. */
 	tprints("{");
-	if ((long)sa.__sa_handler == (long)SIG_ERR)
-		tprints("SIG_ERR");
-	else if ((long)sa.__sa_handler == (long)SIG_DFL)
-		tprints("SIG_DFL");
-	else if ((long)sa.__sa_handler == (long)SIG_IGN)
-		tprints("SIG_IGN");
-	else
-		printaddr((long) sa.__sa_handler);
+	print_sa_handler((unsigned long)sa.__sa_handler);
 	tprints(", ");
 #ifdef MIPS
 	tprintsigmask_addr("", sa.sa_mask);
@@ -360,32 +371,10 @@ SYS_FUNC(signal)
 	if (entering(tcp)) {
 		printsignal(tcp->u_arg[0]);
 		tprints(", ");
-		switch (tcp->u_arg[1]) {
-		case (long) SIG_ERR:
-			tprints("SIG_ERR");
-			break;
-		case (long) SIG_DFL:
-			tprints("SIG_DFL");
-			break;
-		case (long) SIG_IGN:
-			tprints("SIG_IGN");
-			break;
-		default:
-			printaddr(tcp->u_arg[1]);
-		}
+		print_sa_handler(tcp->u_arg[1]);
 		return 0;
-	}
-	else if (!syserror(tcp)) {
-		switch (tcp->u_rval) {
-		case (long) SIG_ERR:
-			tcp->auxstr = "SIG_ERR"; break;
-		case (long) SIG_DFL:
-			tcp->auxstr = "SIG_DFL"; break;
-		case (long) SIG_IGN:
-			tcp->auxstr = "SIG_IGN"; break;
-		default:
-			tcp->auxstr = NULL;
-		}
+	} else if (!syserror(tcp)) {
+		tcp->auxstr = get_sa_handler_str(tcp->u_rval);
 		return RVAL_HEX | RVAL_STR;
 	}
 	return 0;
@@ -560,14 +549,7 @@ decode_new_sigaction(struct tcb *tcp, long addr)
 	 * compiler from generating code to manipulate
 	 * __sa_handler we cast the function pointers to long. */
 	tprints("{");
-	if ((long)sa.__sa_handler == (long)SIG_ERR)
-		tprints("SIG_ERR");
-	else if ((long)sa.__sa_handler == (long)SIG_DFL)
-		tprints("SIG_DFL");
-	else if ((long)sa.__sa_handler == (long)SIG_IGN)
-		tprints("SIG_IGN");
-	else
-		printaddr((unsigned long) sa.__sa_handler);
+	print_sa_handler((unsigned long)sa.__sa_handler);
 	tprints(", ");
 	/*
 	 * Sigset size is in tcp->u_arg[4] (SPARC)
diff --git a/xlat/sa_handler_values.in b/xlat/sa_handler_values.in
new file mode 100644
index 0000000..e907611
--- /dev/null
+++ b/xlat/sa_handler_values.in
@@ -0,0 +1,4 @@
+#val_type unsigned long
+SIG_ERR
+SIG_DFL
+SIG_IGN
-- 
1.7.10.4





More information about the Strace-devel mailing list