added a bit more diagnostic printing
[platform/upstream/ltrace.git] / library.h
1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 2012,2013 Petr Machata, Red Hat Inc.
4  * Copyright (C) 2006 Paul Gilliam, IBM Corporation
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 _LIBRARY_H_
23 #define _LIBRARY_H_
24
25 #include <stdint.h>
26
27 #if defined(HAVE_LIBDW)
28 # include <elfutils/libdwfl.h>
29 #endif
30
31 #include "callback.h"
32 #include "forward.h"
33 #include "sysdep.h"
34
35 enum toplt {
36         LS_TOPLT_NONE = 0,      /* PLT not used for this symbol. */
37         LS_TOPLT_EXEC,          /* PLT for this symbol is executable. */
38 };
39
40 /* Dict interface.  */
41 size_t arch_addr_hash(const arch_addr_t *addr);
42 int arch_addr_eq(const arch_addr_t *addr1, const arch_addr_t *addr2);
43
44 /* For handling -l.  */
45 struct library_exported_name {
46         struct library_exported_name *next;
47         const char *name;
48         int own_name : 1;
49 };
50
51 struct library_symbol {
52         struct library_symbol *next;
53         struct library *lib;
54         const char *name;
55         arch_addr_t enter_addr;
56         enum toplt plt_type;
57
58         /* If this is non-NULL, this prototype is used instead of
59          * looking up one in LIB->protolib.  */
60         struct prototype *proto;
61
62         int own_name : 1;
63
64         /* This is relevant for PLT symbols.  Latent PLT symbols are
65          * those that don't match any of the -e rules, but that might
66          * potentially become active if a library implementing them
67          * appears that matches a -l rule.  Ltrace core is responsible
68          * for clearing latent flag.  */
69         int latent : 1;
70
71         /* Delayed symbols are those for which a breakpoint shouldn't
72          * be enabled yet.  They are similar to latent symbols, but
73          * backend is responsible for clearing the delayed flag.  See
74          * proc_activate_delayed_symbol.  */
75         int delayed : 1;
76
77         struct arch_library_symbol_data arch;
78         struct os_library_symbol_data os;
79 };
80
81 /* Init LIBSYM.  NAME will be freed when LIBSYM is destroyed if
82  * OWN_NAME.  ARCH has to be initialized by a separate call.  */
83 int library_symbol_init(struct library_symbol *libsym,
84                         arch_addr_t addr, const char *name, int own_name,
85                         enum toplt type_of_plt);
86
87 /* Copy library symbol SYM into the area pointed-to by RETP.  Return 0
88  * on success or a negative value on failure.  */
89 int library_symbol_clone(struct library_symbol *retp,
90                          struct library_symbol *sym);
91
92 /* Destroy library symbol.  This essentially just frees name if it's
93  * owned.  It doesn't free the memory associated with SYM pointer
94  * itself.  Returns 0 on success or a negative value in case of an
95  * error (which would be an out of memory condition).  */
96 void library_symbol_destroy(struct library_symbol *sym);
97
98 /* Compare two library symbols.  Returns a negative value, 0, or a
99  * positive value, much like strcmp.  The function compares symbol
100  * addresses, and if those are equal, it compares symbol names.  If
101  * those are equal, too, the symbols are considered equal.  */
102 int library_symbol_cmp(struct library_symbol *a, struct library_symbol *b);
103
104 /* Set a name for library symbol.  This frees the old name, if
105  * that is owned.  */
106 void library_symbol_set_name(struct library_symbol *libsym,
107                              const char *name, int own_name);
108
109 /* A function that can be used as library_each_symbol callback.  Looks
110  * for a symbol SYM for which library_symbol_cmp(SYM, STANDARD)
111  * returns 0.  */
112 enum callback_status library_symbol_equal_cb(struct library_symbol *libsym,
113                                              void *standard);
114
115 /* A function that can be used as library_each_symbol callback.  Looks
116  * for a symbol SYM for which strcmp(SYM->name, NAME) == 0.  */
117 enum callback_status library_symbol_named_cb(struct library_symbol *libsym,
118                                              void *name);
119
120 /* A function that can be used as library_each_symbol callback.  Looks
121  * for a delayed symbol.  */
122 enum callback_status library_symbol_delayed_cb(struct library_symbol *libsym,
123                                                void *unused);
124
125 enum library_type {
126         LT_LIBTYPE_MAIN,
127         LT_LIBTYPE_DSO,
128         LT_LIBTYPE_SYSCALL,
129 };
130
131 /* XXX we might consider sharing libraries across processes.  Things
132  * like libc will be opened by every single process, no point cloning
133  * these everywhere.  But for now, keep the ownership structure
134  * simple.  */
135 struct library {
136         struct library *next;
137
138         /* Unique key. Two library objects are considered equal, if
139          * they have the same key.  */
140         arch_addr_t key;
141
142         /* Address where the library is mapped.  */
143         arch_addr_t base;
144
145         /* Absolute address of the entry point.  Useful for main
146          * binary, though I suppose the value might be useful for the
147          * dynamic linker, too (in case we ever want to do early
148          * process tracing).  */
149         arch_addr_t entry;
150
151         /* Address of PT_DYNAMIC segment.  */
152         arch_addr_t dyn_addr;
153
154         /* Symbols associated with the library.  This includes a
155          * symbols that don't have a breakpoint attached (yet).  */
156         struct library_symbol *symbols;
157
158         /* List of names that this library implements, and that match
159          * -l filter.  Each time a new library is mapped, its list of
160          * exports is examined, and corresponding PLT slots are
161          * enabled.  */
162         struct library_exported_name *exported_names;
163
164         /* Prototype library associated with this library.  */
165         struct protolib *protolib;
166
167         const char *soname;
168         const char *pathname;
169
170         enum library_type type;
171
172         char own_soname : 1;
173         char own_pathname : 1;
174
175         struct arch_library_data arch;
176         struct os_library_data os;
177
178 #if defined(HAVE_LIBDW)
179         Dwfl *dwfl;
180 #endif
181 };
182
183 /* Init LIB.  */
184 int library_init(struct library *lib, enum library_type type);
185
186 /* Initialize RETP to a library identical to LIB.  Symbols are not
187  * shared, but copied over.  Returns 0 on success and a negative value
188  * in case of failure.  */
189 int library_clone(struct library *retp, struct library *lib);
190
191 /* Destroy library.  Doesn't free LIB itself.  Symbols are destroyed
192  * and freed.  */
193 void library_destroy(struct library *lib);
194
195 /* Set library soname.  Frees the old name if necessary.  */
196 void library_set_soname(struct library *lib,
197                         const char *new_name, int own_name);
198
199 /* Set library pathname.  Frees the old name if necessary.  */
200 void library_set_pathname(struct library *lib,
201                           const char *new_name, int own_name);
202
203 /* Iterate through list of symbols of library LIB.  See callback.h for
204  * notes on this interface.  */
205 struct library_symbol *library_each_symbol
206         (struct library *lib, struct library_symbol *start_after,
207          enum callback_status (*cb)(struct library_symbol *, void *),
208          void *data);
209
210 /* Add a new symbol SYM to LIB.  SYM is assumed owned, we need to
211  * overwrite SYM->next.  */
212 void library_add_symbol(struct library *lib, struct library_symbol *sym);
213
214 /* A function that can be used as proc_each_library callback.  Looks
215  * for a library with the name passed in DATA.  PROC is ignored.  */
216 enum callback_status library_named_cb(struct process *proc,
217                                       struct library *lib, void *name);
218
219 /* A function that can be used as proc_each_library callback.  Looks
220  * for a library with given base.
221  *
222  * NOTE: The key is passed as a POINTER to arch_addr_t (that
223  * because in general, arch_addr_t doesn't fit in void*).  */
224 enum callback_status library_with_key_cb(struct process *proc,
225                                          struct library *lib, void *keyp);
226
227 /* XXX this should really be in backend.h (as on pmachata/revamp
228  * branch), or, on this branch, in common.h.  But we need
229  * arch_addr_t (which should also be in backend.h, I reckon), so
230  * stuff it here for the time being.  */
231 /* This function is implemented in the back end.  It is called for all
232  * raw addresses as read from symbol tables etc.  If necessary on
233  * given architecture, this function should translate the address
234  * according to .opd or other indirection mechanism.  Returns 0 on
235  * success and a negative value on failure.  */
236 struct ltelf;
237 int arch_translate_address(struct ltelf *lte,
238                            arch_addr_t addr, arch_addr_t *ret);
239 /* This is the same function as arch_translate_address, except it's
240  * used at the point that we don't have ELF available anymore.  */
241 int arch_translate_address_dyn(struct process *proc,
242                                arch_addr_t addr, arch_addr_t *ret);
243
244 #endif /* _LIBRARY_H_ */