type.h revision b781916d24d6ee96842c818b5e18af31808d427d
1/* 2 * This file is part of ltrace. 3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc. 4 * Copyright (C) 1997-2009 Juan Cespedes 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of the 9 * License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 19 * 02110-1301 USA 20 */ 21 22#ifndef TYPE_H 23#define TYPE_H 24 25#include <stddef.h> 26#include "forward.h" 27#include "vect.h" 28 29enum arg_type { 30 ARGTYPE_VOID, 31 ARGTYPE_INT, 32 ARGTYPE_UINT, 33 ARGTYPE_LONG, 34 ARGTYPE_ULONG, 35 ARGTYPE_CHAR, 36 ARGTYPE_SHORT, 37 ARGTYPE_USHORT, 38 ARGTYPE_FLOAT, 39 ARGTYPE_DOUBLE, 40 ARGTYPE_STRING_N, /* String of known maxlen */ 41 ARGTYPE_ARRAY, /* Series of values in memory */ 42 ARGTYPE_ENUM, /* Enumeration */ 43 ARGTYPE_STRUCT, /* Structure of values */ 44 ARGTYPE_POINTER, /* Pointer to some other type */ 45}; 46 47struct arg_type_info { 48 enum arg_type type; 49 union { 50 struct vect entries; 51 52 /* ARGTYPE_ARRAY */ 53 struct { 54 struct arg_type_info *elt_type; 55 struct expr_node *length; 56 int own_info:1; 57 int own_length:1; 58 } array_info; 59 60 /* ARGTYPE_STRING_N */ 61 struct { 62 struct expr_node *length; 63 int own_length:1; 64 } string_n_info; 65 66 /* ARGTYPE_POINTER */ 67 struct { 68 struct arg_type_info *info; 69 int own_info:1; 70 } ptr_info; 71 } u; 72 73 struct lens *lens; 74 int own_lens; 75}; 76 77/* Return a type info for simple type TYPE (which shall not be array, 78 * struct, enum or pointer. Each call with the same TYPE yields the 79 * same arg_type_info pointer. */ 80struct arg_type_info *type_get_simple(enum arg_type type); 81 82/* Initialize INFO so it becomes ARGTYPE_ENUM. Returns 0 on success 83 * or negative value on failure. */ 84void type_init_enum(struct arg_type_info *info); 85 86/* Push another member of the enumeration, named KEY, with given 87 * VALUE. If OWN_KEY, KEY is owned and released after the type is 88 * destroyed. KEY is typed as const char *, but note that if 89 * OWN_KEY, this value will be freed. */ 90int type_enum_add(struct arg_type_info *info, 91 const char *key, int own_key, int value); 92 93/* Return number of enum elements of type INFO. */ 94size_t type_enum_size(struct arg_type_info *info); 95 96/* Look up enum key with given VALUE in INFO. */ 97const char *type_enum_get(struct arg_type_info *info, int value); 98 99/* Initialize INFO so it becomes ARGTYPE_STRUCT. The created 100 * structure contains no fields. Use type_struct_add to populate the 101 * structure. */ 102void type_init_struct(struct arg_type_info *info); 103 104/* Add a new field of type FIELD_INFO to a structure INFO. If OWN, 105 * the field type is owned and destroyed together with INFO. */ 106int type_struct_add(struct arg_type_info *info, 107 struct arg_type_info *field_info, int own); 108 109/* Get IDX-th field of structure type INFO. */ 110struct arg_type_info *type_struct_get(struct arg_type_info *info, size_t idx); 111 112/* Return number of fields of structure type INFO. */ 113size_t type_struct_size(struct arg_type_info *info); 114 115/* Initialize INFO so it becomes ARGTYPE_ARRAY. The element type is 116 * passed in ELEMENT_INFO, and array length in LENGTH_EXPR. If, 117 * respectively, OWN_INFO and OWN_LENGTH are true, the pointee and 118 * length are owned and destroyed together with INFO. */ 119void type_init_array(struct arg_type_info *info, 120 struct arg_type_info *element_info, int own_info, 121 struct expr_node *length_expr, int own_length); 122 123/* Initialize INFO so it becomes ARGTYPE_STRING_N. Length is passed 124 * in LENGTH_EXPR. If OWN_LENGTH is true, the length is owned and 125 * destroyed together with INFO. */ 126void type_init_string(struct arg_type_info *info, 127 struct expr_node *length, int own_length); 128 129/* Initialize INFO so it becomes ARGTYPE_POINTER. The pointee type is 130 * passed in POINTEE_INFO. If OWN_INFO, the pointee type is owned and 131 * destroyed together with INFO. */ 132void type_init_pointer(struct arg_type_info *info, 133 struct arg_type_info *pointee_info, int own_info); 134 135/* Release any memory associated with INFO. Doesn't free INFO 136 * itself. */ 137void type_destroy(struct arg_type_info *info); 138 139/* Compute a size of given type. Return (size_t)-1 for error. */ 140size_t type_sizeof(struct Process *proc, struct arg_type_info *type); 141 142/* Compute an alignment necessary for elements of this type. Return 143 * (size_t)-1 for error. */ 144size_t type_alignof(struct Process *proc, struct arg_type_info *type); 145 146/* Align value SZ to ALIGNMENT and return the result. */ 147size_t align(size_t sz, size_t alignment); 148 149/* Return ELT-th element of compound type TYPE. This is useful for 150 * arrays and structures. */ 151struct arg_type_info *type_element(struct arg_type_info *type, size_t elt); 152 153/* Compute an offset of EMT-th element of type TYPE. This works for 154 * arrays and structures. Return (size_t)-1 for error. */ 155size_t type_offsetof(struct Process *proc, 156 struct arg_type_info *type, size_t elt); 157 158#endif /* TYPE_H */ 159