[PATCH v2 3/6] dyxlat: building xlat dynamically

Dmitry V. Levin ldv at altlinux.org
Mon Jun 12 22:40:23 UTC 2017


On Sun, Jun 11, 2017 at 04:42:29PM +0900, Masatake YAMATO wrote:
> xlat tables are generating in built-time of strace.
> 
> printxval is suitable for printing `family' field of Netlink GENERIC
> protocol. However, contents of the xlat table cannot be defined in
> build-time because the values for the field are given by Linux kernel.
> 
> dyxlat functions are for building xlat in run-time. Decoding the
> field is the primary use case of functions but they can be used
> another purpose.
> 
> * defs.h (dyxlat_new, dyxlat_delete, dyxlat_may_add_pair): New functions
> declarations.

I think dyxlat_alloc/dyxlat_free would be more suitable than
dyxlat_new/dyxlat_delete, and dyxlat_add_pair would be better
than dyxlat_may_add_pair.

> --- a/defs.h
> +++ b/defs.h
> @@ -1018,4 +1018,11 @@ scno_is_valid(kernel_ulong_t scno)
>  #define PRI__u64 PRI__64"u"
>  #define PRI__x64 PRI__64"x"
>  
> +/* Dynamic Xlat */
> +struct dyxlat;
> +
> +struct dyxlat *dyxlat_new(int allocation);
> +void dyxlat_delete(struct dyxlat *dyxlat);
> +struct xlat *dyxlat_get(struct dyxlat *dyxlat);
> +void dyxlat_may_add_pair(struct dyxlat *dyxlat, uint64_t val, const char *str);
>  #endif /* !STRACE_DEFS_H */

Let's move these declarations closer to other xlat related prototypes.

> diff --git a/dyxlat.c b/dyxlat.c
> new file mode 100644
> index 0000000..98e505c
> --- /dev/null
> +++ b/dyxlat.c
> @@ -0,0 +1,119 @@
> +/*
> + * Copyright (c) 2017 The strace developers.
> + *
> + * 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.
> + */
> +
> +#include "defs.h"
> +
> +struct dyxlat {
> +	int used;
> +	int allocated;

Shouldn't these fields rather be of type size_t, or at least unsigned int?

> +	struct xlat *xlat;
> +};
> +
> +#define MARK_END(xlat)				\
> +	do {					\
> +		(xlat).val = 0;			\
> +		(xlat).str = 0;			\
> +	} while (0)
> +
> +static void
> +dyxlat_init(struct dyxlat *dyxlat, int allocation)
> +{
> +	dyxlat->used = 1;
> +	dyxlat->allocated = allocation ? allocation : 10;
> +	dyxlat->xlat = xcalloc(dyxlat->allocated, sizeof(struct xlat));
> +	MARK_END(dyxlat->xlat[0]);
> +}
> +
> +static void
> +dyxlat_fini(struct dyxlat *dyxlat)
> +{
> +	int i;
> +
> +	for (i = 0; i < dyxlat->used - 1; ++i) {
> +		free((void *)(dyxlat->xlat[i].str));
> +		dyxlat->xlat[i].str = NULL;
> +	}
> +
> +	free(dyxlat->xlat);
> +	dyxlat->xlat = NULL;
> +}
> +
> +struct dyxlat *
> +dyxlat_new(int allocation)
> +{
> +	struct dyxlat *dyxlat;
> +
> +	dyxlat = xmalloc(sizeof(*dyxlat));
> +	dyxlat_init(dyxlat, allocation);
> +	return dyxlat;
> +}
> +
> +void
> +dyxlat_delete(struct dyxlat *dyxlat)
> +{
> +	dyxlat_fini(dyxlat);
> +	free(dyxlat);
> +}

Let's merge dyxlat_init and dyxlat_new, as well as dyxlat_fini
and dyxlat_delete.

> +
> +struct xlat *
> +dyxlat_get(struct dyxlat *dyxlat)
> +{
> +	return dyxlat->xlat;
> +}
> +
> +static void
> +dyxlat_add_pair(struct dyxlat *dyxlat, uint64_t val, const char *str)
> +{
> +	dyxlat->used++;
> +
> +	if (dyxlat->used == dyxlat->allocated) {
> +		dyxlat->allocated *= 2;
> +		dyxlat->xlat = xreallocarray(dyxlat->xlat, dyxlat->allocated,
> +					     sizeof(struct xlat));
> +	}
> +
> +	dyxlat->xlat[dyxlat->used - 2].val = val;
> +	dyxlat->xlat[dyxlat->used - 2].str = xstrdup(str);
> +	MARK_END(dyxlat->xlat[dyxlat->used - 1]);
> +}
> +
> +void
> +dyxlat_may_add_pair(struct dyxlat *dyxlat, uint64_t val, const char *str)
> +{
> +	int i;
> +
> +	for (i = 0; i < dyxlat->used - 1; i++) {
> +		if (dyxlat->xlat[i].val == val) {
> +			if (strcmp(dyxlat->xlat[i].str, str) == 0)
> +				return;
> +
> +			free((void *)(dyxlat->xlat[i].str));
> +			dyxlat->xlat[i].str = xstrdup(str);
> +			return;
> +		}
> +	}
> +	dyxlat_add_pair(dyxlat, val, str);
> +}

Let's merge dyxlat_add_pair and dyxlat_may_add_pair.


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20170613/6adb86c6/attachment.bin>


More information about the Strace-devel mailing list