[PATCH] Replace errs and mem subroutines to separate files

Edgar Kaziahmedov edos at frtk.ru
Thu Jun 22 22:45:57 UTC 2017


From: Edgar Kaziahmedov <edos at linux.com>

According to the current state of affairs, strace.c contains
a lot of functions, which could be grouped into specially
designed files, for instance, file with functions that print
errors. Also it could be useful for the future, because other
binaries, included to the strace package, will use functions for
printing errors, thus it would avoid the duplication of code.
Now, all common prototypes is placed in common.h.
NOTE: asinfo also needs these subroutines.

* Makefile.am (strace_SOURCES): Add common.h and error_prints.c.
* common.h: Move part of system includes from defs.h.
(error_msg, perror_msg, perror_msg_and_die, error_msg_and_help,
error_msg_and_die, die_out_of_memory, xcalloc, xmalloc, xreallocarray,
xstrdup, ARRAY_SIZE): Move from defs.h.
* defs.h: Include "common.h". Move part of system includes to common.h.
(error_msg, perror_msg, perror_msg_and_die, error_msg_and_help,
error_msg_and_die, die_out_of_memory, xcalloc, xmalloc, xreallocarray,
xstrdup, ARRAY_SIZE): Move to common.h.
* error_prints.c: Include "stdarg.h", "errno.h" and "common.h".
(verror_msg, error_msg, perror_msg, perror_msg_and_die,
error_msg_and_help, error_msg_and_die, die_out_of_memory): Move from
strace.c.
* strace.c (verror_msg, error_msg, perror_msg, perror_msg_and_die,
error_msg_and_help, error_msg_and_die, die_out_of_memory): Move to
error_prints.c.
(progname): Remove static to make visible for error_prints.c.
(die): Likewise.
* xmalloc.c: Remove "defs.h". Add "common.h".

Signed-off-by: Edgar Kaziahmedov <edos at linux.com>
---
 Makefile.am    |   2 +
 common.h       |  61 ++++++++++++++++++++++++++++++
 defs.h         |  28 +-------------
 error_prints.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 strace.c       |  78 +-------------------------------------
 xmalloc.c      |   2 +-
 6 files changed, 182 insertions(+), 104 deletions(-)
 create mode 100644 common.h
 create mode 100644 error_prints.c

diff --git a/Makefile.am b/Makefile.am
index 3d6eba1e..a670befe 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -97,6 +97,7 @@ strace_SOURCES =	\
 	chdir.c		\
 	chmod.c		\
 	clone.c		\
+	common.h	\
 	copy_file_range.c \
 	count.c		\
 	defs.h		\
@@ -106,6 +107,7 @@ strace_SOURCES =	\
 	dm.c		\
 	empty.h		\
 	epoll.c		\
+	error_prints.c	\
 	evdev.c		\
 	eventfd.c	\
 	execve.c	\
diff --git a/common.h b/common.h
new file mode 100644
index 00000000..84176b5d
--- /dev/null
+++ b/common.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2001-2017 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef STRACE_COMMON_H
+#define STRACE_COMMON_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "kernel_types.h"
+#include "gcc_compat.h"
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]) + MUST_BE_ARRAY(a))
+
+void error_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
+void perror_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
+void perror_msg_and_die(const char *fmt, ...)
+	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
+void error_msg_and_help(const char *fmt, ...)
+	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
+void error_msg_and_die(const char *fmt, ...)
+	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
+void die_out_of_memory(void) ATTRIBUTE_NORETURN;
+
+void *xcalloc(size_t nmemb, size_t size)
+	ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1, 2));
+void *xmalloc(size_t size) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
+void *xreallocarray(void *ptr, size_t nmemb, size_t size)
+	ATTRIBUTE_ALLOC_SIZE((2, 3));
+char *xstrdup(const char *str) ATTRIBUTE_MALLOC;
+
+#endif /* !STRACE_COMMON_H */
diff --git a/defs.h b/defs.h
index f731fa8c..e03b5038 100644
--- a/defs.h
+++ b/defs.h
@@ -36,26 +36,19 @@
 #endif
 
 #include <features.h>
-#include <stdbool.h>
-#include <stdint.h>
 #include <inttypes.h>
 #include <sys/types.h>
 #include <stddef.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
 /* Open-coding isprint(ch) et al proved more efficient than calling
  * generalized libc interface. We don't *want* to do non-ASCII anyway.
  */
 /* #include <ctype.h> */
-#include <string.h>
 #include <errno.h>
 #include <time.h>
 #include <sys/time.h>
 
-#include "kernel_types.h"
+#include "common.h"
 #include "mpers_type.h"
-#include "gcc_compat.h"
 #include "sysent.h"
 
 #ifndef HAVE_STRERROR
@@ -75,8 +68,6 @@ extern char *stpcpy(char *dst, const char *src);
 	(offsetof(type, member) + sizeof(((type *)NULL)->member))
 #endif
 
-#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]) + MUST_BE_ARRAY(a))
-
 /* macros */
 #ifndef MAX
 # define MAX(a, b)		(((a) > (b)) ? (a) : (b))
@@ -386,23 +377,6 @@ extern unsigned os_release;
 #undef KERNEL_VERSION
 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
 
-void error_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
-void perror_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
-void error_msg_and_die(const char *fmt, ...)
-	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
-void error_msg_and_help(const char *fmt, ...)
-	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
-void perror_msg_and_die(const char *fmt, ...)
-	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
-void die_out_of_memory(void) ATTRIBUTE_NORETURN;
-
-void *xmalloc(size_t size) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
-void *xcalloc(size_t nmemb, size_t size)
-	ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1, 2));
-void *xreallocarray(void *ptr, size_t nmemb, size_t size)
-	ATTRIBUTE_ALLOC_SIZE((2, 3));
-char *xstrdup(const char *str) ATTRIBUTE_MALLOC;
-
 extern int read_int_from_file(const char *, int *);
 
 extern void set_sortby(const char *);
diff --git a/error_prints.c b/error_prints.c
new file mode 100644
index 00000000..935587dc
--- /dev/null
+++ b/error_prints.c
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 1999-2017 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _GNU_SOURCE
+# define _GNU_SOURCE 1
+#endif
+
+#include <stdarg.h>
+#include <errno.h>
+
+#include "common.h"
+
+/* progname has to be defined globally */
+extern const char* progname;
+/* die() has to be implemented in the file, which
+ * is going to include this source */
+extern void ATTRIBUTE_NORETURN die(void);
+
+static void verror_msg(int err_no, const char *fmt, va_list p)
+{
+	char *msg;
+
+	fflush(NULL);
+
+	/* We want to print entire message with single fprintf to ensure
+	 * message integrity if stderr is shared with other programs.
+	 * Thus we use vasprintf + single fprintf.
+	 */
+	msg = NULL;
+	if (vasprintf(&msg, fmt, p) >= 0) {
+		if (err_no)
+			fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no));
+		else
+			fprintf(stderr, "%s: %s\n", progname, msg);
+		free(msg);
+	} else {
+		/* malloc in vasprintf failed, try it without malloc */
+		fprintf(stderr, "%s: ", progname);
+		vfprintf(stderr, fmt, p);
+		if (err_no)
+			fprintf(stderr, ": %s\n", strerror(err_no));
+		else
+			putc('\n', stderr);
+	}
+	/* We don't switch stderr to buffered, thus fprintf(stderr)
+	 * always flushes its output and this is not necessary: */
+	/* fflush(stderr); */
+}
+
+void error_msg(const char *fmt, ...)
+{
+	va_list p;
+	va_start(p, fmt);
+	verror_msg(0, fmt, p);
+	va_end(p);
+}
+
+void error_msg_and_die(const char *fmt, ...)
+{
+	va_list p;
+	va_start(p, fmt);
+	verror_msg(0, fmt, p);
+	die();
+}
+
+void error_msg_and_help(const char *fmt, ...)
+{
+	if (fmt != NULL) {
+		va_list p;
+		va_start(p, fmt);
+		verror_msg(0, fmt, p);
+	}
+	fprintf(stderr, "Try '%s -h' for more information.\n", progname);
+	die();
+}
+
+void perror_msg(const char *fmt, ...)
+{
+	va_list p;
+	va_start(p, fmt);
+	verror_msg(errno, fmt, p);
+	va_end(p);
+}
+
+void perror_msg_and_die(const char *fmt, ...)
+{
+	va_list p;
+	va_start(p, fmt);
+	verror_msg(errno, fmt, p);
+	die();
+}
diff --git a/strace.c b/strace.c
index 42650ab5..7b13b391 100644
--- a/strace.c
+++ b/strace.c
@@ -151,7 +151,7 @@ static struct tcb *current_tcp;
 
 static struct tcb **tcbtab;
 static unsigned int nprocs, tcbtabsize;
-static const char *progname;
+const char *progname;
 
 unsigned os_release; /* generated from uname()'s u.release */
 
@@ -275,7 +275,7 @@ Miscellaneous:\n\
 	exit(0);
 }
 
-static void ATTRIBUTE_NORETURN
+void ATTRIBUTE_NORETURN
 die(void)
 {
 	if (strace_tracer_pid == getpid()) {
@@ -285,80 +285,6 @@ die(void)
 	exit(1);
 }
 
-static void verror_msg(int err_no, const char *fmt, va_list p)
-{
-	char *msg;
-
-	fflush(NULL);
-
-	/* We want to print entire message with single fprintf to ensure
-	 * message integrity if stderr is shared with other programs.
-	 * Thus we use vasprintf + single fprintf.
-	 */
-	msg = NULL;
-	if (vasprintf(&msg, fmt, p) >= 0) {
-		if (err_no)
-			fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no));
-		else
-			fprintf(stderr, "%s: %s\n", progname, msg);
-		free(msg);
-	} else {
-		/* malloc in vasprintf failed, try it without malloc */
-		fprintf(stderr, "%s: ", progname);
-		vfprintf(stderr, fmt, p);
-		if (err_no)
-			fprintf(stderr, ": %s\n", strerror(err_no));
-		else
-			putc('\n', stderr);
-	}
-	/* We don't switch stderr to buffered, thus fprintf(stderr)
-	 * always flushes its output and this is not necessary: */
-	/* fflush(stderr); */
-}
-
-void error_msg(const char *fmt, ...)
-{
-	va_list p;
-	va_start(p, fmt);
-	verror_msg(0, fmt, p);
-	va_end(p);
-}
-
-void error_msg_and_die(const char *fmt, ...)
-{
-	va_list p;
-	va_start(p, fmt);
-	verror_msg(0, fmt, p);
-	die();
-}
-
-void error_msg_and_help(const char *fmt, ...)
-{
-	if (fmt != NULL) {
-		va_list p;
-		va_start(p, fmt);
-		verror_msg(0, fmt, p);
-	}
-	fprintf(stderr, "Try '%s -h' for more information.\n", progname);
-	die();
-}
-
-void perror_msg(const char *fmt, ...)
-{
-	va_list p;
-	va_start(p, fmt);
-	verror_msg(errno, fmt, p);
-	va_end(p);
-}
-
-void perror_msg_and_die(const char *fmt, ...)
-{
-	va_list p;
-	va_start(p, fmt);
-	verror_msg(errno, fmt, p);
-	die();
-}
-
 static void
 error_opt_arg(int opt, const char *arg)
 {
diff --git a/xmalloc.c b/xmalloc.c
index 11589270..622387ef 100644
--- a/xmalloc.c
+++ b/xmalloc.c
@@ -25,7 +25,7 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "defs.h"
+#include "common.h"
 
 void die_out_of_memory(void)
 {
-- 
2.11.0





More information about the Strace-devel mailing list