xref: /netbsd-src/external/bsd/lutok/dist/debug.cpp (revision a15637525a498a84b68d8b44b1c7ec01de4b564a)
1*a1563752Sjmmv // Copyright 2011 Google Inc.
2*a1563752Sjmmv // All rights reserved.
3*a1563752Sjmmv //
4*a1563752Sjmmv // Redistribution and use in source and binary forms, with or without
5*a1563752Sjmmv // modification, are permitted provided that the following conditions are
6*a1563752Sjmmv // met:
7*a1563752Sjmmv //
8*a1563752Sjmmv // * Redistributions of source code must retain the above copyright
9*a1563752Sjmmv //   notice, this list of conditions and the following disclaimer.
10*a1563752Sjmmv // * Redistributions in binary form must reproduce the above copyright
11*a1563752Sjmmv //   notice, this list of conditions and the following disclaimer in the
12*a1563752Sjmmv //   documentation and/or other materials provided with the distribution.
13*a1563752Sjmmv // * Neither the name of Google Inc. nor the names of its contributors
14*a1563752Sjmmv //   may be used to endorse or promote products derived from this software
15*a1563752Sjmmv //   without specific prior written permission.
16*a1563752Sjmmv //
17*a1563752Sjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*a1563752Sjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*a1563752Sjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*a1563752Sjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*a1563752Sjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*a1563752Sjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*a1563752Sjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*a1563752Sjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*a1563752Sjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*a1563752Sjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*a1563752Sjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*a1563752Sjmmv 
29*a1563752Sjmmv #include <cassert>
30*a1563752Sjmmv 
31*a1563752Sjmmv #include <lua.hpp>
32*a1563752Sjmmv 
33*a1563752Sjmmv #include <lutok/c_gate.hpp>
34*a1563752Sjmmv #include <lutok/debug.hpp>
35*a1563752Sjmmv #include <lutok/exceptions.hpp>
36*a1563752Sjmmv #include <lutok/state.ipp>
37*a1563752Sjmmv 
38*a1563752Sjmmv 
39*a1563752Sjmmv /// Internal implementation for lutok::debug.
40*a1563752Sjmmv struct lutok::debug::impl {
41*a1563752Sjmmv     /// The Lua internal debug state.
42*a1563752Sjmmv     lua_Debug lua_debug;
43*a1563752Sjmmv };
44*a1563752Sjmmv 
45*a1563752Sjmmv 
46*a1563752Sjmmv /// Constructor for an empty debug structure.
debug(void)47*a1563752Sjmmv lutok::debug::debug(void) :
48*a1563752Sjmmv     _pimpl(new impl())
49*a1563752Sjmmv {
50*a1563752Sjmmv }
51*a1563752Sjmmv 
52*a1563752Sjmmv 
53*a1563752Sjmmv /// Destructor.
~debug(void)54*a1563752Sjmmv lutok::debug::~debug(void)
55*a1563752Sjmmv {
56*a1563752Sjmmv }
57*a1563752Sjmmv 
58*a1563752Sjmmv 
59*a1563752Sjmmv /// Wrapper around lua_getinfo.
60*a1563752Sjmmv ///
61*a1563752Sjmmv /// \param s The Lua state.
62*a1563752Sjmmv /// \param what_ The second parameter to lua_getinfo.
63*a1563752Sjmmv ///
64*a1563752Sjmmv /// \warning Terminates execution if there is not enough memory to manipulate
65*a1563752Sjmmv /// the Lua stack.
66*a1563752Sjmmv void
get_info(state & s,const std::string & what_)67*a1563752Sjmmv lutok::debug::get_info(state& s, const std::string& what_)
68*a1563752Sjmmv {
69*a1563752Sjmmv     lua_State* raw_state = state_c_gate(s).c_state();
70*a1563752Sjmmv 
71*a1563752Sjmmv     if (lua_getinfo(raw_state, what_.c_str(), &_pimpl->lua_debug) == 0)
72*a1563752Sjmmv         throw lutok::api_error::from_stack(s, "lua_getinfo");
73*a1563752Sjmmv }
74*a1563752Sjmmv 
75*a1563752Sjmmv 
76*a1563752Sjmmv /// Wrapper around lua_getstack.
77*a1563752Sjmmv ///
78*a1563752Sjmmv /// \param s The Lua state.
79*a1563752Sjmmv /// \param level The second parameter to lua_getstack.
80*a1563752Sjmmv void
get_stack(state & s,const int level)81*a1563752Sjmmv lutok::debug::get_stack(state& s, const int level)
82*a1563752Sjmmv {
83*a1563752Sjmmv     lua_State* raw_state = state_c_gate(s).c_state();
84*a1563752Sjmmv 
85*a1563752Sjmmv     lua_getstack(raw_state, level, &_pimpl->lua_debug);
86*a1563752Sjmmv }
87*a1563752Sjmmv 
88*a1563752Sjmmv 
89*a1563752Sjmmv /// Accessor for the 'event' field of lua_Debug.
90*a1563752Sjmmv ///
91*a1563752Sjmmv /// \return Returns the 'event' field of the internal lua_Debug structure.
92*a1563752Sjmmv int
event(void) const93*a1563752Sjmmv lutok::debug::event(void) const
94*a1563752Sjmmv {
95*a1563752Sjmmv     return _pimpl->lua_debug.event;
96*a1563752Sjmmv }
97*a1563752Sjmmv 
98*a1563752Sjmmv 
99*a1563752Sjmmv /// Accessor for the 'name' field of lua_Debug.
100*a1563752Sjmmv ///
101*a1563752Sjmmv /// \return Returns the 'name' field of the internal lua_Debug structure.
102*a1563752Sjmmv std::string
name(void) const103*a1563752Sjmmv lutok::debug::name(void) const
104*a1563752Sjmmv {
105*a1563752Sjmmv     assert(_pimpl->lua_debug.name != NULL);
106*a1563752Sjmmv     return _pimpl->lua_debug.name;
107*a1563752Sjmmv }
108*a1563752Sjmmv 
109*a1563752Sjmmv 
110*a1563752Sjmmv /// Accessor for the 'namewhat' field of lua_Debug.
111*a1563752Sjmmv ///
112*a1563752Sjmmv /// \return Returns the 'namewhat' field of the internal lua_Debug structure.
113*a1563752Sjmmv std::string
name_what(void) const114*a1563752Sjmmv lutok::debug::name_what(void) const
115*a1563752Sjmmv {
116*a1563752Sjmmv     assert(_pimpl->lua_debug.namewhat != NULL);
117*a1563752Sjmmv     return _pimpl->lua_debug.namewhat;
118*a1563752Sjmmv }
119*a1563752Sjmmv 
120*a1563752Sjmmv 
121*a1563752Sjmmv /// Accessor for the 'what' field of lua_Debug.
122*a1563752Sjmmv ///
123*a1563752Sjmmv /// \return Returns the 'what' field of the internal lua_Debug structure.
124*a1563752Sjmmv std::string
what(void) const125*a1563752Sjmmv lutok::debug::what(void) const
126*a1563752Sjmmv {
127*a1563752Sjmmv     assert(_pimpl->lua_debug.what != NULL);
128*a1563752Sjmmv     return _pimpl->lua_debug.what;
129*a1563752Sjmmv }
130*a1563752Sjmmv 
131*a1563752Sjmmv 
132*a1563752Sjmmv /// Accessor for the 'source' field of lua_Debug.
133*a1563752Sjmmv ///
134*a1563752Sjmmv /// \return Returns the 'source' field of the internal lua_Debug structure.
135*a1563752Sjmmv std::string
source(void) const136*a1563752Sjmmv lutok::debug::source(void) const
137*a1563752Sjmmv {
138*a1563752Sjmmv     assert(_pimpl->lua_debug.source != NULL);
139*a1563752Sjmmv     return _pimpl->lua_debug.source;
140*a1563752Sjmmv }
141*a1563752Sjmmv 
142*a1563752Sjmmv 
143*a1563752Sjmmv /// Accessor for the 'currentline' field of lua_Debug.
144*a1563752Sjmmv ///
145*a1563752Sjmmv /// \return Returns the 'currentline' field of the internal lua_Debug structure.
146*a1563752Sjmmv int
current_line(void) const147*a1563752Sjmmv lutok::debug::current_line(void) const
148*a1563752Sjmmv {
149*a1563752Sjmmv     return _pimpl->lua_debug.currentline;
150*a1563752Sjmmv }
151*a1563752Sjmmv 
152*a1563752Sjmmv 
153*a1563752Sjmmv /// Accessor for the 'nups' field of lua_Debug.
154*a1563752Sjmmv ///
155*a1563752Sjmmv /// \return Returns the 'nups' field of the internal lua_Debug structure.
156*a1563752Sjmmv int
n_ups(void) const157*a1563752Sjmmv lutok::debug::n_ups(void) const
158*a1563752Sjmmv {
159*a1563752Sjmmv     return _pimpl->lua_debug.nups;
160*a1563752Sjmmv }
161*a1563752Sjmmv 
162*a1563752Sjmmv 
163*a1563752Sjmmv /// Accessor for the 'linedefined' field of lua_Debug.
164*a1563752Sjmmv ///
165*a1563752Sjmmv /// \return Returns the 'linedefined' field of the internal lua_Debug structure.
166*a1563752Sjmmv int
line_defined(void) const167*a1563752Sjmmv lutok::debug::line_defined(void) const
168*a1563752Sjmmv {
169*a1563752Sjmmv     return _pimpl->lua_debug.linedefined;
170*a1563752Sjmmv }
171*a1563752Sjmmv 
172*a1563752Sjmmv 
173*a1563752Sjmmv /// Accessor for the 'lastlinedefined' field of lua_Debug.
174*a1563752Sjmmv ///
175*a1563752Sjmmv /// \return Returns the 'lastlinedefined' field of the internal lua_Debug
176*a1563752Sjmmv /// structure.
177*a1563752Sjmmv int
last_line_defined(void) const178*a1563752Sjmmv lutok::debug::last_line_defined(void) const
179*a1563752Sjmmv {
180*a1563752Sjmmv     return _pimpl->lua_debug.lastlinedefined;
181*a1563752Sjmmv }
182*a1563752Sjmmv 
183*a1563752Sjmmv 
184*a1563752Sjmmv /// Accessor for the 'short_src' field of lua_Debug.
185*a1563752Sjmmv ///
186*a1563752Sjmmv /// \return Returns the 'short_src' field of the internal lua_Debug structure.
187*a1563752Sjmmv std::string
short_src(void) const188*a1563752Sjmmv lutok::debug::short_src(void) const
189*a1563752Sjmmv {
190*a1563752Sjmmv     assert(_pimpl->lua_debug.short_src != NULL);
191*a1563752Sjmmv     return _pimpl->lua_debug.short_src;
192*a1563752Sjmmv }
193