From 066d4d7809813de90d6dddc284fea505446d6418 Mon Sep 17 00:00:00 2001 From: Mark Pictor Date: Mon, 21 Aug 2017 23:53:21 -0400 Subject: [PATCH 1/4] initial work on pretty printers for libexpress --- src/base/gdb_script_autoload.h | 21 ++++++++++ src/express/CMakeLists.txt | 6 +++ src/express/express.c | 8 ++++ src/express/libexpress-gdb-printers.py | 55 ++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 src/base/gdb_script_autoload.h create mode 100644 src/express/libexpress-gdb-printers.py diff --git a/src/base/gdb_script_autoload.h b/src/base/gdb_script_autoload.h new file mode 100644 index 000000000..0e2464986 --- /dev/null +++ b/src/base/gdb_script_autoload.h @@ -0,0 +1,21 @@ +#ifndef GDB_SCRIPT_AUTOLOAD_H +#define GDB_SCRIPT_AUTOLOAD_H + + +// https://sourceware.org/gdb/onlinedocs/gdb/dotdebug_005fgdb_005fscripts-section.html, section 23.4.2.1 Script File Entries + +/* Embed a section that tells gdb to auto-load a python script. + * In spite of sourceware documentation to the contrary, it appears that gdb + * only tries the file name as absolute path and as something relative to the + * CWD - thus, script_name is practically useless if it isn't an absolute path. + * + * Note: The "MS" section flags are to remove duplicates. */ +#define DEFINE_GDB_PY_SCRIPT(script_name) \ + asm("\ +.pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1\n\ +.byte 1 /* Python */\n\ +.asciz \"" script_name "\"\n\ +.popsection \n\ +"); + +#endif //GDB_SCRIPT_AUTOLOAD_H diff --git a/src/express/CMakeLists.txt b/src/express/CMakeLists.txt index a63f2aa93..7ed306333 100644 --- a/src/express/CMakeLists.txt +++ b/src/express/CMakeLists.txt @@ -119,6 +119,12 @@ if(SC_GENERATE_LP_SOURCES) endif (TARGET express-static) endif(SC_GENERATE_LP_SOURCES) +# Adds a section to libexpress so gdb can find pretty printers. To take +# advantage, add 'add-auto-load-safe-path /path/to/stepcode' to ~/.gdbinit +if( NOT ${CMAKE_BUILD_TYPE} MATCHES "Release") + set_property(TARGET express APPEND PROPERTY COMPILE_DEFINITIONS EXPRESS_PYEMBEDPATH="${CMAKE_CURRENT_SOURCE_DIR}/libexpress-gdb-printers.py") +endif() + if(SC_GENERATE_LP_SOURCES) add_custom_command(TARGET express POST_BUILD COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/express_verify.cmake diff --git a/src/express/express.c b/src/express/express.c index 4ae48e86d..a17180546 100644 --- a/src/express/express.c +++ b/src/express/express.c @@ -86,6 +86,14 @@ #include "express/lexact.h" #include "express/exp_kw.h" +/* Adds a section to libexpress so gdb can find pretty printers. To take + advantage, add 'add-auto-load-safe-path /path/to/stepcode' to ~/.gdbinit +*/ +#if defined(EXPRESS_PYEMBEDPATH) +# include "gdb_script_autoload.h" + DEFINE_GDB_PY_SCRIPT(EXPRESS_PYEMBEDPATH) +#endif //EXPRESS_PYEMBEDPATH + void * ParseAlloc( void * ( *mallocProc )( size_t ) ); void ParseFree( void * parser, void ( *freeProc )( void * ) ); void Parse( void * parser, int tokenID, YYSTYPE data, parse_data_t parseData ); diff --git a/src/express/libexpress-gdb-printers.py b/src/express/libexpress-gdb-printers.py new file mode 100644 index 000000000..240fc0a46 --- /dev/null +++ b/src/express/libexpress-gdb-printers.py @@ -0,0 +1,55 @@ + +#https://www.rethinkdb.com/blog/make-debugging-easier-with-custom-pretty-printers/ + +class Variable: + def __init__(self, val): + self.val = val + def to_string(self): + t=Type(self.val['type']).to_string() + s=Symbol(self.val['name']['symbol']).to_string() + return t + ' ' + s + +class Symbol: + def __init__(self, val): + self.val = val + def to_string(self): + if not self.val: + return "" + fn = os.path.basename(get_char_star(self.val['filename'])) + res=' ' + if self.val['resolved']==0: + res='??' + name=get_char_star(self.val['name']) + #myvar ??[file.exp:123] (where ?? indicates not resolved) + v="{} {}[{}:{}]".format(name,res,fn,self.val['line']) + return v + +def get_char_star(val): + '''make possibly-null strings print sanely''' + if not val: + return '' + try: + return str(val.string()) + except: + return '' + +class Type: + def __init__(self, val): + self.val = val + def to_string(self): + if self.val['type'] != ord('t'): + return str(self.val['type']) + t = str(self.val['u']['type']['body']['type']) + return t[:len(t)-1].upper() + +import gdb.printing +import os.path + +def build_pretty_printer(): + pp = gdb.printing.RegexpCollectionPrettyPrinter("STEPcode - libexpress") + #pp.add_printer('Variable', '^Variable$', Variable) + pp.add_printer('Symbol', '\*?Symbol', Symbol) + pp.add_printer('Type', '^Type$', Type) + return pp + +gdb.printing.register_pretty_printer( gdb.current_objfile(), build_pretty_printer()) From 33a903b9efac95a8442796904b74b10395a35426 Mon Sep 17 00:00:00 2001 From: Mark Pictor Date: Mon, 21 Aug 2017 23:56:18 -0400 Subject: [PATCH 2/4] statement type converted from defines to enum so gdb can print types nicely --- include/express/stmt.h | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/include/express/stmt.h b/include/express/stmt.h index 2a2acd10e..33ffbdf9d 100644 --- a/include/express/stmt.h +++ b/include/express/stmt.h @@ -68,33 +68,28 @@ typedef struct Statement_ * Statement, typedef struct Scope_ * Increment; -/****************/ -/* modules used */ -/****************/ #include "expr.h" #include "alg.h" -/***************************/ -/* hidden type definitions */ -/***************************/ - -#define STMT_ASSIGN 0x1 -#define STMT_CASE 0x2 -#define STMT_COMPOUND 0x4 -#define STMT_COND 0x8 -#define STMT_LOOP 0x10 -#define STMT_PCALL 0x20 -#define STMT_RETURN 0x40 -#define STMT_ALIAS 0x80 -#define STMT_SKIP 0x100 -#define STMT_ESCAPE 0x200 +typedef enum { + STMT_ASSIGN = 0x1, + STMT_CASE = 0x2, + STMT_COMPOUND = 0x4, + STMT_COND = 0x8, + STMT_LOOP = 0x10, + STMT_PCALL = 0x20, + STMT_RETURN = 0x40, + STMT_ALIAS = 0x80, + STMT_SKIP = 0x100, + STMT_ESCAPE = 0x200 +} Statement_type; /* these should probably all be expression types */ struct Statement_ { Symbol symbol; /**< can hold pcall or alias name but otherwise is not used for anything */ - int type; /**< one of STMT_XXX above */ + Statement_type type; /**< one of STMT_XXX above */ /* hey, is there nothing in common beside symbol and private data?? */ union u_statement { struct Alias_ * alias; @@ -234,7 +229,7 @@ extern SC_EXPRESS_EXPORT Statement CONDcreate PROTO( ( Expression, Linked_Lis extern SC_EXPRESS_EXPORT Statement LOOPcreate PROTO( ( struct Scope_ *, Expression, Expression, Linked_List ) ); extern SC_EXPRESS_EXPORT Statement PCALLcreate PROTO( ( Linked_List ) ); extern SC_EXPRESS_EXPORT Statement RETcreate PROTO( ( Expression ) ); -extern SC_EXPRESS_EXPORT void STMTinitialize PROTO( ( void ) ); +extern SC_EXPRESS_EXPORT void STMTinitialize PROTO( ( void ) ); extern SC_EXPRESS_EXPORT struct Scope_ * INCR_CTLcreate PROTO( ( Symbol *, Expression start, Expression end, Expression increment ) ); From d042c5a6dcab828f0e3f0fccd4ff2e1c46951d33 Mon Sep 17 00:00:00 2001 From: Mark Pictor Date: Mon, 21 Aug 2017 23:57:09 -0400 Subject: [PATCH 3/4] add a few keywords to syntax highlighting file --- misc/kate-syntax-highlighting-express.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/misc/kate-syntax-highlighting-express.xml b/misc/kate-syntax-highlighting-express.xml index 22aa299d6..8ceec5d75 100644 --- a/misc/kate-syntax-highlighting-express.xml +++ b/misc/kate-syntax-highlighting-express.xml @@ -5,13 +5,15 @@ This is for Kate, the KDE editor. Rename to express.xml and place in the syntax directory (on Debian, this is /usr/share/kde4/apps/katepart/syntax ) --> - + of or + xor to where + unique subtype supertype abstract From 4a76e58f2cfdf8441dd2eb1250962b12ab3edb6a Mon Sep 17 00:00:00 2001 From: Mark Pictor Date: Mon, 21 Aug 2017 23:58:44 -0400 Subject: [PATCH 4/4] caught by cppcheck --- src/clstepcore/STEPattributeList.cc | 2 +- src/express/error.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/clstepcore/STEPattributeList.cc b/src/clstepcore/STEPattributeList.cc index 7774321b2..73efb1d87 100644 --- a/src/clstepcore/STEPattributeList.cc +++ b/src/clstepcore/STEPattributeList.cc @@ -45,7 +45,7 @@ STEPattribute & STEPattributeList::operator []( int n ) { // else cerr << "\nERROR in STEP Core library: " << __FILE__ << ":" << __LINE__ << "\n" << _POC_ << "\n\n"; - return *( STEPattribute * ) 0; + return nullptr; } int STEPattributeList::list_length() { diff --git a/src/express/error.c b/src/express/error.c index 996aae1df..fe509577a 100644 --- a/src/express/error.c +++ b/src/express/error.c @@ -362,6 +362,7 @@ va_dcl { what->message = "%s"; ERRORreport_with_symbol( what, &sym, buf ); what->message = savemsg; + va_end( args ); } void