Blame lens_enum.c

Packit 0021fb
/*
Packit 0021fb
 * This file is part of ltrace.
Packit 0021fb
 * Copyright (C) 2011, 2012 Petr Machata, Red Hat Inc.
Packit 0021fb
 *
Packit 0021fb
 * This program is free software; you can redistribute it and/or
Packit 0021fb
 * modify it under the terms of the GNU General Public License as
Packit 0021fb
 * published by the Free Software Foundation; either version 2 of the
Packit 0021fb
 * License, or (at your option) any later version.
Packit 0021fb
 *
Packit 0021fb
 * This program is distributed in the hope that it will be useful, but
Packit 0021fb
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0021fb
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0021fb
 * General Public License for more details.
Packit 0021fb
 *
Packit 0021fb
 * You should have received a copy of the GNU General Public License
Packit 0021fb
 * along with this program; if not, write to the Free Software
Packit 0021fb
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
Packit 0021fb
 * 02110-1301 USA
Packit 0021fb
 */
Packit 0021fb
Packit 0021fb
#include <stdlib.h>
Packit 0021fb
#include <assert.h>
Packit 0021fb
#include <string.h>
Packit 0021fb
Packit 0021fb
#include "lens_enum.h"
Packit 0021fb
#include "lens_default.h"
Packit 0021fb
#include "value.h"
Packit 0021fb
#include "sysdep.h"
Packit 0021fb
#include "type.h"
Packit 0021fb
Packit 0021fb
struct enum_entry {
Packit 0021fb
	char *key;
Packit 0021fb
	int own_key;
Packit 0021fb
	struct value *value;
Packit 0021fb
	int own_value;
Packit 0021fb
};
Packit 0021fb
Packit 0021fb
static void
Packit 0021fb
enum_entry_dtor(struct enum_entry *entry, void *data)
Packit 0021fb
{
Packit 0021fb
	if (entry->own_key)
Packit 0021fb
		free(entry->key);
Packit 0021fb
	if (entry->own_value) {
Packit 0021fb
		value_destroy(entry->value);
Packit 0021fb
		free(entry->value);
Packit 0021fb
	}
Packit 0021fb
}
Packit 0021fb
Packit 0021fb
static void
Packit 0021fb
enum_lens_destroy_cb(struct lens *lens)
Packit 0021fb
{
Packit 0021fb
	struct enum_lens *self = (void *)lens;
Packit 0021fb
Packit 0021fb
	VECT_DESTROY(&self->entries, struct enum_entry,
Packit 0021fb
		     enum_entry_dtor, NULL);
Packit 0021fb
}
Packit 0021fb
Packit 0021fb
enum {
Packit 0021fb
#ifdef ARCH_ENDIAN_BIG
Packit 0021fb
	big_endian = 1,
Packit 0021fb
#elif defined (ARCH_ENDIAN_LITTLE)
Packit 0021fb
	big_endian = 0,
Packit 0021fb
#else
Packit 0021fb
# error Undefined endianness.
Packit 0021fb
#endif
Packit 0021fb
};
Packit 0021fb
Packit 0021fb
/* Returns 0 if they are not equal, >0 if they are, and <0 if there
Packit 0021fb
 * was an error.  */
Packit 0021fb
static int
Packit 0021fb
enum_values_equal(struct value *inf_value, struct value *enum_value,
Packit 0021fb
		  struct value_dict *arguments)
Packit 0021fb
{
Packit 0021fb
	/* Width may not match between what's defined in config file
Packit 0021fb
	 * and what arrives from the back end.  Typically, if there is
Packit 0021fb
	 * a mismatch, the config file size will be larger, as we are
Packit 0021fb
	 * in a situation where 64-bit tracer traces 32-bit process.
Packit 0021fb
	 * But opposite situation can occur e.g. on PPC, where it's
Packit 0021fb
	 * apparently possible for 32-bit tracer to trace 64-bit
Packit 0021fb
	 * inferior, or hypothetically in a x32/x86_64 situation.  */
Packit 0021fb
Packit 0021fb
	unsigned char *inf_data = value_get_data(inf_value, arguments);
Packit 0021fb
	size_t inf_sz = value_size(inf_value, arguments);
Packit 0021fb
	if (inf_data == NULL || inf_sz == (size_t)-1)
Packit 0021fb
		return -1;
Packit 0021fb
Packit 0021fb
	assert(inf_value->type->type == enum_value->type->type);
Packit 0021fb
Packit 0021fb
	unsigned char *enum_data = value_get_data(enum_value, arguments);
Packit 0021fb
	size_t enum_sz = value_size(enum_value, arguments);
Packit 0021fb
	if (enum_data == NULL || enum_sz == (size_t)-1)
Packit 0021fb
		return -1;
Packit 0021fb
Packit 0021fb
	size_t sz = enum_sz > inf_sz ? inf_sz : enum_sz;
Packit 0021fb
Packit 0021fb
	if (big_endian)
Packit 0021fb
		return memcmp(enum_data + enum_sz - sz,
Packit 0021fb
			      inf_data + inf_sz - sz, sz) == 0;
Packit 0021fb
	else
Packit 0021fb
		return memcmp(enum_data, inf_data, sz) == 0;
Packit 0021fb
}
Packit 0021fb
Packit 0021fb
static const char *
Packit 0021fb
enum_get(struct enum_lens *lens, struct value *value,
Packit 0021fb
	 struct value_dict *arguments)
Packit 0021fb
{
Packit 0021fb
	size_t i;
Packit 0021fb
	for (i = 0; i < vect_size(&lens->entries); ++i) {
Packit 0021fb
		struct enum_entry *entry = VECT_ELEMENT(&lens->entries,
Packit 0021fb
							struct enum_entry, i);
Packit 0021fb
		int st = enum_values_equal(value, entry->value, arguments);
Packit 0021fb
		if (st < 0)
Packit 0021fb
			return NULL;
Packit 0021fb
		else if (st != 0)
Packit 0021fb
			return entry->key;
Packit 0021fb
	}
Packit 0021fb
	return NULL;
Packit 0021fb
}
Packit 0021fb
Packit 0021fb
static int
Packit 0021fb
enum_lens_format_cb(struct lens *lens, FILE *stream,
Packit 0021fb
		    struct value *value, struct value_dict *arguments)
Packit 0021fb
{
Packit 0021fb
	struct enum_lens *self = (void *)lens;
Packit 0021fb
Packit 0021fb
	const char *name = enum_get(self, value, arguments);
Packit 0021fb
	if (name != NULL)
Packit 0021fb
		return fprintf(stream, "%s", name);
Packit 0021fb
Packit 0021fb
	return lens_format(&default_lens, stream, value, arguments);
Packit 0021fb
}
Packit 0021fb
Packit 0021fb
Packit 0021fb
void
Packit 0021fb
lens_init_enum(struct enum_lens *lens)
Packit 0021fb
{
Packit 0021fb
	*lens = (struct enum_lens){
Packit 0021fb
		{
Packit 0021fb
			.format_cb = enum_lens_format_cb,
Packit 0021fb
			.destroy_cb = enum_lens_destroy_cb,
Packit 0021fb
		},
Packit 0021fb
	};
Packit 0021fb
	VECT_INIT(&lens->entries, struct enum_entry);
Packit 0021fb
}
Packit 0021fb
Packit 0021fb
int
Packit 0021fb
lens_enum_add(struct enum_lens *lens,
Packit 0021fb
	      const char *key, int own_key,
Packit 0021fb
	      struct value *value, int own_value)
Packit 0021fb
{
Packit 0021fb
	struct enum_entry entry = { (char *)key, own_key, value, own_value };
Packit 0021fb
	return VECT_PUSHBACK(&lens->entries, &entry);
Packit 0021fb
}
Packit 0021fb
Packit 0021fb
size_t
Packit 0021fb
lens_enum_size(struct enum_lens *lens)
Packit 0021fb
{
Packit 0021fb
	return vect_size(&lens->entries);
Packit 0021fb
}