blob: 5882689f6453fbea37549227425e2b225c489df3 [file] [log] [blame]
Petr Machata865303f2012-01-06 18:40:38 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 */
20
21#ifndef PARAM_H
22#define PARAM_H
23
24#include "forward.h"
25
26/* The structure param holds information about a parameter of a
27 * function. It's used to configure a function prototype. There are
28 * two flavors of parameters:
29 *
30 * - simple types
31 * - parameter packs
32 *
33 * Parameter packs are used to describe various vararg constructs.
34 * They themselves are parametrized by ltrace expressions. Those will
35 * typically be references to other arguments, but constants might
36 * also make sense, and it principle, anything can be used. */
37
38enum param_flavor {
39 PARAM_FLAVOR_TYPE,
40 PARAM_FLAVOR_PACK,
41
42 /* This is for emitting arguments in two bunches. This is
43 * where we should stop emitting "left" bunch. All that's
44 * after this parameter should be emitted in the "right"
45 * bunch. */
46 PARAM_FLAVOR_STOP,
47};
48
49enum param_status {
50 PPCB_ERR = -1, /* An error occurred. */
51 PPCB_STOP, /* Stop fetching the arguments. */
52 PPCB_CONT, /* Display this argument and keep going. */
53};
54
55/* Each parameter enumerator defines its own context object.
56 * Definitions of these are in respective .c files of each
57 * enumerator. */
58struct param_enum;
59
60/* int printf(string, pack(format, arg1)); */
61struct param {
62 enum param_flavor flavor;
63 union {
64 struct {
65 struct arg_type_info *type;
66 int own_type;
67 } type;
68 struct {
69 struct expr_node *args;
70 size_t nargs;
71 int own_args;
72
73 struct param_enum *(*init)(struct value *cb_args,
74 size_t nargs,
75 struct value_dict *arguments);
76 int (*next)(struct param_enum *self,
77 struct arg_type_info *info,
78 int *insert_stop);
79 enum param_status (*stop)(struct param_enum *self,
80 struct value *value);
81 void (*done)(struct param_enum *self);
82 } pack;
83 } u;
84};
85
86/* Initialize simple type parameter. TYPE is owned and released by
87 * PARAM if OWN_TYPE. */
88void param_init_type(struct param *param,
89 struct arg_type_info *type, int own_type);
90
91/* Initialize a stop. */
92void param_init_stop(struct param *param);
93
94/* Initialize parameter pack PARAM. ARGS is an array of expressions
95 * with parameters. ARGS is owned and released by the pack if
96 * OWN_ARGS. NARGS is number of ARGS.
97 *
98 * When the parameter pack should be expanded, those expressions are
99 * evaluated and passed to the INIT callback. This has to return a
100 * non-NULL context object.
101 *
102 * The NEXT callback is then called repeatedly, and should initialize
103 * its INFOP argument to a type of the next parameter in the pack.
104 * When there are no more parameters in the pack, the NEXT callback
105 * will set INFOP to a VOID parameter. If the callback sets
106 * INSERT_STOP to a non-zero value, a stop parameter shall be inserted
107 * before this actual parameter.
108 *
109 * Core then uses the passed-in type to fetch the next argument, which
110 * is in turn passed to STOP callback. This callback then tells
111 * ltrace core what to do next: whether there are more arguments, and
112 * if not, whether this argument should be displayed.
113 *
114 * After the enumeration is ended, DONE callback is called. */
115void param_init_pack(struct param *param,
116 struct expr_node *args, size_t nargs, int own_args,
117 struct param_enum *(*init)(struct value *cb_args,
118 size_t nargs,
119 struct value_dict *arguments),
120 int (*next)(struct param_enum *self,
121 struct arg_type_info *infop,
122 int *insert_stop),
123 enum param_status (*stop)(struct param_enum *self,
124 struct value *value),
125 void (*done)(struct param_enum *self));
126
127/* Start enumerating types in parameter pack. This evaluates the
128 * parameter the pack arguments and calls the init callback. See the
129 * documentation of param_init_pack for details. */
130struct param_enum *param_pack_init(struct param *param,
131 struct value_dict *fargs);
132
133/* Ask for next type in enumeration. See the documentation of
134 * param_init_pack for details. */
135int param_pack_next(struct param *param, struct param_enum *self,
136 struct arg_type_info *infop, int *insert_stop);
137
138/* Ask whether we should stop enumerating. See the documentation of
139 * param_init_pack for details. */
140enum param_status param_pack_stop(struct param *param,
141 struct param_enum *self, struct value *value);
142
143/* Finish enumerating types in parameter pack. See the documentation
144 * of param_init_pack for details. */
145void param_pack_done(struct param *param, struct param_enum *self);
146
147/* Destroy data held by PARAM, but not the PARAM pointer itself. */
148void param_destroy(struct param *param);
149
150#endif /* PARAM_H */