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