[PATCH v3 1/5] dyxlat: building xlat dynamically

Dmitry V. Levin ldv at altlinux.org
Tue Jun 13 21:55:03 UTC 2017


On Tue, Jun 13, 2017 at 05:26:41PM +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_alloc, dyxlat_free, dyxlat_add_pair): New functions
> declarations.
> (struct dyxlat): New opaque data type.
> 
> * dyxlat.c: New file.
> 
> Changes in version 3 (suggested by ldv):
> 
> 	* Rename dyxlat_new/dyxlat_delete to dyxlat_alloc/dyxlat_free.
> 	  Rename dyxlat_may_add_pair to dyxlat_may_add_pair.
> 
> 	* Move dyxlat declarations closer to other xlat related prototypes.
> 
> 	* Change the field types of dyxlat structure from int to size_t.
> 
> 	* Merge the file static functions in dyxlat.c to the exported functions
> 	  in the file.
> 
> Signed-off-by: Masatake YAMATO <yamato at redhat.com>
> ---
>  Makefile.am |   1 +
>  defs.h      |   6 ++++
>  dyxlat.c    | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 110 insertions(+)
>  create mode 100644 dyxlat.c
> 
> diff --git a/Makefile.am b/Makefile.am
> index 80f8a34..d004ac6 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -104,6 +104,7 @@ strace_SOURCES =	\
>  	dirent.c	\
>  	dirent64.c	\
>  	dm.c		\
> +	dyxlat.c	\
>  	empty.h		\
>  	epoll.c		\
>  	evdev.c		\
> diff --git a/defs.h b/defs.h
> index 6449bce..52f40b3 100644
> --- a/defs.h
> +++ b/defs.h
> @@ -504,6 +504,12 @@ extern enum sock_proto getfdproto(struct tcb *, int);
>  extern const char *xlookup(const struct xlat *, const uint64_t);
>  extern const char *xlat_search(const struct xlat *, const size_t, const uint64_t);
>  
> +struct dyxlat;
> +struct dyxlat *dyxlat_alloc(size_t allocation);

as "allocation" means number of elements (as nmemb argument of calloc),
let's call it "nmemb" or something like that.

> +void dyxlat_free(struct dyxlat *dyxlat);

Do you think "struct dyxlat *dyxlat" is more descriptive
than "struct dyxlat *"?

> +struct xlat *dyxlat_get(struct dyxlat *dyxlat);

This function doesn't change anything, let's change its prototype to
const struct xlat *dyxlat_get(const struct dyxlat *);

> +void dyxlat_add_pair(struct dyxlat *dyxlat, uint64_t val, const char *str);

It looks like in the primary use case (genl_parse_families_response)
it would be convenient to specify the string length as well.

I suggest changing this prototype to
void dyxlat_add_pair(struct dyxlat *, uint64_t val, const char *str, size_t len);

The implementation would invoke xstrndup instead of xstrdup
if len is not equal to zero.

> +
>  extern unsigned long get_pagesize(void);
>  extern int
>  string_to_uint_ex(const char *str, char **endptr,
> diff --git a/dyxlat.c b/dyxlat.c
> new file mode 100644
> index 0000000..c0952ed
> --- /dev/null
> +++ b/dyxlat.c
> @@ -0,0 +1,103 @@
> +/*
> + * 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 {
> +	size_t used;
> +	size_t allocated;
> +	struct xlat *xlat;
> +};
> +
> +#define MARK_END(xlat)				\
> +	do {					\
> +		(xlat).val = 0;			\
> +		(xlat).str = 0;			\
> +	} while (0)
> +
> +struct dyxlat *
> +dyxlat_alloc(size_t allocation)
> +{
> +	struct dyxlat *dyxlat;
> +
> +	dyxlat = xmalloc(sizeof(*dyxlat));
> +
> +	dyxlat->used = 1;
> +	dyxlat->allocated = allocation ? allocation : 10;

Why 10?

> +	dyxlat->xlat = xcalloc(dyxlat->allocated, sizeof(struct xlat));
> +	MARK_END(dyxlat->xlat[0]);
> +
> +	return dyxlat;
> +}
> +
> +void
> +dyxlat_free(struct dyxlat *dyxlat)
> +{
> +	size_t 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;
> +	free(dyxlat);
> +}
> +
> +struct xlat *
> +dyxlat_get(struct dyxlat *dyxlat)
> +{
> +	return dyxlat->xlat;
> +}
> +
> +void
> +dyxlat_add_pair(struct dyxlat *dyxlat, uint64_t val, const char *str)
> +{
> +	size_t 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->used++;
> +	if (dyxlat->used == dyxlat->allocated) {

This won't work properly if dyxlat_alloc was called with the number
of elements equal to 1.  Let's check for >= instead.

> +		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);

I think it would look a bit nicer if these two assignments were made
before dyxlat->used++.

> +	MARK_END(dyxlat->xlat[dyxlat->used - 1]);
> +}


-- 
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/20170614/d519e8f5/attachment.bin>


More information about the Strace-devel mailing list