LCOV - code coverage report
Current view: top level - src - Debug.h (source / functions) Hit Total Coverage
Test: app.info Lines: 7 16 43.8 %
Date: 2010-12-13 Functions: 9 25 36.0 %
Branches: 3 8 37.5 %

           Branch data     Line data    Source code
       1                 :            : // $Id: Debug.h 80 2004-07-14 20:15:50Z jason $
       2                 :            : 
       3                 :            : // Debugging support for Bro policy files.
       4                 :            : 
       5                 :            : #ifndef debug_h
       6                 :            : #define debug_h
       7                 :            : 
       8                 :            : #include <vector>
       9                 :            : #include <map>
      10                 :            : #include <string>
      11                 :            : using namespace std;
      12                 :            : 
      13                 :            : 
      14                 :            : class Stmt;
      15                 :            : 
      16                 :            : // This needs to be defined before we do the includes that come after it.
      17                 :            : enum ParseLocationRecType { plrUnknown, plrFileAndLine, plrFunction };
      18                 :          0 : struct ParseLocationRec {
      19                 :            :         ParseLocationRecType type;
      20                 :            :         Stmt* stmt;
      21                 :            :         const char* filename;
      22                 :            :         int line;
      23                 :            : };
      24                 :            : 
      25                 :            : #include "Expr.h"
      26                 :            : #include "Var.h"
      27                 :            : #include "Frame.h"
      28                 :            : #include "Queue.h"
      29                 :            : #include "Dict.h"
      30                 :            : #include "StmtEnums.h"
      31                 :            : #include "DbgBreakpoint.h"
      32                 :            : 
      33                 :            : class StmtLocMapping;
      34                 :          0 : declare(PQueue,StmtLocMapping);
      35                 :            : typedef PQueue(StmtLocMapping) Filemap; // mapping for a single file
      36 [ #  # ][ -  + ]:      18993 : declare(PDict,Filemap);
      37                 :            : 
      38                 :            : class DbgBreakpoint;
      39                 :            : class DbgWatch;
      40                 :            : class DbgDisplay;
      41                 :            : class StmtHashFn;
      42                 :            : 
      43                 :            : typedef map<int, DbgBreakpoint*> BPIDMapType;
      44                 :            : typedef multimap<const Stmt*, DbgBreakpoint*> BPMapType;
      45                 :            : 
      46                 :            : extern string current_module;
      47                 :            : 
      48                 :            : class TraceState {
      49                 :            : public:
      50                 :          3 :         TraceState()    { dbgtrace = false; trace_file = stderr; }
      51                 :            : 
      52                 :            :         // Returns previous filename.
      53                 :            :         FILE* SetTraceFile(const char* filename);
      54                 :            : 
      55                 :     418054 :         bool DoTrace() const    { return dbgtrace; }
      56                 :            :         void TraceOn();
      57                 :            :         void TraceOff();
      58                 :            : 
      59                 :            :         int LogTrace(const char* fmt, ...);
      60                 :            : 
      61                 :            : protected:
      62                 :            :         bool dbgtrace;          // print an execution trace
      63                 :            :         FILE* trace_file;
      64                 :            : };
      65                 :            : 
      66                 :            : extern TraceState g_trace_state;
      67                 :            : 
      68                 :            : class DebuggerState {
      69                 :            : public:
      70                 :            :         DebuggerState();
      71                 :            :         ~DebuggerState();
      72                 :            : 
      73                 :          0 :         int NextBPID()          { return next_bp_id++; }
      74                 :            :         int NextWatchID()       { return next_watch_id++; }
      75                 :            :         int NextDisplayID()     { return next_display_id++; }
      76                 :            : 
      77                 :          0 :         bool BreakBeforeNextStmt() { return break_before_next_stmt; }
      78                 :          3 :         void BreakBeforeNextStmt(bool dobrk) { break_before_next_stmt = dobrk; }
      79                 :            : 
      80                 :          0 :         bool BreakFromSignal() { return break_from_signal; }
      81                 :          3 :         void BreakFromSignal(bool dobrk) { break_from_signal = dobrk; }
      82                 :            : 
      83                 :            : 
      84                 :            :         // Temporary state: vanishes when execution resumes.
      85                 :            : 
      86                 :            :         //### Umesh, why do these all need to be public? -- Vern
      87                 :            : 
      88                 :            :         // Which frame we're looking at; 0 = the innermost frame.
      89                 :            :         int curr_frame_idx;
      90                 :            : 
      91                 :            :         bool already_did_list;  // did we already do a 'list' command?
      92                 :            : 
      93                 :            :         Location last_loc;      // used by 'list'; the last location listed
      94                 :            : 
      95                 :            :         BPIDMapType breakpoints;        // BPID -> Breakpoint
      96                 :            :         vector<DbgWatch*> watches;
      97                 :            :         vector<DbgDisplay*> displays;
      98                 :            :         BPMapType breakpoint_map;       // maps Stmt -> Breakpoints on it
      99                 :            : 
     100                 :            : protected:
     101                 :            :         bool break_before_next_stmt;    // trap into debugger (used for "step")
     102                 :            :         bool break_from_signal;         // was break caused by a signal?
     103                 :            : 
     104                 :            :         int next_bp_id, next_watch_id, next_display_id;
     105                 :            : 
     106                 :            : private:
     107                 :            :         Frame* dbg_locals; // unused
     108                 :            : };
     109                 :            : 
     110                 :            : // Source line -> statement mapping.
     111                 :            : // (obj -> source line mapping available in object itself)
     112                 :          0 : class StmtLocMapping {
     113                 :            : public:
     114                 :            :         StmtLocMapping()        { }
     115                 :          0 :         StmtLocMapping(const Location* l, Stmt* s)      { loc = *l; stmt = s; }
     116                 :            : 
     117                 :            :         bool StartsAfter(const StmtLocMapping* m2);
     118                 :          0 :         const Location& Loc() const { return loc; }
     119                 :          0 :         Stmt* Statement() const         { return stmt; }
     120                 :            : 
     121                 :            : protected:
     122                 :            :         Location loc;
     123                 :            :         Stmt* stmt;
     124                 :            : };
     125                 :            : 
     126                 :            : 
     127                 :            : extern bool g_policy_debug;             // enable debugging facility
     128                 :            : extern DebuggerState g_debugger_state;
     129                 :            : 
     130                 :            : //
     131                 :            : // Helper functions
     132                 :            : //
     133                 :            : 
     134                 :            : // parse_location_string() takes a string specifying a location by
     135                 :            : // filename and/or line number or function name and returns the
     136                 :            : // corresponding nearest statement, the actual filename and line
     137                 :            : // number specified (not the one corresponding to the nearest
     138                 :            : // statement) if applicable. The implicit filename is the one
     139                 :            : // containing the currently-debugged policy statement.
     140                 :            : // Multiple results can be returned depending on the input, but always
     141                 :            : // at least 1.
     142                 :            : 
     143                 :            : vector<ParseLocationRec> parse_location_string(const string& s);
     144                 :            : 
     145                 :            : // ### TODO: Add a bunch of hook functions for various events
     146                 :            : //   e.g. variable changed, breakpoint hit, etc.
     147                 :            : //
     148                 :            : //   Also add some hooks for UI? -- See GDB
     149                 :            : 
     150                 :            : 
     151                 :            : // Debugging hooks.
     152                 :            : 
     153                 :            : // Return true to continue execution, false to abort.
     154                 :            : bool pre_execute_stmt(Stmt* stmt, Frame* f);
     155                 :            : bool post_execute_stmt(Stmt* stmt, Frame* f, Val* result, stmt_flow_type* flow);
     156                 :            : 
     157                 :            : // Returns 1 if successful, 0 otherwise.
     158                 :            : // If cmdfile is non-nil, it contains the location of a file of commands
     159                 :            : // to be executed as debug commands.
     160                 :            : int dbg_init_debugger(const char* cmdfile = 0);
     161                 :            : int dbg_shutdown_debugger();
     162                 :            : 
     163                 :            : // Returns 1 if successful, 0 otherwise.
     164                 :            : int dbg_handle_debug_input();   // read a line and then have it executed
     165                 :            : 
     166                 :            : // Returns > 0 if execution should be resumed, 0 if another debug command
     167                 :            : // should be read, or < 0 if there was an error.
     168                 :            : int dbg_execute_command(const char* cmd);
     169                 :            : 
     170                 :            : // Interactive expression evaluation.
     171                 :            : Val* dbg_eval_expr(const char* expr);
     172                 :            : 
     173                 :            : // Extra debugging facilities.
     174                 :            : // TODO: current connections, memory allocated, other internal data structures.
     175                 :            : // ### Note: not currently defined.
     176                 :            : int dbg_read_internal_state();
     177                 :            : 
     178                 :            : // Get line that looks like "In FnFoo(arg = val) at File:Line".
     179                 :            : string get_context_description(const Stmt* stmt, const Frame* frame);
     180                 :            : 
     181                 :            : extern Frame* g_dbg_locals;     // variables created within debugger context
     182                 :            : 
     183                 :            : extern PDict(Filemap) g_dbgfilemaps; // filename => filemap
     184                 :            : 
     185                 :            : // Perhaps add a code/priority argument to do selective output.
     186 [ +  - ][ +  - ]:          6 : int debug_msg(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
     187                 :          3 : 
     188                 :            : #endif

Generated by: LCOV version 1.8