prototype.c revision 97485501f87e5452bc49721003ebfb9717f497b6
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2012,2013 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#include <alloca.h>
22#include <errno.h>
23#include <stdlib.h>
24#include <string.h>
25#include <stdio.h>
26
27#include "common.h"
28#include "callback.h"
29#include "param.h"
30#include "prototype.h"
31#include "type.h"
32#include "options.h"
33#include "read_config_file.h"
34#include "backend.h"
35
36struct protolib_cache g_protocache;
37static struct protolib legacy_typedefs;
38
39void
40prototype_init(struct prototype *proto)
41{
42	VECT_INIT(&proto->params, struct param);
43
44	proto->return_info = NULL;
45	proto->own_return_info = 0;
46}
47
48static void
49param_destroy_cb(struct param *param, void *data)
50{
51	param_destroy(param);
52}
53
54void
55prototype_destroy(struct prototype *proto)
56{
57	if (proto == NULL)
58		return;
59	if (proto->own_return_info) {
60		type_destroy(proto->return_info);
61		free(proto->return_info);
62	}
63
64	VECT_DESTROY(&proto->params, struct param, &param_destroy_cb, NULL);
65}
66
67int
68prototype_push_param(struct prototype *proto, struct param *param)
69{
70	return VECT_PUSHBACK(&proto->params, param);
71}
72
73size_t
74prototype_num_params(struct prototype *proto)
75{
76	return vect_size(&proto->params);
77}
78
79void
80prototype_destroy_nth_param(struct prototype *proto, size_t n)
81{
82	assert(n < prototype_num_params(proto));
83	VECT_ERASE(&proto->params, struct param, n, n+1,
84		   &param_destroy_cb, NULL);
85}
86
87struct param *
88prototype_get_nth_param(struct prototype *proto, size_t n)
89{
90	assert(n < prototype_num_params(proto));
91	return VECT_ELEMENT(&proto->params, struct param, n);
92}
93
94struct each_param_data {
95	struct prototype *proto;
96	enum callback_status (*cb)(struct prototype *, struct param *, void *);
97	void *data;
98};
99
100static enum callback_status
101each_param_cb(struct param *param, void *data)
102{
103	struct each_param_data *cb_data = data;
104	return (cb_data->cb)(cb_data->proto, param, cb_data->data);
105}
106
107struct param *
108prototype_each_param(struct prototype *proto, struct param *start_after,
109		     enum callback_status (*cb)(struct prototype *,
110						struct param *, void *),
111		     void *data)
112{
113	struct each_param_data cb_data = { proto, cb, data };
114	return VECT_EACH(&proto->params, struct param, start_after,
115			 &each_param_cb, &cb_data);
116}
117
118void
119named_type_init(struct named_type *named,
120		struct arg_type_info *info, int own_type)
121{
122	named->info = info;
123	named->own_type = own_type;
124	named->forward = 0;
125}
126
127void
128named_type_destroy(struct named_type *named)
129{
130	if (named->own_type) {
131		type_destroy(named->info);
132		free(named->info);
133	}
134}
135
136void
137protolib_init(struct protolib *plib)
138{
139	DICT_INIT(&plib->prototypes, char *, struct prototype,
140		  dict_hash_string, dict_eq_string, NULL);
141
142	DICT_INIT(&plib->named_types, char *, struct named_type,
143		  dict_hash_string, dict_eq_string, NULL);
144
145	VECT_INIT(&plib->imports, struct protolib *);
146
147	plib->refs = 0;
148}
149
150static void
151destroy_prototype_cb(struct prototype *proto, void *data)
152{
153	prototype_destroy(proto);
154}
155
156static void
157destroy_named_type_cb(struct named_type *named, void *data)
158{
159	named_type_destroy(named);
160}
161
162void
163protolib_destroy(struct protolib *plib)
164{
165	assert(plib->refs == 0);
166
167	VECT_DESTROY(&plib->imports, struct prototype *, NULL, NULL);
168
169	DICT_DESTROY(&plib->prototypes, const char *, struct prototype,
170		     dict_dtor_string, destroy_prototype_cb, NULL);
171
172	DICT_DESTROY(&plib->named_types, const char *, struct named_type,
173		     dict_dtor_string, destroy_named_type_cb, NULL);
174}
175
176static struct protolib **
177each_import(struct protolib *plib, struct protolib **start_after,
178	    enum callback_status (*cb)(struct protolib **, void *), void *data)
179{
180	assert(plib != NULL);
181	return VECT_EACH(&plib->imports, struct protolib *,
182			 start_after, cb, data);
183}
184
185static enum callback_status
186is_or_imports(struct protolib **plibp, void *data)
187{
188	assert(plibp != NULL);
189	assert(*plibp != NULL);
190	struct protolib *import = data;
191	if (*plibp == import
192	    || each_import(*plibp, NULL, &is_or_imports, import) != NULL)
193		return CBS_STOP;
194	else
195		return CBS_CONT;
196}
197
198int
199protolib_add_import(struct protolib *plib, struct protolib *import)
200{
201	assert(plib != NULL);
202	assert(import != NULL);
203	if (is_or_imports(&import, plib) == CBS_STOP) {
204		fprintf(stderr, "Recursive import rejected.\n");
205		return -2;
206	}
207
208	return VECT_PUSHBACK(&plib->imports, &import) < 0 ? -1 : 0;
209}
210
211static int
212bailout(const char *name, int own)
213{
214	int save_errno = errno;
215	if (own)
216		free((char *)name);
217	errno = save_errno;
218	return -1;
219}
220
221int
222protolib_add_prototype(struct protolib *plib, const char *name, int own_name,
223		       struct prototype *proto)
224{
225	assert(plib != NULL);
226	if (strdup_if(&name, name, !own_name) < 0)
227		return -1;
228	if (DICT_INSERT(&plib->prototypes, &name, proto) < 0)
229		return bailout(name, own_name);
230	return 0;
231}
232
233int
234protolib_add_named_type(struct protolib *plib, const char *name, int own_name,
235			struct named_type *named)
236{
237	assert(plib != NULL);
238	if (strdup_if(&name, name, !own_name) < 0)
239		return -1;
240	if (DICT_INSERT(&plib->named_types, &name, named) < 0)
241		return bailout(name, own_name);
242	return 0;
243}
244
245struct lookup {
246	const char *name;
247	void *result;
248	struct dict *(*getter)(struct protolib *plib);
249};
250
251static struct dict *
252get_prototypes(struct protolib *plib)
253{
254	assert(plib != NULL);
255	return &plib->prototypes;
256}
257
258static struct dict *
259get_named_types(struct protolib *plib)
260{
261	assert(plib != NULL);
262	return &plib->named_types;
263}
264
265static enum callback_status
266protolib_lookup_rec(struct protolib **plibp, void *data)
267{
268	assert(plibp != NULL);
269	assert(*plibp != NULL);
270	struct lookup *lookup = data;
271	struct dict *dict = (*lookup->getter)(*plibp);
272
273	lookup->result = dict_find(dict, &lookup->name);
274	if (lookup->result != NULL)
275		return CBS_STOP;
276
277	if (each_import(*plibp, NULL, &protolib_lookup_rec, lookup) != NULL) {
278		assert(lookup->result != NULL);
279		return CBS_STOP;
280	}
281
282	return CBS_CONT;
283}
284
285static void *
286protolib_lookup(struct protolib *plib, const char *name,
287		struct dict *(*getter)(struct protolib *))
288{
289	assert(plib != NULL);
290	struct lookup lookup = { name, NULL, getter };
291	if (protolib_lookup_rec(&plib, &lookup) == CBS_STOP)
292		assert(lookup.result != NULL);
293	else
294		assert(lookup.result == NULL);
295	return lookup.result;
296}
297
298struct prototype *
299protolib_lookup_prototype(struct protolib *plib, const char *name)
300{
301	assert(plib != NULL);
302	return protolib_lookup(plib, name, &get_prototypes);
303}
304
305struct named_type *
306protolib_lookup_type(struct protolib *plib, const char *name)
307{
308	assert(plib != NULL);
309	return protolib_lookup(plib, name, &get_named_types);
310}
311
312static void
313destroy_protolib_cb(struct protolib **plibp, void *data)
314{
315	assert(plibp != NULL);
316
317	if (*plibp != NULL
318	    && --(*plibp)->refs == 0) {
319		protolib_destroy(*plibp);
320		free(*plibp);
321	}
322}
323
324void
325protolib_cache_destroy(struct protolib_cache *cache)
326{
327	DICT_DESTROY(&cache->protolibs, const char *, struct protolib *,
328		     dict_dtor_string, destroy_protolib_cb, NULL);
329}
330
331struct load_config_data {
332	struct protolib_cache *self;
333	const char *key;
334	struct protolib *result;
335};
336
337static struct protolib *
338consider_config_dir(struct protolib_cache *cache,
339		    const char *path, const char *key)
340{
341	size_t len = sizeof ".conf";
342	char slash[2] = {'/'};
343	char *buf = alloca(strlen(path) + 1 + strlen(key) + len);
344	strcpy(stpcpy(stpcpy(stpcpy(buf, path), slash), key), ".conf");
345
346	return protolib_cache_file(cache, buf, 0);
347}
348
349static enum callback_status
350consider_confdir_cb(struct opt_F_t *entry, void *d)
351{
352	if (opt_F_get_kind(entry) != OPT_F_DIR)
353		return CBS_CONT;
354	struct load_config_data *data = d;
355
356	data->result = consider_config_dir(data->self,
357					   entry->pathname, data->key);
358	return data->result != NULL ? CBS_STOP : CBS_CONT;
359}
360
361static int
362load_dash_F_dirs(struct protolib_cache *cache,
363		 const char *key, struct protolib **retp)
364{
365	struct load_config_data data = {cache, key};
366
367	if (VECT_EACH(&opt_F, struct opt_F_t, NULL,
368		      consider_confdir_cb, &data) == NULL)
369		/* Not found.  That's fine.  */
370		return 0;
371
372	if (data.result == NULL)
373		/* There were errors.  */
374		return -1;
375
376	*retp = data.result;
377	return 0;
378}
379
380static int
381load_config(struct protolib_cache *cache,
382	    const char *key, int private, struct protolib **retp)
383{
384	const char **dirs = NULL;
385	if (os_get_config_dirs(private, &dirs) < 0
386	    || dirs == NULL)
387		return -1;
388
389	for (; *dirs != NULL; ++dirs) {
390		struct protolib *plib = consider_config_dir(cache, *dirs, key);
391		if (plib != NULL) {
392			*retp = plib;
393			break;
394		}
395	}
396
397	return 0;
398}
399
400static int
401add_ltrace_conf(struct protolib_cache *cache)
402{
403	/* Look into private config directories for .ltrace.conf and
404	 * into system config directories for ltrace.conf.  If it's
405	 * found, add it to implicit import module.  */
406	struct protolib *plib = NULL;
407	const char *home = NULL;
408	if (os_get_ltrace_conf_filename(&home) < 0
409	    || (home != NULL
410		&& (plib = consider_config_dir(cache, home, ".ltrace")) != NULL
411		&& protolib_add_import(&cache->imports, plib) < 0)
412	    || (plib == NULL && load_config(cache, "ltrace", 0, &plib) < 0)
413	    || (plib != NULL && protolib_add_import(&cache->imports, plib) < 0))
414		/* N.B. If plib is non-NULL, it has been already
415		 * cached.  We don't therefore destroy it on
416		 * failures.  */
417		return -1;
418
419	/* Never mind whether we've found anything.  It's fine if the
420	 * config is absent.  */
421	return 0;
422}
423
424static enum callback_status
425add_imports_cb(struct opt_F_t *entry, void *data)
426{
427	struct protolib_cache *self = data;
428	if (opt_F_get_kind(entry) != OPT_F_FILE)
429		return CBS_CONT;
430
431	struct protolib *new_import
432		= protolib_cache_file(self, entry->pathname, 0);
433
434	if (new_import == NULL
435	    || protolib_add_import(&self->imports, new_import) < 0)
436		/* N.B. If new_import is non-NULL, it has been already
437		 * cached.  We don't therefore destroy it on
438		 * failures.  */
439		return CBS_STOP;
440
441	return CBS_CONT;
442}
443
444int
445protolib_cache_init(struct protolib_cache *cache, struct protolib *import)
446{
447	DICT_INIT(&cache->protolibs, char *, struct protolib *,
448		  dict_hash_string, dict_eq_string, NULL);
449	protolib_init(&cache->imports);
450
451	/* At this point the cache is consistent.  This is important,
452	 * because next we will use it to cache files that we load
453	 * due to -F.
454	 *
455	 * But we are about to construct the implicit import module,
456	 * which means this module can't be itself imported to the
457	 * files that we load now.  So remember that we are still
458	 * bootstrapping.  */
459	cache->bootstrap = 1;
460
461	if (protolib_add_import(&cache->imports, &legacy_typedefs) < 0
462	    || (import != NULL
463		&& protolib_add_import(&cache->imports, import) < 0)
464	    || add_ltrace_conf(cache) < 0
465	    || VECT_EACH(&opt_F, struct opt_F_t, NULL,
466			 add_imports_cb, cache) != NULL) {
467		protolib_cache_destroy(cache);
468		return -1;
469	}
470
471	cache->bootstrap = 0;
472	return 0;
473}
474
475static enum callback_status
476add_import_cb(struct protolib **importp, void *data)
477{
478	struct protolib *plib = data;
479	if (protolib_add_import(plib, *importp) < 0)
480		return CBS_STOP;
481	else
482		return CBS_CONT;
483}
484
485static struct protolib *
486build_default_config(struct protolib_cache *cache, const char *key)
487{
488	struct protolib *new_plib = malloc(sizeof(*new_plib));
489	if (new_plib == NULL) {
490		fprintf(stderr, "Couldn't create config module %s: %s\n",
491			key, strerror(errno));
492		return NULL;
493	}
494
495	protolib_init(new_plib);
496
497	/* If bootstrapping, copy over imports from implicit import
498	 * module to new_plib.  We can't reference the implicit
499	 * import module itself, because new_plib will become part of
500	 * this same implicit import module itself.  */
501	if ((cache->bootstrap && each_import(&cache->imports, NULL,
502					     add_import_cb, new_plib) != NULL)
503	    || (!cache->bootstrap
504		&& protolib_add_import(new_plib, &cache->imports) < 0)) {
505
506		fprintf(stderr,
507			"Couldn't add imports to config module %s: %s\n",
508			key, strerror(errno));
509		protolib_destroy(new_plib);
510		free(new_plib);
511		return NULL;
512	}
513
514	return new_plib;
515}
516
517static void
518attempt_to_cache(struct protolib_cache *cache,
519		 const char *key, struct protolib *plib)
520{
521	if (protolib_cache_protolib(cache, key, 1, plib) == 0
522	    || plib == NULL)
523		/* Never mind failing to store a NULL.  */
524		return;
525
526	/* Returning a protolib that hasn't been cached would leak
527	 * that protolib, but perhaps it's less bad then giving up
528	 * outright.  At least print an error message.  */
529	fprintf(stderr, "Couldn't cache prototype library for %s\n", key);
530}
531
532struct protolib *
533protolib_cache_search(struct protolib_cache *cache,
534		      const char *key, int own_key, int allow_private)
535{
536	struct protolib *plib = NULL;
537	if (DICT_FIND_VAL(&cache->protolibs, &key, &plib) == 0)
538		return plib;
539
540	if (strdup_if(&key, key, !own_key) < 0) {
541		fprintf(stderr, "Couldn't cache %s: %s\n",
542			key, strerror(errno));
543		return NULL;
544	}
545
546	/* The order is: -F directories, private directories, system
547	 * directories.  If the config file is not found anywhere,
548	 * build a default one.  */
549	if (load_dash_F_dirs(cache, key, &plib) < 0
550	    || (plib == NULL && allow_private
551		&& load_config(cache, key, 1, &plib) < 0)
552	    || (plib == NULL
553		&& load_config(cache, key, 0, &plib) < 0)
554	    || (plib == NULL
555		&& (plib = build_default_config(cache, key)) == NULL))
556		fprintf(stderr,
557			"Error occurred when attempting to load a prototype "
558			"library for %s.\n", key);
559
560	/* Whatever came out of this (even NULL), store it in the
561	 * cache.  */
562	attempt_to_cache(cache, key, plib);
563	return plib;
564}
565
566struct protolib *
567protolib_cache_file(struct protolib_cache *cache,
568		    const char *filename, int own_filename)
569{
570	{
571		struct protolib *plib;
572		if (DICT_FIND_VAL(&cache->protolibs, &filename, &plib) == 0)
573			return plib;
574	}
575
576	FILE *stream = fopen(filename, "r");
577	if (stream == NULL)
578		return NULL;
579
580	if (strdup_if(&filename, filename, !own_filename) < 0) {
581		fprintf(stderr, "Couldn't cache %s: %s\n",
582			filename, strerror(errno));
583		fclose(stream);
584		return NULL;
585	}
586
587	struct protolib *new_plib = build_default_config(cache, filename);
588	if (new_plib == NULL
589	    || read_config_file(stream, filename, new_plib) < 0) {
590		fclose(stream);
591		if (own_filename)
592			free((char *)filename);
593		if (new_plib != NULL) {
594			protolib_destroy(new_plib);
595			free(new_plib);
596		}
597		return NULL;
598	}
599
600	attempt_to_cache(cache, filename, new_plib);
601	fclose(stream);
602	return new_plib;
603}
604
605int
606protolib_cache_protolib(struct protolib_cache *cache,
607			const char *filename, int own_filename,
608			struct protolib *plib)
609{
610	if (strdup_if(&filename, filename, !own_filename) < 0) {
611		fprintf(stderr, "Couldn't cache %s: %s\n",
612			filename, strerror(errno));
613		return -1;
614	}
615
616	int rc = DICT_INSERT(&cache->protolibs, &filename, &plib);
617	if (rc < 0 && own_filename)
618		free((char *)filename);
619	if (rc == 0 && plib != NULL)
620		plib->refs++;
621	return rc;
622}
623
624static void
625destroy_global_config(void)
626{
627	protolib_cache_destroy(&g_protocache);
628	protolib_destroy(&legacy_typedefs);
629}
630
631void
632init_global_config(void)
633{
634	protolib_init(&legacy_typedefs);
635	struct arg_type_info *void_info = type_get_simple(ARGTYPE_VOID);
636	static struct arg_type_info ptr_info;
637	type_init_pointer(&ptr_info, void_info, 0);
638
639	static struct named_type voidptr_type;
640	named_type_init(&voidptr_type, &ptr_info, 0);
641
642	/* Build legacy typedefs first.  This is used by
643	 * protolib_cache_init call below.  */
644	if (protolib_add_named_type(&legacy_typedefs, "addr", 0,
645				    &voidptr_type) < 0
646	    || protolib_add_named_type(&legacy_typedefs, "file", 0,
647				       &voidptr_type) < 0) {
648		fprintf(stderr,
649			"Couldn't initialize aliases `addr' and `file'.\n");
650
651		exit(1);
652	}
653
654	if (protolib_cache_init(&g_protocache, NULL) < 0) {
655		fprintf(stderr, "Couldn't init prototype cache\n");
656		exit(1);
657	}
658
659	atexit(destroy_global_config);
660}
661