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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <errno.h>
#include <string.h>
#include <memory.h>
#include <vector>
#include <map>
#include "gdb_result_parser.h"
#include "gdb_parser_incl.h"
char *loadFile(const char *fileName);
void MakeSubTree(int depth);
void MakeTree();
void ReadTokens();
static void printNode(const std::string& str, int depth = 0)
{
for (int i=0; i<depth; i++)
printf(" ");
printf("%s\n", str.c_str());
}
static void GDB_STRIP_QUOATES(std::string ¤tToken)
{
size_t where = currentToken.find("\"");
if (where != std::string::npos && where == 0) {
currentToken.erase(0, 1);
}
where = currentToken.rfind("\"");
if (where != std::string::npos && where == currentToken.length()-1) {
currentToken.erase(where);
}
where = currentToken.find("\"\\\\");
if (where != std::string::npos && where == 0) {
currentToken.erase(0, 3);
}
where = currentToken.rfind("\"\\\\");
if (where != std::string::npos && where == currentToken.length()-3) {
currentToken.erase(where);
}
}
#define GDB_LEX()\
{\
type = gdb_result_lex();\
currentToken = gdb_result_string;\
}
#define GDB_BREAK(ch)\
if(type != (int)ch){\
break;\
}
bool testParseLocals();
bool testTokens();
bool testChildrenParser();
void testRegisterNames();
int main(int argc, char **argv)
{
//testTokens();
// testChildrenParser();
testRegisterNames();
return 0;
}
void testRegisterNames()
{
char *l = loadFile("../test.txt");
if( !l ) {
return;
}
std::vector<std::string> names;
gdbParseRegisterNames(l, names);
for(size_t i=0; i<names.size(); ++i) {
printf("%s\n", names.at(i).c_str());
}
free(l);
}
bool testChildrenParser()
{
char *l = loadFile("../test.txt");
if( !l ) {
return false;
}
GdbChildrenInfo info;
gdbParseListChildren( l, info );
info.print();
free(l);
}
char *loadFile(const char *fileName)
{
FILE *fp;
long len;
char *buf = NULL;
fp = fopen(fileName, "rb");
if (!fp) {
printf("failed to open file '../test.txt': %s\n", strerror(errno));
return NULL;
}
//read the whole file
fseek(fp, 0, SEEK_END); //go to end
len = ftell(fp); //get position at end (length)
fseek(fp, 0, SEEK_SET); //go to begining
buf = (char *)malloc(len+1); //malloc buffer
//read into buffer
long bytes = fread(buf, sizeof(char), len, fp);
printf("read: %ld\n", bytes);
if (bytes != len) {
fclose(fp);
printf("failed to read from file 'test.h': %s\n", strerror(errno));
return NULL;
}
buf[len] = 0; // make it null terminated string
fclose(fp);
return buf;
}
bool testTokens()
{
char *l = loadFile("../test.txt");
if (!l) {
return false;
}
setGdbLexerInput(l, true, true);
ReadTokens();
gdb_result_lex_clean();
free(l);
}
bool testParseLocals()
{
char *l = loadFile("../test.txt");
if (!l) {
return false;
}
std::string strline = l, tmpline;
size_t pos = strline.find("{");
if(pos != std::string::npos) {
strline = strline.substr(pos);
}
pos = strline.rfind("}");
if(pos != std::string::npos) {
strline = strline.substr(0, pos);
}
#ifdef __WXMAC__
if(strline.find("^done,locals={") != std::string::npos)
#else
if(strline.find("^done,locals=[") != std::string::npos)
#endif
{
strline = strline.substr(14);
}
if (strline.at(strline.length()-1) == ']') {
strline = strline.erase(strline.length()-1);
}
setGdbLexerInput(strline, true);
MakeTree();
gdb_result_lex_clean();
return true;
}
void ReadTokens()
{
std::string currentToken;
int type(0);
GDB_LEX();
while (type != 0) {
printf("Token=%s | %d\n", currentToken.c_str(), type);
GDB_LEX();
}
}
void MakeTree()
{
std::string displayLine;
std::string currentToken;
int type(0);
//remove prefix
GDB_LEX();
while (type != 0) {
//pattern is *always* name="somename",value="somevalue"
//however, value can be a sub tree value="{....}"
if (type != GDB_NAME) {
GDB_LEX();
continue;
}
//wait for the '='
GDB_LEX();
GDB_BREAK('=');
GDB_LEX();
if (type != GDB_STRING && type != GDB_ESCAPED_STRING) {
break;
}
// remove quoates from the name value
GDB_STRIP_QUOATES(currentToken);
displayLine += currentToken;
//comma
GDB_LEX();
GDB_BREAK(',');
//value
GDB_LEX();
if (type != GDB_VALUE) {
break;
}
GDB_LEX();
GDB_BREAK('=');
GDB_LEX();
if (type != GDB_STRING) {
break;
}
// remove the quoates from the value
GDB_STRIP_QUOATES(currentToken);
if (currentToken.at(0) == '{') {
if (displayLine.empty() == false) {
//open a new node for the tree
printNode(displayLine);
// since we dont want a dummy <unnamed> node, we remove the false
// open brace
std::string tmp(currentToken);
tmp = tmp.substr(1);
// also remove the last closing brace
tmp = tmp.erase(tmp.length()-1, 1);
// set new buffer to the
gdb_result_push_buffer(tmp);
MakeSubTree(1);
// restore the previous buffer
gdb_result_pop_buffer();
}
} else {
// simple case
displayLine += " = ";
// set new buffer to the
gdb_result_push_buffer(currentToken);
GDB_LEX();
while (type != 0) {
if (type == (int)'{') {
//open a new node for the tree
printNode(displayLine);
MakeSubTree(1);
displayLine.clear();
break;
} else {
displayLine += currentToken;
displayLine += " ";
}
GDB_LEX();
}
// restore the previous buffer
gdb_result_pop_buffer();
if (displayLine.empty() == false) {
printNode(displayLine);
displayLine.clear();
}
}
displayLine.clear();
GDB_LEX();
}
}
void MakeSubTree(int depth)
{
//the pattern here should be
//key = value, ....
//where value can be a complex value:
//key = {...}
std::string displayLine;
std::string name, value;
std::string currentToken;
int type(0);
GDB_LEX();
while (type != 0) {
switch (type) {
case (int)'=':
displayLine += "= ";
break;
case (int)'{': {
//create the new child node
// display line can be empty (in case of unnamed structures)
if (displayLine.empty()) {
displayLine = "<unnamed>";
}
//make a sub node
printNode(displayLine, depth);
MakeSubTree(depth++);
displayLine.clear();
}
break;
case (int)',':
if (displayLine.empty() == false) {
printNode(displayLine, depth);
displayLine.clear();
}
break;
case (int)'}':
if (displayLine.empty() == false) {
printNode(displayLine, depth);
displayLine.clear();
}
return;
default:
displayLine += currentToken;
displayLine += " ";
break;
}
GDB_LEX();
}
if (type == 0 && !displayLine.empty()) {
printNode(displayLine);
displayLine = "";
}
}
|