From f73367f88c2a8aaf2e1a6dbd969daddb667427eb Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Tue, 13 Jun 2017 18:35:16 -0400 Subject: [PATCH] plugins/ctf/common/metadata: logging: standardize in parser and lexer MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit YYDEBUG is set if the VERBOSE mode is enabled for the common metadata module. YYFPRINTF is defined to BT_LOGV so that everything uses the standard logging macros. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- plugins/ctf/common/metadata/Makefile.am | 1 + plugins/ctf/common/metadata/decoder.c | 9 +++ plugins/ctf/common/metadata/lexer.l | 18 ++++-- plugins/ctf/common/metadata/logging.h | 6 ++ plugins/ctf/common/metadata/parser.y | 81 +++++++++++++------------ plugins/ctf/lttng-live/metadata.c | 4 -- 6 files changed, 70 insertions(+), 49 deletions(-) diff --git a/plugins/ctf/common/metadata/Makefile.am b/plugins/ctf/common/metadata/Makefile.am index 1cd9e753..0a490f3b 100644 --- a/plugins/ctf/common/metadata/Makefile.am +++ b/plugins/ctf/common/metadata/Makefile.am @@ -10,6 +10,7 @@ libctf_parser_la_SOURCES = lexer.l parser.y objstack.c # with bt_. libctf_parser_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir) \ -include $(srcdir)/scanner-symbols.h +libctf_parser_la_CFLAGS = $(AM_CFLAGS) -Wno-unused-function libctf_ast_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir) libctf_ast_la_SOURCES = \ diff --git a/plugins/ctf/common/metadata/decoder.c b/plugins/ctf/common/metadata/decoder.c index c0cfa438..ddaeb02c 100644 --- a/plugins/ctf/common/metadata/decoder.c +++ b/plugins/ctf/common/metadata/decoder.c @@ -32,6 +32,9 @@ #define TSDL_MAGIC 0x75d11d57 +BT_HIDDEN +int yydebug; + struct ctf_metadata_decoder { struct ctf_visitor_generate_ir *visitor; uint8_t uuid[16]; @@ -362,6 +365,10 @@ enum ctf_metadata_decoder_status ctf_metadata_decoder_decode( } } + if (BT_LOG_ON_VERBOSE) { + yydebug = 1; + } + /* Allocate a scanner and append the metadata text content */ scanner = ctf_scanner_alloc(); if (!scanner) { @@ -406,6 +413,8 @@ end: ctf_scanner_free(scanner); } + yydebug = 0; + if (fp && close_fp) { if (fclose(fp)) { BT_LOGE("Cannot close metadata file stream"); diff --git a/plugins/ctf/common/metadata/lexer.l b/plugins/ctf/common/metadata/lexer.l index d805a434..694ddc3a 100644 --- a/plugins/ctf/common/metadata/lexer.l +++ b/plugins/ctf/common/metadata/lexer.l @@ -25,20 +25,26 @@ * SOFTWARE. */ +#define BT_LOG_TAG "PLUGIN-CTF-METADATA-LEXER" +#include "logging.h" + #include #include -#include #include "scanner.h" #include "parser.h" #include "ast.h" +#define YY_FATAL_ERROR(_msg) BT_LOGF_STR(_msg) + #define PARSE_INTEGER_LITERAL(base) \ do { \ errno = 0; \ yylval->ull = strtoull(yytext, NULL, base); \ if (errno) { \ - printfl_perror(yylineno, "Integer literal"); \ - return CTF_ERROR; \ + _BT_LOGE_LINENO(yylineno, \ + "Cannot parser constant integer: " \ + "base=%d, text=\"%s\"", base, yytext); \ + return CTF_ERROR; \ } \ } while (0) @@ -72,7 +78,7 @@ IDENTIFIER {ID_NONDIGIT}({ID_NONDIGIT}|{DIGIT})* /* * Using start conditions to deal with comments * and strings. - */ + */ "/*" BEGIN(comment_ml); [^*\n]* /* eat anything that's not a '*' */ @@ -136,7 +142,7 @@ _Imaginary setstring(yyextra, yylval, yytext); return CTF_IMAGINARY; 0{OCTALDIGIT}*{INTEGER_SUFFIX}? PARSE_INTEGER_LITERAL(8); return CTF_INTEGER_LITERAL; 0[xX]{HEXDIGIT}+{INTEGER_SUFFIX}? PARSE_INTEGER_LITERAL(16); return CTF_INTEGER_LITERAL; -{IDENTIFIER} printf_debug("\n", yytext); setstring(yyextra, yylval, yytext); if (is_type(yyextra, yytext)) return ID_TYPE; else return IDENTIFIER; +{IDENTIFIER} BT_LOGV("Got identifier: id=\"%s\"", yytext); setstring(yyextra, yylval, yytext); if (is_type(yyextra, yytext)) return ID_TYPE; else return IDENTIFIER; [ \t\r\n] ; /* ignore */ -. printfl_error(yylineno, "invalid character '0x%02X'", yytext[0]); return CTF_ERROR; +. _BT_LOGE_LINENO(yylineno, "Invalid character: char=\"%c\", val=0x%02x", isprint(yytext[0]) ? yytext[0] : '\0', yytext[0]); return CTF_ERROR; %% diff --git a/plugins/ctf/common/metadata/logging.h b/plugins/ctf/common/metadata/logging.h index 5ecbdb13..78a41a66 100644 --- a/plugins/ctf/common/metadata/logging.h +++ b/plugins/ctf/common/metadata/logging.h @@ -28,4 +28,10 @@ BT_LOG_LEVEL_EXTERN_SYMBOL(metadata_log_level); +#define _BT_LOGW_LINENO(_lineno, _msg, args...) \ + BT_LOGW("At line %u in metadata stream: " _msg, _lineno, ## args) + +#define _BT_LOGE_LINENO(_lineno, _msg, args...) \ + BT_LOGE("At line %u in metadata stream: " _msg, _lineno, ## args) + #endif /* CTF_METADATA_LOGGING_H */ diff --git a/plugins/ctf/common/metadata/parser.y b/plugins/ctf/common/metadata/parser.y index 02203aeb..83080063 100644 --- a/plugins/ctf/common/metadata/parser.y +++ b/plugins/ctf/common/metadata/parser.y @@ -25,6 +25,9 @@ * SOFTWARE. */ +#define BT_LOG_TAG "PLUGIN-CTF-METADATA-PARSER" +#include "logging.h" + #include #include #include @@ -35,14 +38,17 @@ #include #include #include -#include #include "scanner.h" #include "parser.h" #include "ast.h" #include "objstack.h" -BT_HIDDEN -int yydebug; +#if BT_LOG_ENABLED_VERBOSE +# define YYDEBUG 1 +# define YYFPRINTF(_stream, _fmt, args...) BT_LOGV(_fmt, ## args) +#else +# define YYDEBUG 0 +#endif /* Join two lists, put "add" at the end of "head". */ static inline void @@ -284,8 +290,9 @@ int import_string(struct ctf_scanner *scanner, YYSTYPE *lvalp, lvalp->s = objstack_alloc(scanner->objstack, len); if (src[0] == 'L') { // TODO: import wide string - printfl_error(yyget_lineno(scanner), - "Wide string not supported yet."); + _BT_LOGE_LINENO(yyget_lineno(scanner), + "wide characters are not supported as of this version: " + "scanner-addr=%p", scanner); return -1; } else { return import_basic_string(scanner, lvalp, len, src, delim); @@ -309,7 +316,7 @@ static void push_scope(struct ctf_scanner *scanner) { struct ctf_scanner_scope *ns; - printf_debug("push scope\n"); + BT_LOGV("Pushing scope: scanner-addr=%p", scanner); ns = malloc(sizeof(struct ctf_scanner_scope)); init_scope(ns, scanner->cs); scanner->cs = ns; @@ -319,7 +326,7 @@ static void pop_scope(struct ctf_scanner *scanner) { struct ctf_scanner_scope *os; - printf_debug("pop scope\n"); + BT_LOGV("Popping scope: scanner-addr=%p", scanner); os = scanner->cs; scanner->cs = os->parent; finalize_scope(os); @@ -331,7 +338,8 @@ static int lookup_type(struct ctf_scanner_scope *s, const char *id) int ret; ret = GPOINTER_TO_INT(g_hash_table_lookup(s->types, id)); - printf_debug("lookup %p %s %d\n", s, id, ret); + BT_LOGV("Looked up type: scanner-addr=%p, id=\"%s\", ret=%d", + s, id, ret); return ret; } @@ -347,13 +355,15 @@ int is_type(struct ctf_scanner *scanner, const char *id) break; } } - printf_debug("is type %s %d\n", id, ret); + BT_LOGV("Found if ID is type: scanner-addr=%p, id=\"%s\", ret=%d", + scanner, id, ret); return ret; } static void add_type(struct ctf_scanner *scanner, char *id) { - printf_debug("add type %s\n", id); + BT_LOGV("Adding type: scanner-addr=%p, id=\"%s\"", + scanner, id); if (lookup_type(scanner->cs, id)) return; g_hash_table_insert(scanner->cs->types, id, id); @@ -366,7 +376,9 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner, node = objstack_alloc(scanner->objstack, sizeof(*node)); if (!node) { - printfl_fatal(yyget_lineno(scanner->scanner), "out of memory"); + _BT_LOGE_LINENO(yyget_lineno(scanner->scanner), + "failed to allocate one stack entry: " + "scanner-addr=%p", scanner); return &error_node; } node->type = type; @@ -377,9 +389,9 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner, switch (type) { case NODE_ROOT: node->type = NODE_ERROR; - printfn_fatal(node, "trying to create root node"); + BT_LOGE("Trying to create root node: scanner-addr=%p", + scanner); break; - case NODE_EVENT: BT_INIT_LIST_HEAD(&node->u.event.declaration_list); break; @@ -398,14 +410,12 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner, case NODE_CALLSITE: BT_INIT_LIST_HEAD(&node->u.callsite.declaration_list); break; - case NODE_CTF_EXPRESSION: BT_INIT_LIST_HEAD(&node->u.ctf_expression.left); BT_INIT_LIST_HEAD(&node->u.ctf_expression.right); break; case NODE_UNARY_EXPRESSION: break; - case NODE_TYPEDEF: BT_INIT_LIST_HEAD(&node->u._typedef.type_declarators); break; @@ -417,7 +427,6 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner, break; case NODE_TYPEALIAS: break; - case NODE_TYPE_SPECIFIER: break; case NODE_TYPE_SPECIFIER_LIST: @@ -428,7 +437,6 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner, case NODE_TYPE_DECLARATOR: BT_INIT_LIST_HEAD(&node->u.type_declarator.pointers); break; - case NODE_FLOATING_POINT: BT_INIT_LIST_HEAD(&node->u.floating_point.expressions); break; @@ -454,11 +462,11 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner, BT_INIT_LIST_HEAD(&node->u._struct.declaration_list); BT_INIT_LIST_HEAD(&node->u._struct.min_align); break; - case NODE_UNKNOWN: default: node->type = NODE_ERROR; - printfn_fatal(node, "unknown node type '%d'", (int) type); + BT_LOGE("Unknown node type: scanner-addr=%p, node-type=%d", + scanner, type); break; } @@ -517,7 +525,7 @@ static int reparent_ctf_expression(struct ctf_node *node, case NODE_UNKNOWN: default: - printfn_fatal(node, "unknown node type '%d'", (int) parent->type); + BT_LOGE("Unknown node type: node-type=%d", parent->type); return -EINVAL; } return 0; @@ -574,7 +582,7 @@ static int reparent_typedef(struct ctf_node *node, struct ctf_node *parent) case NODE_UNKNOWN: default: - printfn_fatal(node, "unknown node type %d", parent->type); + BT_LOGE("Unknown node type: node-type=%d", parent->type); return -EINVAL; } return 0; @@ -631,7 +639,7 @@ static int reparent_typealias(struct ctf_node *node, struct ctf_node *parent) case NODE_UNKNOWN: default: - printfn_fatal(node, "unknown node type '%d'", (int) parent->type); + BT_LOGE("Unknown node type: node-type=%d", parent->type); return -EINVAL; } return 0; @@ -672,7 +680,7 @@ static int reparent_type_specifier(struct ctf_node *node, case NODE_UNKNOWN: default: - printfn_fatal(node, "unknown node type '%d'", (int) parent->type); + BT_LOGE("Unknown node type: node-type=%d", parent->type); return -EINVAL; } return 0; @@ -738,7 +746,7 @@ static int reparent_type_specifier_list(struct ctf_node *node, case NODE_UNKNOWN: default: - printfn_fatal(node, "unknown node type '%d'", (int) parent->type); + BT_LOGE("Unknown node type: node-type=%d", parent->type); return -EINVAL; } return 0; @@ -789,7 +797,7 @@ static int reparent_type_declarator(struct ctf_node *node, case NODE_UNKNOWN: default: - printfn_fatal(node, "unknown node type '%d'", (int) parent->type); + BT_LOGE("Unknown node type: node-type=%d", parent->type); return -EINVAL; } return 0; @@ -812,7 +820,7 @@ static int set_parent_node(struct ctf_node *node, switch (node->type) { case NODE_ROOT: - printfn_fatal(node, "trying to reparent root node"); + BT_LOGE_STR("Trying to reparent root node."); return -EINVAL; case NODE_EVENT: @@ -927,7 +935,7 @@ static int set_parent_node(struct ctf_node *node, case NODE_UNKNOWN: default: - printfn_fatal(node, "unknown node type '%d'", (int) parent->type); + BT_LOGE("Unknown node type: node-type=%d", parent->type); return -EINVAL; } return 0; @@ -936,9 +944,8 @@ static int set_parent_node(struct ctf_node *node, BT_HIDDEN void yyerror(struct ctf_scanner *scanner, yyscan_t yyscanner, const char *str) { - printfl_error(yyget_lineno(scanner->scanner), - "token \"%s\": %s\n", - yyget_text(scanner->scanner), str); + _BT_LOGE_LINENO(yyget_lineno(scanner->scanner), + "%s: token=\"%s\"", str, yyget_text(scanner->scanner)); } BT_HIDDEN @@ -976,10 +983,6 @@ int ctf_scanner_append_ast(struct ctf_scanner *scanner, FILE *input) { /* Start processing new stream */ yyrestart(input, scanner->scanner); - if (yydebug) - fprintf(stdout, "Scanner input is a%s.\n", - isatty(fileno(input)) ? "n interactive tty" : - " noninteractive file"); return yyparse(scanner, scanner->scanner); } @@ -988,15 +991,13 @@ struct ctf_scanner *ctf_scanner_alloc(void) struct ctf_scanner *scanner; int ret; - yydebug = babeltrace_debug; - scanner = malloc(sizeof(*scanner)); if (!scanner) return NULL; memset(scanner, 0, sizeof(*scanner)); ret = yylex_init_extra(scanner, &scanner->scanner); if (ret) { - printf_fatal("yylex_init error"); + BT_LOGE("yylex_init_extra() failed: ret=%d", ret); goto cleanup_scanner; } scanner->objstack = objstack_create(); @@ -1015,7 +1016,8 @@ cleanup_objstack: cleanup_lexer: ret = yylex_destroy(scanner->scanner); if (!ret) - printf_fatal("yylex_destroy error"); + BT_LOGE("yylex_destroy() failed: scanner-addr=%p, ret=%d", + scanner, ret); cleanup_scanner: free(scanner); return NULL; @@ -1031,7 +1033,8 @@ void ctf_scanner_free(struct ctf_scanner *scanner) objstack_destroy(scanner->objstack); ret = yylex_destroy(scanner->scanner); if (ret) - printf_error("yylex_destroy error"); + BT_LOGE("yylex_destroy() failed: scanner-addr=%p, ret=%d", + scanner, ret); free(scanner); } diff --git a/plugins/ctf/lttng-live/metadata.c b/plugins/ctf/lttng-live/metadata.c index ffc775d0..1adc81c1 100644 --- a/plugins/ctf/lttng-live/metadata.c +++ b/plugins/ctf/lttng-live/metadata.c @@ -185,10 +185,6 @@ enum bt_ctf_lttng_live_iterator_status lttng_live_metadata_update( goto end; } - if (babeltrace_debug) { - // yydebug = 1; - } - fp = bt_fmemopen(metadata_buf, len_read, "rb"); if (!fp) { BT_LOGE("Cannot memory-open metadata buffer: %s", -- 2.34.1