aboutsummaryrefslogtreecommitdiff
path: root/libop/op_xml_out.c
blob: d779c45107d7fc9f20d0eda2ab1897a07baa83b9 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
 * @file op_xml_out.c
 * C utility routines for writing XML
 *
 * @remark Copyright 2008 OProfile authors
 * @remark Read the file COPYING
 *
 * @author Dave Nomura
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "op_xml_out.h"

char const * xml_tag_map[] = {
	"NONE",
	"id",
	"profile",
		"processor",
		"cputype",
		"title",
		"schemaversion",
		"mhz",
	"setup",
	"timersetup",
		"rtcinterrupts",
	"eventsetup",
		"eventname",
		"unitmask",
		"setupcount",
		"separatedcpus",
	"options",
		"session", "debuginfo", "details", "excludedependent",
		"excludesymbols", "imagepath", "includesymbols", "merge",
	"classes",
	"class",
		"cpu",
		"event",
		"mask",
	"process",
		"pid",
	"thread",
		"tid",
	"binary",
	"module",
		"name",
	"callers",
	"callees",
	"symbol",
		"idref",
		"self",
		"detaillo",
		"detailhi",
	"symboltable",
	"symboldata",
		"startingaddr",
		"file",
		"line",
		"codelength",
	"summarydata",
	"sampledata",
	"count",
	"detailtable",
	"symboldetails",
	"detaildata",
		"vmaoffset",
	"bytestable",
	"bytes",
	"help_events",
	"header",
		"title",
		"doc",
	"event",
		"event_name",
		"group",
		"desc",
		"counter_mask",
		"min_count",
	"unit_masks",
		"default",
	"unit_mask",
		"mask",
		"desc"
};

#define MAX_BUF_LEN 2048
char const * xml_tag_name(tag_t tag)
{
	return xml_tag_map[tag];
}


void open_xml_element(tag_t tag, int with_attrs, char * buffer)
{
	char const * tag_name = xml_tag_name(tag);
	unsigned int const max_len = strlen(tag_name) + 3;
	char tmp_buf[MAX_BUF_LEN];

	if (max_len >= sizeof(tmp_buf))
		fprintf(stderr,"Warning: open_xml_element: buffer overflow %d\n", max_len);

	if (snprintf(tmp_buf, sizeof(tmp_buf), "<%s%s", tag_name,
		(with_attrs ? " " : ">\n")) < 0) {
		fprintf(stderr,"open_xml_element: snprintf failed\n");
		exit(EXIT_FAILURE);
	}
	strncat(buffer, tmp_buf, sizeof(tmp_buf));
}


void close_xml_element(tag_t tag, int has_nested, char * buffer)
{
	char const * tag_name = xml_tag_name(tag);
	unsigned int const max_len = strlen(tag_name) + 3;
	char tmp_buf[MAX_BUF_LEN];

	if (max_len >= sizeof(tmp_buf))
		fprintf(stderr,"Warning: close_xml_element: buffer overflow %d\n", max_len);

	if (tag == NONE) {
		if (snprintf(tmp_buf, sizeof(tmp_buf), "%s\n", (has_nested ? ">" : "/>")) < 0) {
			fprintf(stderr, "close_xml_element: snprintf failed\n");
			exit(EXIT_FAILURE);
		}
	} else {
		if (snprintf(tmp_buf, sizeof(tmp_buf), "</%s>\n", tag_name) < 0) {
			fprintf(stderr, "close_xml_element: snprintf failed\n");
			exit(EXIT_FAILURE);
		}
	}
	strncat(buffer, tmp_buf, sizeof(tmp_buf));
}


void init_xml_int_attr(tag_t attr, int value, char * buffer)
{
	char const * attr_name = xml_tag_name(attr);
	char tmp_buf[MAX_BUF_LEN];
	unsigned int const max_len = strlen(attr_name) + 50;

	if (max_len >= sizeof(tmp_buf)) {
		fprintf(stderr,
			"Warning: init_xml_int_attr: buffer overflow %d\n", max_len);
	}


	if (snprintf(tmp_buf, sizeof(tmp_buf), " %s=\"%d\"", attr_name, value) < 0) {
		fprintf(stderr,"init_xml_int_attr: snprintf failed\n");
		exit(EXIT_FAILURE);
	}
	strncat(buffer, tmp_buf, sizeof(tmp_buf));
}


void init_xml_dbl_attr(tag_t attr, double value, char * buffer)
{
	char const * attr_name = xml_tag_name(attr);
	unsigned int const max_len = strlen(attr_name) + 50;
	char tmp_buf[MAX_BUF_LEN];

	if (max_len >= sizeof(tmp_buf))
		fprintf(stderr, "Warning: init_xml_dbl_attr: buffer overflow %d\n", max_len);

	if (snprintf(tmp_buf, sizeof(tmp_buf), " %s=\"%.2f\"", attr_name, value) < 0) {
		fprintf(stderr, "init_xml_dbl_attr: snprintf failed\n");
		exit(EXIT_FAILURE);
	}
	strncat(buffer, tmp_buf, sizeof(tmp_buf));
}


static char * xml_quote(char const * str, char * quote_buf)
{
	int i;
	int pos = 0;
	int len = strlen(str);

	
	quote_buf[pos++] = '"';

	for (i = 0; i < len; i++) {
		if (pos >= MAX_BUF_LEN - 10) {
			fprintf(stderr,"quote_str: buffer overflow %d\n", pos);
			exit(EXIT_FAILURE);
		}

		switch(str[i]) {
		case '&':
			strncpy(quote_buf + pos, "&amp;", 5);
			pos += 5;
			break;
		case '<':
			strncpy(quote_buf + pos, "&lt;", 4);
			pos += 4;
			break;
		case '>':
			strncpy(quote_buf + pos, "&gt;", 4);
			pos += 4;
			break;
		case '"':
			strncpy(quote_buf + pos, "&quot;", 6);
			pos += 6;
			break;
		default:
			quote_buf[pos++] = str[i];
			break;
		}
	}

	quote_buf[pos++] = '"';
	quote_buf[pos++] = '\0';
	return quote_buf;
}


void init_xml_str_attr(tag_t attr, char const * str, char * buffer)
{
	char tmp_buf[MAX_BUF_LEN];
	char quote_buf[MAX_BUF_LEN];
	char const * attr_name = xml_tag_name(attr);
	char const * quote_str = xml_quote(str, quote_buf);
	const unsigned int max_len = strlen(attr_name) + strlen(quote_str) + 10;

	if (max_len >= sizeof(tmp_buf))
		fprintf(stderr, "Warning: init_xml_str_attr: buffer overflow %d\n", max_len);

	if (snprintf(tmp_buf, sizeof(tmp_buf), " %s=""%s""", attr_name, quote_str) < 0) {
		fprintf(stderr,"init_xml_str_attr: snprintf failed\n");
		exit(EXIT_FAILURE);
	}
	strncat(buffer, tmp_buf, sizeof(tmp_buf));
}