Lib
QOLを高める
script_debugger.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "util.h"
4 #include <lua.h>
5 
6 namespace yappy {
7 namespace lua {
9 namespace debugger {
10 
15 class LuaDebugger : private util::noncopyable {
16 public:
23  LuaDebugger(lua_State *L, bool debugEnable, int instLimit, size_t heapSize);
24 
28  lua_State *getLuaState() const;
34  void loadDebugInfo(const char *name, const char *src, size_t size);
40  void pcall(int narg, int nret, bool autoBreak);
41 
42  bool help(const wchar_t *usage, const std::vector<std::wstring> &args);
43  bool conf(const wchar_t *usage, const std::vector<std::wstring> &args);
44  bool mem(const wchar_t *usage, const std::vector<std::wstring> &args);
45  bool bt(const wchar_t *usage, const std::vector<std::wstring> &args);
46  bool fr(const wchar_t *usage, const std::vector<std::wstring> &args);
47  bool src(const wchar_t *usage, const std::vector<std::wstring> &args);
48  bool eval(const wchar_t *usage, const std::vector<std::wstring> &args);
49  bool watch(const wchar_t *usage, const std::vector<std::wstring> &args);
50  bool c(const wchar_t *usage, const std::vector<std::wstring> &args);
51  bool s(const wchar_t *usage, const std::vector<std::wstring> &args);
52  bool n(const wchar_t *usage, const std::vector<std::wstring> &args);
53  bool out(const wchar_t *usage, const std::vector<std::wstring> &args);
54  bool bp(const wchar_t *usage, const std::vector<std::wstring> &args);
55 
56 private:
57  struct ChunkDebugInfo {
58  // must be unique
59  std::string chunkName;
60  // source code lines array
61  std::vector<std::string> srcLines;
62  // each line is valid? (can put breakpoint?)
63  std::vector<uint8_t> validLines;
64  // breakpoints
65  std::vector<uint8_t> breakPoints;
66 
67  // Cannot copy, move only
68  ChunkDebugInfo() = default;
69  ChunkDebugInfo(const ChunkDebugInfo &) = delete;
70  ChunkDebugInfo &operator=(const ChunkDebugInfo &) = delete;
71  ChunkDebugInfo(ChunkDebugInfo &&) = default;
72  ChunkDebugInfo &operator=(ChunkDebugInfo &&) = default;
73  };
74 
75  enum class DebugState {
76  CONT, // only bp or error
77  BREAK_LINE_ANY, // line event any
78  BREAK_LINE_DEPTH0, // line event, if call-ret depth == 0
79  };
80 
81  static const int DefSrcLines = 21;
82  static const int DefTableDepth = 3;
83 
84  lua_State *m_L;
85  bool m_debugEnable;
86  int m_instLimit;
87  size_t m_heapSize;
88 
89  std::unordered_map<std::string, ChunkDebugInfo> m_debugInfo;
90  std::string m_fileNameStr;
91  DebugState m_debugState = DebugState::CONT;
92  int m_callDepth = 0;
93  int m_currentFrame = 0;
94  std::vector<std::string> m_watchList;
95 
96  void hook(lua_Debug *ar);
97  void hookNonDebug(lua_Debug *ar);
98  void hookDebug(lua_Debug *ar);
99 
100  static int msghandler(lua_State *L);
101  static void hookRaw(lua_State *L, lua_Debug *ar);
102 
103  void cmdLoop();
104  void summaryOnBreak(lua_Debug *ar);
105  void printSrcLines(const std::string &name, int line, int range, int execLine = -1);
106  void printLocalAndUpvalue(lua_Debug *ar, int maxDepth, bool skipNoName);
107  void pushLocalEnv(lua_Debug *ar, int frameNo);
108  void printEval(const std::string &expr);
109  void printWatchList();
110 };
111 
112 } // namespace debugger
113 } // namespace lua
114 } // namespace yappy
bool c(const wchar_t *usage, const std::vector< std::wstring > &args)
Noncopyable class.
Definition: util.h:24
lua_State * getLuaState() const
Get Lua state.
const wchar_t * usage
bool fr(const wchar_t *usage, const std::vector< std::wstring > &args)
bool out(const wchar_t *usage, const std::vector< std::wstring > &args)
bool help(const wchar_t *usage, const std::vector< std::wstring > &args)
bool src(const wchar_t *usage, const std::vector< std::wstring > &args)
Definition: config.cpp:6
noncopyable & operator=(const noncopyable &)=delete
void loadDebugInfo(const char *name, const char *src, size_t size)
Load debug info from chunk name and source string.
bool watch(const wchar_t *usage, const std::vector< std::wstring > &args)
bool conf(const wchar_t *usage, const std::vector< std::wstring > &args)
Utilities.
bool bp(const wchar_t *usage, const std::vector< std::wstring > &args)
bool s(const wchar_t *usage, const std::vector< std::wstring > &args)
LuaDebugger(lua_State *L, bool debugEnable, int instLimit, size_t heapSize)
Constructor.
bool mem(const wchar_t *usage, const std::vector< std::wstring > &args)
bool eval(const wchar_t *usage, const std::vector< std::wstring > &args)
void pcall(int narg, int nret, bool autoBreak)
Prepare and call lua_pcall().
bool bt(const wchar_t *usage, const std::vector< std::wstring > &args)
bool n(const wchar_t *usage, const std::vector< std::wstring > &args)