From 7cb8c5f7096be04cd65ec41a5b8f98d0ae3e890a Mon Sep 17 00:00:00 2001 From: NĂ©lio Laranjeiro Date: Sun, 23 Nov 2008 19:35:46 +0100 Subject: tools/trace: First version of the trace creator parser source file. --- tools/trace/example/trace.trc | 3 +++ tools/trace/lib/__init__.py | 0 tools/trace/lib/parser.g | 32 +++++++++++++++++++++++++ tools/trace/lib/traceclass.py | 43 +++++++++++++++++++++++++++++++++ tools/trace/tcreator/__init__.py | 0 tools/trace/tcreator/tcreator.py | 27 +++++++++++++++++++++ tools/trace/tcreator/template.h | 33 +++++++++++++++++++++++++ tools/trace/tcreator/writer.py | 39 ++++++++++++++++++++++++++++++ tools/trace/tinter/__init__.py | 0 tools/trace/tinter/thost.py | 2 ++ tools/trace/tinter/tinter.py | 43 +++++++++++++++++++++++++++++++++ tools/trace/trace.py | 52 ++++++++++++++++++++++++++++++++++++++++ 12 files changed, 274 insertions(+) create mode 100644 tools/trace/example/trace.trc create mode 100644 tools/trace/lib/__init__.py create mode 100644 tools/trace/lib/parser.g create mode 100644 tools/trace/lib/traceclass.py create mode 100644 tools/trace/tcreator/__init__.py create mode 100644 tools/trace/tcreator/tcreator.py create mode 100644 tools/trace/tcreator/template.h create mode 100644 tools/trace/tcreator/writer.py create mode 100644 tools/trace/tinter/__init__.py create mode 100644 tools/trace/tinter/thost.py create mode 100644 tools/trace/tinter/tinter.py create mode 100644 tools/trace/trace.py (limited to 'tools') diff --git a/tools/trace/example/trace.trc b/tools/trace/example/trace.trc new file mode 100644 index 00000000..db92823e --- /dev/null +++ b/tools/trace/example/trace.trc @@ -0,0 +1,3 @@ +EVENT asserv__right_motor speed 4 position 4 acceleration 2 "Right motor speed : %d, position : %d, acceleration : %d" +EVENT asserv__left_motor speed 4 position 4 acceleration 2 "Left motor speed : %d, position : %d, acceleration : %d" +EVENT ia__ia_cmd arg1 2 arg2 1 arg3 4 "IA cmd, arg1 : %d, arg2 : %d, arg3 : %d" diff --git a/tools/trace/lib/__init__.py b/tools/trace/lib/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tools/trace/lib/parser.g b/tools/trace/lib/parser.g new file mode 100644 index 00000000..2cee66b5 --- /dev/null +++ b/tools/trace/lib/parser.g @@ -0,0 +1,32 @@ +import sys + +from traceclass import * + +%% + +parser TraceParser: + ignore: "(#.*?)?\n" + token START: "^" + token EOF: "$" + token EVENT: "[a-zA-Z_]+" + token PARAM: "[a-zA-Z_1-9]+" + token LENGTH: "[1-2-4]" + token STRING: "\".*\"" + token SPACE: " " + + rule parser: START {{ my_list = list() }} + (event {{ my_list.append (event) }} + )* + EOF {{ return my_list }} + + rule event: "EVENT " EVENT {{ e = TraceEvent(EVENT.strip()) }} + SPACE + ( param SPACE {{ e.param_add (param[0], param[1]) }} + )* + ( string {{ e.string_set (string) }} + )* + "\n" {{ return e }} + + rule param: PARAM SPACE LENGTH {{ return [PARAM.strip(), int(LENGTH.strip())] }} + + rule string: STRING {{ return (STRING.strip()) }} diff --git a/tools/trace/lib/traceclass.py b/tools/trace/lib/traceclass.py new file mode 100644 index 00000000..3dd07142 --- /dev/null +++ b/tools/trace/lib/traceclass.py @@ -0,0 +1,43 @@ +# Define the trace Param. +class TraceParam: + def __init__ (self, name, length): + self.__name = name + + if (length == 0) or (length == 3) or (length > 4): + self.__length = 0 + raise Exception ("Length not permitted") + else: + self.__length = length + + def name (self): + return self.__name + + def length (self): + return self.__length + +# Defines the Events of the trace module. +class TraceEvent: + def __init__(self, name = ''): + self.__name = name + self.__param_list = list() + self.__string = "" + + def name (self): + return self.__name + + def param_add (self, name, length): + param = TraceParam (name, length) + self.__param_list.append(param) + + def param_get (self, pos): + if pos <= len (self.__param_list): + return self.__param_list[pos] + + def param_nb (self): + return len(self.__param_list) + + def string_set (self, string = ""): + self.__string = string + + def string_get (self): + return self.__string diff --git a/tools/trace/tcreator/__init__.py b/tools/trace/tcreator/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tools/trace/tcreator/tcreator.py b/tools/trace/tcreator/tcreator.py new file mode 100644 index 00000000..e0624259 --- /dev/null +++ b/tools/trace/tcreator/tcreator.py @@ -0,0 +1,27 @@ +import sys +from writer import * + +try: + from lib.parser import * +except: + print "--> You should run yapps on lib/parser.g" + +class TCreator: + + def __init__(self, infile, outfile, enum_name = "trace_id_t"): + self.__infile = infile + self.__outfile = outfile + self.__enum_name = enum_name + + def create (self): + infile = open (self.__infile, 'r') + data = parse ('parser', infile.read()) + infile.close() + + w = Writer (self.__outfile, self.__enum_name) + outstring = w.parse_list (data) + + if self.__outfile != "": + w.write_file (outstring) + else: + w.print_file (outstring) diff --git a/tools/trace/tcreator/template.h b/tools/trace/tcreator/template.h new file mode 100644 index 00000000..579acfad --- /dev/null +++ b/tools/trace/tcreator/template.h @@ -0,0 +1,33 @@ +#ifndef %%template%% +#define %%template%% +/* %%template%% */ +/* {{{ + * + * Copyright (C) 2008 APBTeam + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ + +enum %%enum_name%% +{ +%%data%% +}; + +#endif /* %%template%% */ diff --git a/tools/trace/tcreator/writer.py b/tools/trace/tcreator/writer.py new file mode 100644 index 00000000..8bfaf0fb --- /dev/null +++ b/tools/trace/tcreator/writer.py @@ -0,0 +1,39 @@ +import sys + +from lib.traceclass import * + +class Writer: + + """Template writer""" + def __init__ (self, name, enum_name = "trace_id"): + self.__name = name + self.__enum_name = enum_name + + def parse_list (self, event_list = None): + if event_list != None: + string = "" + for i in range (0, len (event_list)): + string += " TRACE_" + event_list[i].name() + ",\n" + string += " TRACE_NB" + return string.upper() + + def __read_template__ (self, string): + f = open ('tcreator/template.h', 'r') + template = f.read() + f.close() + define = self.__name.replace('.', '_') + template = template.replace('%%template%%', define) + template = template.replace('%%enum_name%%', self.__enum_name) + template = template.replace('%%data%%', string) + return template + + + def write_file (self, string): + template = self.__read_template__(string) + f = open (self.__name, 'w') + f.write (template) + f.close() + + def print_file (self, string): + template = self.__read_template__(string) + print template diff --git a/tools/trace/tinter/__init__.py b/tools/trace/tinter/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tools/trace/tinter/thost.py b/tools/trace/tinter/thost.py new file mode 100644 index 00000000..5608874c --- /dev/null +++ b/tools/trace/tinter/thost.py @@ -0,0 +1,2 @@ +def thost_dump_memory(): + return "000000000100000002000301000000020000000300040200FEDA0056789A" diff --git a/tools/trace/tinter/tinter.py b/tools/trace/tinter/tinter.py new file mode 100644 index 00000000..7c0a6d90 --- /dev/null +++ b/tools/trace/tinter/tinter.py @@ -0,0 +1,43 @@ +import sys +from thost import * + +try: + from lib.parser import * +except: + print "--> You should run yapps on lib/parser.g" + +class TInter: + + def __init__(self, infile): + self.__infile = infile + self.__events = None + + def __events_get (self): + infile = open (self.__infile, 'r') + events = parse ('parser', infile.read()) + infile.close() + return events + + def __event_print (self, events, memory): + if len(memory) > 0: + cmd = int (memory[0:2]) + e = events[cmd] + string = e.string_get() + memory = memory[2:len(memory)] + for i in range (0, e.param_nb()): + p = e.param_get(i) + size = p.length() * 2 + val = memory[0:size] + memory = memory[size:len(memory)] + string = string.replace('%d', str(int(val, 16)), 1) + return [memory, string] + + def trace_print (self): + events = self.__events_get () + memory = thost_dump_memory() + + while len(memory) > 0: + data = self.__event_print(events, memory) + string = data[1] + memory = data[0] + print string[1:len(string)-1] diff --git a/tools/trace/trace.py b/tools/trace/trace.py new file mode 100644 index 00000000..df327d9a --- /dev/null +++ b/tools/trace/trace.py @@ -0,0 +1,52 @@ +#!/bin/usr/pythonenv + +import sys +from tcreator.tcreator import * +from tinter.tinter import * + +def create_parse_args(list = None): + infile = "" + outfile = "" + enum_name = "" + if list == None: + return None + else: + for i in range (0, len(list)): + if list[i] == "-e": + enum_name = list[i+1] + if list[i] == "-o": + outfile = list[i+1] + else: + infile = list[i] + return [infile, outfile, enum_name] + + +print "Trace System v1.0 by APBTeam\n" + +try: + if len(sys.argv) > 1: + argc = len(sys.argv) + if sys.argv[1] == "create": + if (argc >= 2) and (argc <= 7): + data = create_parse_args(sys.argv) + cre = TCreator (data[0], data[1], data[2]) + cre.create () + else: + raise Exception ("Not enough arguments") + if sys.argv[1] == "inter": + if argc == 3: + inter = TInter (sys.argv[2]) + inter.trace_print() + else: + raise Exception ("Not enough arguments") + else: + raise Exception ("Not enough arguments") +except: + print "Trace system use..." + print "python trace.py create [options] " + print " Options : " + print " -e enum name" + print " -o file out name" + print "" + print "python trace.py inter " + print "" -- cgit v1.2.3