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 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/*
* Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License (not later!)
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <libxml/xmlwriter.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include "trace-cmd.h"
#include "trace-xml.h"
struct tracecmd_xml_handle {
xmlTextWriterPtr writer;
xmlDocPtr doc;
};
struct tracecmd_xml_system {
struct tracecmd_xml_handle *handle;
xmlXPathObjectPtr result;
xmlNodePtr cur;
};
#define TRACE_ENCODING "UTF-8"
int tracecmd_xml_write_element(struct tracecmd_xml_handle *handle,
const char *obj,
const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = xmlTextWriterWriteVFormatElement(handle->writer,
BAD_CAST obj, fmt, ap);
va_end(ap);
return ret;
}
struct tracecmd_xml_handle *tracecmd_xml_create(const char *name,
const char *version)
{
struct tracecmd_xml_handle *handle;
int ret;
handle = malloc_or_die(sizeof(*handle));
memset(handle, 0, sizeof(*handle));
handle->writer = xmlNewTextWriterFilename(name, 0);
if (!handle->writer)
goto fail_free;
ret = xmlTextWriterStartDocument(handle->writer, NULL,
TRACE_ENCODING, NULL);
if (ret < 0)
goto fail_close;
ret = xmlTextWriterStartElement(handle->writer,
BAD_CAST "KernelShark");
if (ret < 0)
goto fail_close;
return handle;
fail_close:
xmlFreeTextWriter(handle->writer);
fail_free:
free(handle);
return NULL;
}
int tracecmd_xml_start_system(struct tracecmd_xml_handle *handle,
const char *system)
{
int ret;
ret = xmlTextWriterStartElement(handle->writer,
BAD_CAST system);
if (ret < 0)
return ret;
return 0;
}
int tracecmd_xml_start_sub_system(struct tracecmd_xml_handle *handle,
const char *subsystem)
{
int ret;
ret = xmlTextWriterStartElement(handle->writer,
BAD_CAST subsystem);
return ret;
}
void tracecmd_xml_end_system(struct tracecmd_xml_handle *handle)
{
xmlTextWriterEndElement(handle->writer);
}
void tracecmd_xml_end_sub_system(struct tracecmd_xml_handle *handle)
{
xmlTextWriterEndElement(handle->writer);
}
void tracecmd_xml_close(struct tracecmd_xml_handle *handle)
{
if (handle->writer) {
xmlTextWriterEndElement(handle->writer);
xmlTextWriterEndDocument(handle->writer);
xmlFreeTextWriter(handle->writer);
}
if (handle->doc) {
xmlFreeDoc(handle->doc);
}
free(handle);
}
/***********************************************************/
/*** Reading XML files ***/
/***********************************************************/
struct tracecmd_xml_handle *tracecmd_xml_open(const char *file)
{
struct tracecmd_xml_handle *handle;
handle = malloc_or_die(sizeof(*handle));
memset(handle, 0, sizeof(*handle));
handle->doc = xmlParseFile(file);
if (!handle->doc)
goto fail_free;
return handle;
fail_free:
free(handle);
return NULL;
}
struct tracecmd_xml_system *
tracecmd_xml_find_system(struct tracecmd_xml_handle *handle,
const char *system)
{
struct tracecmd_xml_system *sys;
xmlXPathContextPtr context;
xmlXPathObjectPtr result;
xmlChar *xpath;
char *path;
path = malloc_or_die(strlen(system) + 3);
sprintf(path, "//%s", system);
xpath = BAD_CAST path;
context = xmlXPathNewContext(handle->doc);
result = xmlXPathEvalExpression(xpath, context);
free(path);
if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
xmlXPathFreeObject(result);
return NULL;
}
sys = malloc_or_die(sizeof(*sys));
sys->handle = handle;
sys->result = result;
sys->cur = result->nodesetval->nodeTab[0]->xmlChildrenNode;
return sys;
}
struct tracecmd_xml_system_node *
tracecmd_xml_system_node(struct tracecmd_xml_system *system)
{
return (struct tracecmd_xml_system_node *)system->cur;
}
const char *tracecmd_xml_node_type(struct tracecmd_xml_system_node *tnode)
{
xmlNodePtr node = (xmlNodePtr)tnode;
return (const char *)node->name;
}
struct tracecmd_xml_system_node *
tracecmd_xml_node_child(struct tracecmd_xml_system_node *tnode)
{
xmlNodePtr node = (xmlNodePtr)tnode;
return (struct tracecmd_xml_system_node *)node->xmlChildrenNode;
}
struct tracecmd_xml_system_node *
tracecmd_xml_node_next(struct tracecmd_xml_system_node *tnode)
{
xmlNodePtr node = (xmlNodePtr)tnode;
return (struct tracecmd_xml_system_node *)node->next;
}
const char *tracecmd_xml_node_value(struct tracecmd_xml_handle *handle,
struct tracecmd_xml_system_node *tnode)
{
xmlNodePtr node = (xmlNodePtr)tnode;
return (const char *)xmlNodeListGetString(handle->doc, node->xmlChildrenNode, 1);
}
void tracecmd_xml_free_system(struct tracecmd_xml_system *system)
{
xmlXPathFreeObject(system->result);
free(system);
}
int tracecmd_xml_system_exists(struct tracecmd_xml_handle *handle,
const char *system)
{
struct tracecmd_xml_system *sys;
int exists = 0;
sys = tracecmd_xml_find_system(handle, system);
if (sys) {
exists = 1;
tracecmd_xml_free_system(sys);
}
return exists;
}
|