summaryrefslogtreecommitdiffstats
path: root/dwarf.c
blob: 637d71043461ace22afc2f2ab3b7343b339e35c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include "dwarf.h"
#include "libdwarf.h"


static void parse_dwarf(Dwarf_Debug dbg, const char *struct_name, const char *field_name);
static void parse_level(Dwarf_Debug dbg, Dwarf_Die die, const char *struct_name, const char *field_name, int level);

void usage(char *argv[]) {
	printf("%s file struct field\n", argv[0]);
}

int main(int argc, char *argv[]) {
	Dwarf_Debug dbg = 0;
	int fd = -1;
	const char *filepath;
	const char *struct_name, *field_name;
	int res = DW_DLV_ERROR;
	Dwarf_Error error;
	Dwarf_Handler errhand = 0;
	Dwarf_Ptr errarg = 0;

	if(argc < 4) {
		usage(argv);
		exit(1);
	}

	filepath = argv[1];
	struct_name = argv[2];
	field_name = argv[3];

	fd = open(filepath,O_RDONLY);
	if(fd < 0) {
		printf("Failure attempting to open %s\n",filepath);
	}
	res = dwarf_init(fd,DW_DLC_READ,errhand,errarg, &dbg,&error);
	if(res != DW_DLV_OK) {
		printf("Giving up, cannot do DWARF processing\n");
		exit(1);
	}

	parse_dwarf(dbg, struct_name, field_name);

	res = dwarf_finish(dbg,&error);
	if(res != DW_DLV_OK) {
		printf("dwarf_finish failed!\n");
	}
	close(fd);
	return 0;
}

static void parse_dwarf(Dwarf_Debug dbg, const char *struct_name,
		const char *field_name) {
	Dwarf_Bool is_info = 1;
	Dwarf_Unsigned cu_length;
	Dwarf_Half cu_version;
	Dwarf_Off cu_abbrev_offset;
	Dwarf_Half cu_pointer_size;
	Dwarf_Half cu_offset_size;
	Dwarf_Half cu_extension_size;
	Dwarf_Sig8 type_signature;
	Dwarf_Unsigned type_offset;
	Dwarf_Unsigned cu_next_offset;
	Dwarf_Error err;
	int rc;


	while (1) {
		Dwarf_Die die, next;

		rc = dwarf_next_cu_header_c(dbg, is_info, &cu_length,
			&cu_version, &cu_abbrev_offset, &cu_pointer_size,
			&cu_offset_size, &cu_extension_size, &type_signature,
			&type_offset, &cu_next_offset, &err);

		if (rc == DW_DLV_NO_ENTRY)
			break;
		if (rc != DW_DLV_OK) {
			printf("error dwarf_next_cu_header_c: %d %s\n", rc, dwarf_errmsg(err));
			exit(1);
		}


		rc = dwarf_siblingof(dbg, NULL, &die, &err);
		if (rc != DW_DLV_OK) {
			printf("first dwarf_siblingof failed: %d %s\n", rc, dwarf_errmsg(err));
			exit(1);
		}

		parse_level(dbg, die, struct_name, field_name, 0);
	}

	printf("struct %s / member %s not found\n", struct_name, field_name);
	exit(2);
}

static void parse_level(Dwarf_Debug dbg, Dwarf_Die die, const char *struct_name, const char *field_name, int level) {
	Dwarf_Die next;
	Dwarf_Error err;
	int rc;

	do {
		char *name;
		const char *tag_name;
		Dwarf_Half tag;

		rc = dwarf_diename(die, &name, &err);
		if (rc == DW_DLV_NO_ENTRY) {
			printf("no diename for %p?\n", die);
			break;
		}
		if (rc != DW_DLV_OK) {
			printf("dwarf_diename error: %d %s\n", rc, dwarf_errmsg(err));
			exit(1);
		}

		rc = dwarf_tag(die, &tag, &err);
		if (rc != DW_DLV_OK) {
			printf("dwarf_tag error: %d %s\n", rc, dwarf_errmsg(err));
			exit(1);
		}

		rc = dwarf_get_TAG_name(tag, &tag_name);
		if (rc != DW_DLV_OK) {
			printf("dwarf_get_TAG_name error: %d\n", rc);
			exit(1);
		}

		printf("<%d> %p <%d> %s: %s\n", level, die, tag, tag_name, name);

		rc = dwarf_siblingof(dbg, die, &next, &err);
		dwarf_dealloc(dbg, die, DW_DLA_DIE);

		die = next;
	} while (die);
}