| 1 | #ifndef PARSER_H |
| 2 | #define PARSER_H |
| 3 | |
| 4 | /* Extensible array container */ |
| 5 | |
| 6 | typedef struct _sequence { |
| 7 | int size; |
| 8 | int position; |
| 9 | void **array; |
| 10 | } sequence_t; |
| 11 | |
| 12 | void sequence_init(sequence_t *t); |
| 13 | void sequence_dispose(sequence_t *t); |
| 14 | void sequence_push(sequence_t *t, void *elem); |
| 15 | void *sequence_pop(sequence_t *t); |
| 16 | |
| 17 | |
| 18 | /* Hash table */ |
| 19 | |
| 20 | typedef struct _table { |
| 21 | sequence_t keys; |
| 22 | sequence_t values; |
| 23 | } table_t; |
| 24 | |
| 25 | void table_init(table_t *t); |
| 26 | void table_dispose(table_t *t); |
| 27 | void table_insert(table_t *t, char *key, void *value); |
| 28 | void *table_find(table_t *t, char *key); |
| 29 | void table_insert_int(table_t *t, int *key, void *value); |
| 30 | void *table_find_int(table_t *t, int *key); |
| 31 | |
| 32 | |
| 33 | /* Token types */ |
| 34 | |
| 35 | typedef enum _token_type { |
| 36 | ENDFILE, |
| 37 | FORWARDSLASH, |
| 38 | LANGLEBRACKET, |
| 39 | RANGLEBRACKET, |
| 40 | EQUAL, |
| 41 | QUOTEDSTRING, |
| 42 | NUMBER, |
| 43 | NAME |
| 44 | } token_type_t; |
| 45 | |
| 46 | |
| 47 | /* State associated with a file being parsed */ |
| 48 | typedef struct _parse_file { |
| 49 | char *name; |
| 50 | FILE * fp; |
| 51 | int lineno; |
| 52 | char *buffer; |
| 53 | token_type_t type; |
| 54 | int unget; |
| 55 | void (*error) (struct _parse_file *, char *); |
| 56 | } parse_file_t; |
| 57 | |
| 58 | void ungetToken(parse_file_t * in); |
| 59 | char *getToken(parse_file_t *in); |
| 60 | char *getForwardslash(parse_file_t *in); |
| 61 | char *getLAnglebracket(parse_file_t *in); |
| 62 | char *getRAnglebracket(parse_file_t *in); |
| 63 | char *getQuotedString(parse_file_t *in); |
| 64 | char *getName(parse_file_t *in); |
| 65 | int getNumber(parse_file_t *in); |
| 66 | char *getEqual(parse_file_t *in); |
| 67 | char seekNextChar(parse_file_t *in); |
| 68 | |
| 69 | void skipComment(parse_file_t * in); |
| 70 | void skipEOL(parse_file_t * in); |
| 71 | |
| 72 | /* Some constants */ |
| 73 | |
| 74 | static const int BUFFER_SIZE = 1024; |
| 75 | |
| 76 | |
| 77 | /* Events data types */ |
| 78 | |
| 79 | typedef enum _data_type { |
| 80 | INT_FIXED, |
| 81 | UINT_FIXED, |
| 82 | POINTER, |
| 83 | CHAR, |
| 84 | UCHAR, |
| 85 | SHORT, |
| 86 | USHORT, |
| 87 | INT, |
| 88 | UINT, |
| 89 | LONG, |
| 90 | ULONG, |
| 91 | SIZE_T, |
| 92 | SSIZE_T, |
| 93 | OFF_T, |
| 94 | FLOAT, |
| 95 | STRING, |
| 96 | ENUM, |
| 97 | ARRAY, |
| 98 | SEQUENCE, |
| 99 | STRUCT, |
| 100 | UNION, |
| 101 | NONE |
| 102 | } data_type_t; |
| 103 | |
| 104 | typedef struct _type_descriptor { |
| 105 | char * type_name; //used for named type |
| 106 | data_type_t type; |
| 107 | char *fmt; |
| 108 | size_t size; |
| 109 | sequence_t labels; // for enumeration |
| 110 | sequence_t labels_values; // for enumeration |
| 111 | sequence_t labels_description; |
| 112 | int already_printed; |
| 113 | sequence_t fields; // for structure, array and sequence (field_t type) |
| 114 | int custom_write; /* Should we use a custom write function ? */ |
| 115 | int network; /* Is the type a in network byte order ? */ |
| 116 | } type_descriptor_t; |
| 117 | |
| 118 | |
| 119 | |
| 120 | /* Fields within types or events */ |
| 121 | typedef struct _field{ |
| 122 | char *name; |
| 123 | char *description; |
| 124 | type_descriptor_t *type; |
| 125 | } field_t; |
| 126 | |
| 127 | |
| 128 | /* Events definitions */ |
| 129 | |
| 130 | typedef struct _event { |
| 131 | char *name; |
| 132 | char *description; |
| 133 | //type_descriptor_t *type; |
| 134 | sequence_t fields; /* event fields */ |
| 135 | int per_trace; /* Is the event able to be logged to a specific trace ? */ |
| 136 | int per_tracefile; /* Must we log this event in a specific tracefile ? */ |
| 137 | int param_buffer; /* For userspace tracing : takes a buffer as parameter? */ |
| 138 | int no_instrument_function; |
| 139 | } event_t; |
| 140 | |
| 141 | typedef struct _facility { |
| 142 | char * name; |
| 143 | char * capname; |
| 144 | char * arch; |
| 145 | char * description; |
| 146 | sequence_t events; |
| 147 | sequence_t unnamed_types; //FIXME : remove |
| 148 | table_t named_types; |
| 149 | unsigned int checksum; |
| 150 | int user; /* Is this a userspace facility ? */ |
| 151 | } facility_t; |
| 152 | |
| 153 | int getSizeindex(unsigned int value); |
| 154 | unsigned long long int getSize(parse_file_t *in); |
| 155 | unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor_t * type); |
| 156 | |
| 157 | void parseFacility(parse_file_t *in, facility_t * fac); |
| 158 | void parseEvent(parse_file_t *in, event_t *ev, sequence_t * unnamed_types, |
| 159 | table_t * named_types); |
| 160 | void parseTypeDefinition(parse_file_t *in, |
| 161 | sequence_t * unnamed_types, table_t * named_types); |
| 162 | type_descriptor_t *parseType(parse_file_t *in, |
| 163 | type_descriptor_t *t, sequence_t * unnamed_types, table_t * named_types); |
| 164 | void parseFields(parse_file_t *in, field_t *f, |
| 165 | sequence_t * unnamed_types, |
| 166 | table_t * named_types, |
| 167 | int tag); |
| 168 | void checkNamedTypesImplemented(table_t * namedTypes); |
| 169 | type_descriptor_t * find_named_type(char *name, table_t * named_types); |
| 170 | void generateChecksum(char * facName, |
| 171 | unsigned int * checksum, sequence_t * events); |
| 172 | |
| 173 | |
| 174 | /* get attributes */ |
| 175 | char * getNameAttribute(parse_file_t *in); |
| 176 | char * getFormatAttribute(parse_file_t *in); |
| 177 | int getSizeAttribute(parse_file_t *in); |
| 178 | int getValueAttribute(parse_file_t *in, long long *value); |
| 179 | |
| 180 | char * getDescription(parse_file_t *in); |
| 181 | |
| 182 | |
| 183 | /* Dynamic memory allocation and freeing */ |
| 184 | |
| 185 | void * memAlloc(int size); |
| 186 | char *allocAndCopy(char * str); |
| 187 | char *appendString(char *s, char *suffix); |
| 188 | void freeTypes(sequence_t *t); |
| 189 | void freeType(type_descriptor_t * td); |
| 190 | void freeEvents(sequence_t *t); |
| 191 | void freeNamedType(table_t * t); |
| 192 | void error_callback(parse_file_t *in, char *msg); |
| 193 | |
| 194 | |
| 195 | //checksum part |
| 196 | static const unsigned int crctab32[] = |
| 197 | { |
| 198 | #include "crc32.tab" |
| 199 | }; |
| 200 | |
| 201 | static inline unsigned long |
| 202 | partial_crc32_one(unsigned char c, unsigned long crc) |
| 203 | { |
| 204 | return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8); |
| 205 | } |
| 206 | |
| 207 | static inline unsigned long |
| 208 | partial_crc32(const char *s, unsigned long crc) |
| 209 | { |
| 210 | while (*s) |
| 211 | crc = partial_crc32_one(*s++, crc); |
| 212 | return crc; |
| 213 | } |
| 214 | |
| 215 | static inline unsigned long |
| 216 | crc32(const char *s) |
| 217 | { |
| 218 | return partial_crc32(s, 0xffffffff) ^ 0xffffffff; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | extern char *intOutputTypes[]; |
| 223 | |
| 224 | extern char *uintOutputTypes[]; |
| 225 | |
| 226 | extern char *floatOutputTypes[]; |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | #endif // PARSER_H |