type.h revision e3f4a984db115979e09414b7281da98399dd8949
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_ARRAY, /* Series of values in memory */ 41 ARGTYPE_ENUM, /* Enumeration */ 42 ARGTYPE_STRUCT, /* Structure of values */ 43 ARGTYPE_POINTER, /* Pointer to some other type */ 44}; 45 46struct arg_type_info { 47 enum arg_type type; 48 union { 49 struct vect entries; 50 51 /* ARGTYPE_ARRAY */ 52 struct { 53 struct arg_type_info *elt_type; 54 struct expr_node *length; 55 int own_info:1; 56 int own_length:1; 57 } array_info; 58 59 /* ARGTYPE_POINTER */ 60 struct { 61 struct arg_type_info *info; 62 int own_info:1; 63 } ptr_info; 64 } u; 65 66 struct lens *lens; 67 int own_lens; 68}; 69 70/* Return a type info for simple type TYPE (which shall not be array, 71 * struct, enum or pointer. Each call with the same TYPE yields the 72 * same arg_type_info pointer. */ 73struct arg_type_info *type_get_simple(enum arg_type type); 74 75/* Initialize INFO so it becomes ARGTYPE_ENUM. Returns 0 on success 76 * or negative value on failure. */ 77void type_init_enum(struct arg_type_info *info); 78 79/* Push another member of the enumeration, named KEY, with given 80 * VALUE. If OWN_KEY, KEY is owned and released after the type is 81 * destroyed. KEY is typed as const char *, but note that if 82 * OWN_KEY, this value will be freed. */ 83int type_enum_add(struct arg_type_info *info, 84 const char *key, int own_key, int value); 85 86/* Return number of enum elements of type INFO. */ 87size_t type_enum_size(struct arg_type_info *info); 88 89/* Look up enum key with given VALUE in INFO. */ 90const char *type_enum_get(struct arg_type_info *info, int value); 91 92/* Initialize INFO so it becomes ARGTYPE_STRUCT. The created 93 * structure contains no fields. Use type_struct_add to populate the 94 * structure. */ 95void type_init_struct(struct arg_type_info *info); 96 97/* Add a new field of type FIELD_INFO to a structure INFO. If OWN, 98 * the field type is owned and destroyed together with INFO. */ 99int type_struct_add(struct arg_type_info *info, 100 struct arg_type_info *field_info, int own); 101 102/* Get IDX-th field of structure type INFO. */ 103struct arg_type_info *type_struct_get(struct arg_type_info *info, size_t idx); 104 105/* Return number of fields of structure type INFO. */ 106size_t type_struct_size(struct arg_type_info *info); 107 108/* Initialize INFO so it becomes ARGTYPE_ARRAY. The element type is 109 * passed in ELEMENT_INFO, and array length in LENGTH_EXPR. If, 110 * respectively, OWN_INFO and OWN_LENGTH are true, the pointee and 111 * length are owned and destroyed together with INFO. */ 112void type_init_array(struct arg_type_info *info, 113 struct arg_type_info *element_info, int own_info, 114 struct expr_node *length_expr, int own_length); 115 116/* Initialize INFO so it becomes ARGTYPE_POINTER. The pointee type is 117 * passed in POINTEE_INFO. If OWN_INFO, the pointee type is owned and 118 * destroyed together with INFO. */ 119void type_init_pointer(struct arg_type_info *info, 120 struct arg_type_info *pointee_info, int own_info); 121 122/* Release any memory associated with INFO. Doesn't free INFO 123 * itself. */ 124void type_destroy(struct arg_type_info *info); 125 126/* Compute a size of given type. Return (size_t)-1 for error. */ 127size_t type_sizeof(struct Process *proc, struct arg_type_info *type); 128 129/* Compute an alignment necessary for elements of this type. Return 130 * (size_t)-1 for error. */ 131size_t type_alignof(struct Process *proc, struct arg_type_info *type); 132 133/* Align value SZ to ALIGNMENT and return the result. */ 134size_t align(size_t sz, size_t alignment); 135 136/* Return ELT-th element of compound type TYPE. This is useful for 137 * arrays and structures. */ 138struct arg_type_info *type_element(struct arg_type_info *type, size_t elt); 139 140/* Compute an offset of EMT-th element of type TYPE. This works for 141 * arrays and structures. Return (size_t)-1 for error. */ 142size_t type_offsetof(struct Process *proc, 143 struct arg_type_info *type, size_t elt); 144 145#endif /* TYPE_H */ 146