Move err/mem subroutines to separate files
In order to allow usage of utility functions by other binaries included in the strace package (like the upcoming asinfo utility), these functions should be moved to separate files. * error_prints.h: New file. * xmalloc.h: Likewise. * defs.h: Include "xmalloc.h" and "error_prints.h". (error_msg, error_msg_and_die, error_msg_and_help, perror_msg, perror_msg_and_die): Move to error_prints.h. (xcalloc, xmalloc, xreallocarray, xstrdup, xstrndup): Move to xmalloc.h. * strace.c (die): Remove static quialifier to make visible by error_prints.c. (error_msg, error_msg_and_die, error_msg_and_help, perror_msg, perror_msg_and_die, verror_msg): Move ... * error_prints.c: ... to the new file. * xmalloc.c: Include "config.h", <stdlib.h>, <string.h>, "error_prints.h", and "xmalloc.h" instead of "defs.h". Use int instead of bool. Fix codestyle. * Makefile.am (strace_SOURCES): Add error_prints.c, error_prints.h, and xmalloc.h. Signed-off-by: Edgar Kaziakhmedov <edgar.kaziakhmedov@virtuozzo.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
This commit is contained in:
parent
35c006d93c
commit
b4f16886e7
@ -114,6 +114,8 @@ strace_SOURCES = \
|
||||
dyxlat.c \
|
||||
empty.h \
|
||||
epoll.c \
|
||||
error_prints.c \
|
||||
error_prints.h \
|
||||
evdev.c \
|
||||
eventfd.c \
|
||||
execve.c \
|
||||
@ -296,6 +298,7 @@ strace_SOURCES = \
|
||||
xlat.c \
|
||||
xlat.h \
|
||||
xmalloc.c \
|
||||
xmalloc.h \
|
||||
# end of strace_SOURCES
|
||||
|
||||
if USE_LIBUNWIND
|
||||
|
21
defs.h
21
defs.h
@ -53,11 +53,13 @@
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "kernel_types.h"
|
||||
#include "error_prints.h"
|
||||
#include "gcc_compat.h"
|
||||
#include "kernel_types.h"
|
||||
#include "macros.h"
|
||||
#include "mpers_type.h"
|
||||
#include "sysent.h"
|
||||
#include "xmalloc.h"
|
||||
|
||||
#ifndef HAVE_STRERROR
|
||||
const char *strerror(int);
|
||||
@ -384,23 +386,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 *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;
|
||||
char *xstrndup(const char *str, size_t n) ATTRIBUTE_MALLOC;
|
||||
|
||||
extern int read_int_from_file(const char *, int *);
|
||||
|
||||
extern void set_sortby(const char *);
|
||||
|
123
error_prints.c
Normal file
123
error_prints.c
Normal file
@ -0,0 +1,123 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "error_prints.h"
|
||||
|
||||
extern char *program_invocation_name;
|
||||
|
||||
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",
|
||||
program_invocation_name, msg, strerror(err_no));
|
||||
else
|
||||
fprintf(stderr, "%s: %s\n",
|
||||
program_invocation_name, msg);
|
||||
free(msg);
|
||||
} else {
|
||||
/* malloc in vasprintf failed, try it without malloc */
|
||||
fprintf(stderr, "%s: ", program_invocation_name);
|
||||
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",
|
||||
program_invocation_name);
|
||||
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();
|
||||
}
|
49
error_prints.h
Normal file
49
error_prints.h
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* This file contains error printing functions.
|
||||
* These functions can be used by various binaries included in the strace
|
||||
* package. Variable 'program_invocation_name' and function 'die()'
|
||||
* have to be defined globally.
|
||||
*
|
||||
* 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_ERROR_PRINTS_H
|
||||
#define STRACE_ERROR_PRINTS_H
|
||||
|
||||
#include "gcc_compat.h"
|
||||
|
||||
void die(void) ATTRIBUTE_NORETURN;
|
||||
|
||||
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;
|
||||
|
||||
#endif /* !STRACE_ERROR_PRINTS_H */
|
79
strace.c
79
strace.c
@ -286,7 +286,7 @@ Miscellaneous:\n\
|
||||
exit(0);
|
||||
}
|
||||
|
||||
static void ATTRIBUTE_NORETURN
|
||||
void ATTRIBUTE_NORETURN
|
||||
die(void)
|
||||
{
|
||||
if (strace_tracer_pid == getpid()) {
|
||||
@ -296,83 +296,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",
|
||||
program_invocation_name, msg, strerror(err_no));
|
||||
else
|
||||
fprintf(stderr, "%s: %s\n",
|
||||
program_invocation_name, msg);
|
||||
free(msg);
|
||||
} else {
|
||||
/* malloc in vasprintf failed, try it without malloc */
|
||||
fprintf(stderr, "%s: ", program_invocation_name);
|
||||
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",
|
||||
program_invocation_name);
|
||||
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)
|
||||
{
|
||||
|
30
xmalloc.c
30
xmalloc.c
@ -25,11 +25,20 @@
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "defs.h"
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
|
||||
static void die_out_of_memory(void)
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "error_prints.h"
|
||||
#include "xmalloc.h"
|
||||
|
||||
static void
|
||||
die_out_of_memory(void)
|
||||
{
|
||||
static bool recursed;
|
||||
static int recursed;
|
||||
|
||||
if (recursed)
|
||||
exit(1);
|
||||
@ -38,7 +47,8 @@ static void die_out_of_memory(void)
|
||||
error_msg_and_die("Out of memory");
|
||||
}
|
||||
|
||||
void *xmalloc(size_t size)
|
||||
void *
|
||||
xmalloc(size_t size)
|
||||
{
|
||||
void *p = malloc(size);
|
||||
|
||||
@ -48,7 +58,8 @@ void *xmalloc(size_t size)
|
||||
return p;
|
||||
}
|
||||
|
||||
void *xcalloc(size_t nmemb, size_t size)
|
||||
void *
|
||||
xcalloc(size_t nmemb, size_t size)
|
||||
{
|
||||
void *p = calloc(nmemb, size);
|
||||
|
||||
@ -60,7 +71,8 @@ void *xcalloc(size_t nmemb, size_t size)
|
||||
|
||||
#define HALF_SIZE_T (((size_t) 1) << (sizeof(size_t) * 4))
|
||||
|
||||
void *xreallocarray(void *ptr, size_t nmemb, size_t size)
|
||||
void *
|
||||
xreallocarray(void *ptr, size_t nmemb, size_t size)
|
||||
{
|
||||
size_t bytes = nmemb * size;
|
||||
|
||||
@ -76,7 +88,8 @@ void *xreallocarray(void *ptr, size_t nmemb, size_t size)
|
||||
return p;
|
||||
}
|
||||
|
||||
char *xstrdup(const char *str)
|
||||
char *
|
||||
xstrdup(const char *str)
|
||||
{
|
||||
char *p = strdup(str);
|
||||
|
||||
@ -86,7 +99,8 @@ char *xstrdup(const char *str)
|
||||
return p;
|
||||
}
|
||||
|
||||
char *xstrndup(const char *str, size_t n)
|
||||
char *
|
||||
xstrndup(const char *str, size_t n)
|
||||
{
|
||||
char *p;
|
||||
|
||||
|
47
xmalloc.h
Normal file
47
xmalloc.h
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* This file contains wrapper functions working with memory allocations,
|
||||
* they just terminate the program in case of memory allocation failure.
|
||||
* These functions can be used by various binaries included in the strace
|
||||
* package.
|
||||
*
|
||||
* 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_XMALLOC_H
|
||||
#define STRACE_XMALLOC_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include "gcc_compat.h"
|
||||
|
||||
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;
|
||||
char *xstrndup(const char *str, size_t n) ATTRIBUTE_MALLOC;
|
||||
|
||||
#endif /* !STRACE_XMALLOC_H */
|
Loading…
x
Reference in New Issue
Block a user