type.h revision 94078ecce3a103c28457e6f90f1e5b0dacc61146
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_UNKNOWN = -1,
31	ARGTYPE_VOID,
32	ARGTYPE_INT,
33	ARGTYPE_UINT,
34	ARGTYPE_LONG,
35	ARGTYPE_ULONG,
36	ARGTYPE_OCTAL,
37	ARGTYPE_CHAR,
38	ARGTYPE_SHORT,
39	ARGTYPE_USHORT,
40	ARGTYPE_FLOAT,		/* float value, may require index */
41	ARGTYPE_DOUBLE,		/* double value, may require index */
42	ARGTYPE_ADDR,
43	ARGTYPE_FILE,
44	ARGTYPE_FORMAT,		/* printf-like format */
45	ARGTYPE_STRING,		/* NUL-terminated string */
46	ARGTYPE_STRING_N,	/* String of known maxlen */
47	ARGTYPE_ARRAY,		/* Series of values in memory */
48	ARGTYPE_ENUM,		/* Enumeration */
49	ARGTYPE_STRUCT,		/* Structure of values */
50	ARGTYPE_POINTER,	/* Pointer to some other type */
51	ARGTYPE_COUNT		/* number of ARGTYPE_* values */
52};
53
54struct arg_type_info {
55	enum arg_type type;
56	union {
57		struct vect entries;
58
59		/* ARGTYPE_ENUM */
60		struct {
61			size_t entries;
62			char **keys;
63			int *values;
64		} enum_info;
65
66		/* ARGTYPE_ARRAY */
67		struct {
68			struct arg_type_info *elt_type;
69			struct expr_node *length;
70			int own_info:1;
71			int own_length:1;
72		} array_info;
73
74		/* ARGTYPE_STRING_N */
75		struct {
76			struct expr_node *length;
77			int own_length:1;
78		} string_n_info;
79
80		/* ARGTYPE_POINTER */
81		struct {
82			struct arg_type_info *info;
83			int own_info:1;
84		} ptr_info;
85
86		/* ARGTYPE_FLOAT */
87		struct {
88			size_t float_index;
89		} float_info;
90
91		/* ARGTYPE_DOUBLE */
92		struct {
93			size_t float_index;
94		} double_info;
95	} u;
96};
97
98/* Return a type info for simple type TYPE (which shall not be array,
99 * struct, enum or pointer.  Each call with the same TYPE yields the
100 * same arg_type_info pointer.  */
101struct arg_type_info *type_get_simple(enum arg_type type);
102
103/* Initialize INFO so it becomes ARGTYPE_ENUM.  Returns 0 on success
104 * or negative value on failure.  */
105void type_init_enum(struct arg_type_info *info);
106
107/* Push another member of the enumeration, named KEY, with given
108 * VALUE.  If OWN_KEY, KEY is owned and released after the type is
109 * destroyed.  KEY is typed as const char *, but note that if
110 * OWN_KEY, this value will be freed.  */
111int type_enum_add(struct arg_type_info *info,
112		  const char *key, int own_key, int value);
113
114/* Return number of enum elements of type INFO.  */
115size_t type_enum_size(struct arg_type_info *info);
116
117/* Look up enum key with given VALUE in INFO.  */
118const char *type_enum_get(struct arg_type_info *info, int value);
119
120/* Initialize INFO so it becomes ARGTYPE_STRUCT.  The created
121 * structure contains no fields.  Use type_struct_add to populate the
122 * structure.  */
123void type_init_struct(struct arg_type_info *info);
124
125/* Add a new field of type FIELD_INFO to a structure INFO.  If OWN,
126 * the field type is owned and destroyed together with INFO.  */
127int type_struct_add(struct arg_type_info *info,
128		    struct arg_type_info *field_info, int own);
129
130/* Get IDX-th field of structure type INFO.  */
131struct arg_type_info *type_struct_get(struct arg_type_info *info, size_t idx);
132
133/* Return number of fields of structure type INFO.  */
134size_t type_struct_size(struct arg_type_info *info);
135
136/* Initialize INFO so it becomes ARGTYPE_ARRAY.  The element type is
137 * passed in ELEMENT_INFO, and array length in LENGTH_EXPR.  If,
138 * respectively, OWN_INFO and OWN_LENGTH are true, the pointee and
139 * length are owned and destroyed together with INFO.  */
140void type_init_array(struct arg_type_info *info,
141		     struct arg_type_info *element_info, int own_info,
142		     struct expr_node *length, int own_length);
143
144/* Initialize INFO so it becomes ARGTYPE_POINTER.  The pointee type is
145 * passed in POINTEE_INFO.  If OWN_INFO, the pointee type is owned and
146 * destroyed together with INFO.  */
147void type_init_pointer(struct arg_type_info *info,
148		       struct arg_type_info *pointee_info, int own_info);
149
150/* Release any memory associated with INFO.  Doesn't free INFO
151 * itself.  */
152void type_destroy(struct arg_type_info *info);
153
154/* Compute a size of given type.  Return (size_t)-1 for error.  */
155size_t type_sizeof(struct Process *proc, struct arg_type_info *type);
156
157/* Compute an alignment necessary for elements of this type.  Return
158 * (size_t)-1 for error.  */
159size_t type_alignof(struct Process *proc, struct arg_type_info *type);
160
161/* Align value SZ to ALIGNMENT and return the result.  */
162size_t align(size_t sz, size_t alignment);
163
164/* Return ELT-th element of compound type TYPE.  This is useful for
165 * arrays and structures.  */
166struct arg_type_info *type_element(struct arg_type_info *type, size_t elt);
167
168/* Compute an offset of EMT-th element of type TYPE.  This works for
169 * arrays and structures.  Return (size_t)-1 for error.  */
170size_t type_offsetof(struct Process *proc,
171		     struct arg_type_info *type, size_t elt);
172
173struct arg_type_info *lookup_prototype(enum arg_type at);
174
175#endif /* TYPE_H */
176