type.h revision 000e31195ad4ad30a0c80c93ab57a424e7d8d918
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			size_t elt_size;
70			int len_spec;
71			int own_info:1;
72		} array_info;
73
74		/* ARGTYPE_STRING_N */
75		struct {
76			int size_spec;
77		} string_n_info;
78
79		/* ARGTYPE_STRUCT */
80		struct {
81			struct arg_type_info **fields;
82			size_t * offset;
83			size_t size;
84		} struct_info;
85
86		/* ARGTYPE_POINTER */
87		struct {
88			struct arg_type_info *info;
89			int own_info:1;
90		} ptr_info;
91
92		/* ARGTYPE_FLOAT */
93		struct {
94			size_t float_index;
95		} float_info;
96
97		/* ARGTYPE_DOUBLE */
98		struct {
99			size_t float_index;
100		} double_info;
101	} u;
102};
103
104/* Return a type info for simple type TYPE (which shall not be array,
105 * struct, enum or pointer.  Each call with the same TYPE yields the
106 * same arg_type_info pointer.  */
107struct arg_type_info *type_get_simple(enum arg_type type);
108
109/* Initialize INFO so it becomes ARGTYPE_ENUM.  Returns 0 on success
110 * or negative value on failure.  */
111void type_init_enum(struct arg_type_info *info);
112
113/* Push another member of the enumeration, named KEY, with given
114 * VALUE.  If OWN_KEY, KEY is owned and released after the type is
115 * destroyed.  KEY is typed as const char *, but note that if
116 * OWN_KEY, this value will be freed.  */
117int type_enum_add(struct arg_type_info *info,
118		  const char *key, int own_key, int value);
119
120/* Return number of enum elements of type INFO.  */
121size_t type_enum_size(struct arg_type_info *info);
122
123/* Look up enum key with given VALUE in INFO.  */
124const char *type_enum_get(struct arg_type_info *info, int value);
125
126/* Initialize INFO so it becomes ARGTYPE_STRUCT.  The created
127 * structure contains no fields.  Use type_struct_add to populate the
128 * structure.  */
129void type_init_struct(struct arg_type_info *info);
130
131/* Add a new field of type FIELD_INFO to a structure INFO.  If OWN,
132 * the field type is owned and destroyed together with INFO.  */
133int type_struct_add(struct arg_type_info *info,
134		    struct arg_type_info *field_info, int own);
135
136/* Get IDX-th field of structure type INFO.  */
137struct arg_type_info *type_struct_get(struct arg_type_info *info, size_t idx);
138
139/* Return number of fields of structure type INFO.  */
140size_t type_struct_size(struct arg_type_info *info);
141
142/* Initialize INFO so it becomes ARGTYPE_ARRAY.  The element type is
143 * passed in ELEMENT_INFO, and array length in LENGTH_EXPR.  If,
144 * respectively, OWN_INFO and OWN_LENGTH are true, the pointee and
145 * length are owned and destroyed together with INFO.  */
146void type_init_array(struct arg_type_info *info,
147		     struct arg_type_info *element_info, int own_info,
148		     int len_spec);
149
150/* Initialize INFO so it becomes ARGTYPE_POINTER.  The pointee type is
151 * passed in POINTEE_INFO.  If OWN_INFO, the pointee type is owned and
152 * destroyed together with INFO.  */
153void type_init_pointer(struct arg_type_info *info,
154		       struct arg_type_info *pointee_info, int own_info);
155
156/* Release any memory associated with INFO.  Doesn't free INFO
157 * itself.  */
158void type_destroy(struct arg_type_info *info);
159
160/* Compute a size of given type.  Return (size_t)-1 for error.  */
161size_t type_sizeof(struct Process *proc, struct arg_type_info *type);
162
163/* Compute an alignment necessary for elements of this type.  Return
164 * (size_t)-1 for error.  */
165size_t type_alignof(struct Process *proc, struct arg_type_info *type);
166
167/* Align value SZ to ALIGNMENT and return the result.  */
168size_t align(size_t sz, size_t alignment);
169
170/* Return ELT-th element of compound type TYPE.  This is useful for
171 * arrays and structures.  */
172struct arg_type_info *type_element(struct arg_type_info *type, size_t elt);
173
174/* Compute an offset of EMT-th element of type TYPE.  This works for
175 * arrays and structures.  Return (size_t)-1 for error.  */
176size_t type_offsetof(struct Process *proc,
177		     struct arg_type_info *type, size_t elt);
178
179struct arg_type_info *lookup_prototype(enum arg_type at);
180
181#endif /* TYPE_H */
182