xref: /src/contrib/lutok/debug.cpp (revision c697fb7f7cc9bedc5beee44d35b771c4e87b335a)
14b2c3eb9SBrooks Davis // Copyright 2011 Google Inc.
24b2c3eb9SBrooks Davis // All rights reserved.
34b2c3eb9SBrooks Davis //
44b2c3eb9SBrooks Davis // Redistribution and use in source and binary forms, with or without
54b2c3eb9SBrooks Davis // modification, are permitted provided that the following conditions are
64b2c3eb9SBrooks Davis // met:
74b2c3eb9SBrooks Davis //
84b2c3eb9SBrooks Davis // * Redistributions of source code must retain the above copyright
94b2c3eb9SBrooks Davis //   notice, this list of conditions and the following disclaimer.
104b2c3eb9SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
114b2c3eb9SBrooks Davis //   notice, this list of conditions and the following disclaimer in the
124b2c3eb9SBrooks Davis //   documentation and/or other materials provided with the distribution.
134b2c3eb9SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
144b2c3eb9SBrooks Davis //   may be used to endorse or promote products derived from this software
154b2c3eb9SBrooks Davis //   without specific prior written permission.
164b2c3eb9SBrooks Davis //
174b2c3eb9SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
184b2c3eb9SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
194b2c3eb9SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
204b2c3eb9SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
214b2c3eb9SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
224b2c3eb9SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
234b2c3eb9SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
244b2c3eb9SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
254b2c3eb9SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
264b2c3eb9SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
274b2c3eb9SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
284b2c3eb9SBrooks Davis 
294b2c3eb9SBrooks Davis #include <cassert>
304b2c3eb9SBrooks Davis 
314b2c3eb9SBrooks Davis #include <lua.hpp>
324b2c3eb9SBrooks Davis 
334b2c3eb9SBrooks Davis #include <lutok/c_gate.hpp>
344b2c3eb9SBrooks Davis #include <lutok/debug.hpp>
354b2c3eb9SBrooks Davis #include <lutok/exceptions.hpp>
364b2c3eb9SBrooks Davis #include <lutok/state.ipp>
374b2c3eb9SBrooks Davis 
384b2c3eb9SBrooks Davis 
394b2c3eb9SBrooks Davis /// Internal implementation for lutok::debug.
404b2c3eb9SBrooks Davis struct lutok::debug::impl {
414b2c3eb9SBrooks Davis     /// The Lua internal debug state.
424b2c3eb9SBrooks Davis     lua_Debug lua_debug;
434b2c3eb9SBrooks Davis };
444b2c3eb9SBrooks Davis 
454b2c3eb9SBrooks Davis 
464b2c3eb9SBrooks Davis /// Constructor for an empty debug structure.
debug(void)474b2c3eb9SBrooks Davis lutok::debug::debug(void) :
484b2c3eb9SBrooks Davis     _pimpl(new impl())
494b2c3eb9SBrooks Davis {
504b2c3eb9SBrooks Davis }
514b2c3eb9SBrooks Davis 
524b2c3eb9SBrooks Davis 
534b2c3eb9SBrooks Davis /// Destructor.
~debug(void)544b2c3eb9SBrooks Davis lutok::debug::~debug(void)
554b2c3eb9SBrooks Davis {
564b2c3eb9SBrooks Davis }
574b2c3eb9SBrooks Davis 
584b2c3eb9SBrooks Davis 
594b2c3eb9SBrooks Davis /// Wrapper around lua_getinfo.
604b2c3eb9SBrooks Davis ///
614b2c3eb9SBrooks Davis /// \param s The Lua state.
624b2c3eb9SBrooks Davis /// \param what_ The second parameter to lua_getinfo.
634b2c3eb9SBrooks Davis ///
644b2c3eb9SBrooks Davis /// \warning Terminates execution if there is not enough memory to manipulate
654b2c3eb9SBrooks Davis /// the Lua stack.
664b2c3eb9SBrooks Davis void
get_info(state & s,const std::string & what_)674b2c3eb9SBrooks Davis lutok::debug::get_info(state& s, const std::string& what_)
684b2c3eb9SBrooks Davis {
694b2c3eb9SBrooks Davis     lua_State* raw_state = state_c_gate(s).c_state();
704b2c3eb9SBrooks Davis 
714b2c3eb9SBrooks Davis     if (lua_getinfo(raw_state, what_.c_str(), &_pimpl->lua_debug) == 0)
724b2c3eb9SBrooks Davis         throw lutok::api_error::from_stack(s, "lua_getinfo");
734b2c3eb9SBrooks Davis }
744b2c3eb9SBrooks Davis 
754b2c3eb9SBrooks Davis 
764b2c3eb9SBrooks Davis /// Wrapper around lua_getstack.
774b2c3eb9SBrooks Davis ///
784b2c3eb9SBrooks Davis /// \param s The Lua state.
794b2c3eb9SBrooks Davis /// \param level The second parameter to lua_getstack.
804b2c3eb9SBrooks Davis void
get_stack(state & s,const int level)814b2c3eb9SBrooks Davis lutok::debug::get_stack(state& s, const int level)
824b2c3eb9SBrooks Davis {
834b2c3eb9SBrooks Davis     lua_State* raw_state = state_c_gate(s).c_state();
844b2c3eb9SBrooks Davis 
854b2c3eb9SBrooks Davis     lua_getstack(raw_state, level, &_pimpl->lua_debug);
864b2c3eb9SBrooks Davis }
874b2c3eb9SBrooks Davis 
884b2c3eb9SBrooks Davis 
894b2c3eb9SBrooks Davis /// Accessor for the 'event' field of lua_Debug.
904b2c3eb9SBrooks Davis ///
914b2c3eb9SBrooks Davis /// \return Returns the 'event' field of the internal lua_Debug structure.
924b2c3eb9SBrooks Davis int
event(void) const934b2c3eb9SBrooks Davis lutok::debug::event(void) const
944b2c3eb9SBrooks Davis {
954b2c3eb9SBrooks Davis     return _pimpl->lua_debug.event;
964b2c3eb9SBrooks Davis }
974b2c3eb9SBrooks Davis 
984b2c3eb9SBrooks Davis 
994b2c3eb9SBrooks Davis /// Accessor for the 'name' field of lua_Debug.
1004b2c3eb9SBrooks Davis ///
1014b2c3eb9SBrooks Davis /// \return Returns the 'name' field of the internal lua_Debug structure.
1024b2c3eb9SBrooks Davis std::string
name(void) const1034b2c3eb9SBrooks Davis lutok::debug::name(void) const
1044b2c3eb9SBrooks Davis {
1054b2c3eb9SBrooks Davis     assert(_pimpl->lua_debug.name != NULL);
1064b2c3eb9SBrooks Davis     return _pimpl->lua_debug.name;
1074b2c3eb9SBrooks Davis }
1084b2c3eb9SBrooks Davis 
1094b2c3eb9SBrooks Davis 
1104b2c3eb9SBrooks Davis /// Accessor for the 'namewhat' field of lua_Debug.
1114b2c3eb9SBrooks Davis ///
1124b2c3eb9SBrooks Davis /// \return Returns the 'namewhat' field of the internal lua_Debug structure.
1134b2c3eb9SBrooks Davis std::string
name_what(void) const1144b2c3eb9SBrooks Davis lutok::debug::name_what(void) const
1154b2c3eb9SBrooks Davis {
1164b2c3eb9SBrooks Davis     assert(_pimpl->lua_debug.namewhat != NULL);
1174b2c3eb9SBrooks Davis     return _pimpl->lua_debug.namewhat;
1184b2c3eb9SBrooks Davis }
1194b2c3eb9SBrooks Davis 
1204b2c3eb9SBrooks Davis 
1214b2c3eb9SBrooks Davis /// Accessor for the 'what' field of lua_Debug.
1224b2c3eb9SBrooks Davis ///
1234b2c3eb9SBrooks Davis /// \return Returns the 'what' field of the internal lua_Debug structure.
1244b2c3eb9SBrooks Davis std::string
what(void) const1254b2c3eb9SBrooks Davis lutok::debug::what(void) const
1264b2c3eb9SBrooks Davis {
1274b2c3eb9SBrooks Davis     assert(_pimpl->lua_debug.what != NULL);
1284b2c3eb9SBrooks Davis     return _pimpl->lua_debug.what;
1294b2c3eb9SBrooks Davis }
1304b2c3eb9SBrooks Davis 
1314b2c3eb9SBrooks Davis 
1324b2c3eb9SBrooks Davis /// Accessor for the 'source' field of lua_Debug.
1334b2c3eb9SBrooks Davis ///
1344b2c3eb9SBrooks Davis /// \return Returns the 'source' field of the internal lua_Debug structure.
1354b2c3eb9SBrooks Davis std::string
source(void) const1364b2c3eb9SBrooks Davis lutok::debug::source(void) const
1374b2c3eb9SBrooks Davis {
1384b2c3eb9SBrooks Davis     assert(_pimpl->lua_debug.source != NULL);
1394b2c3eb9SBrooks Davis     return _pimpl->lua_debug.source;
1404b2c3eb9SBrooks Davis }
1414b2c3eb9SBrooks Davis 
1424b2c3eb9SBrooks Davis 
1434b2c3eb9SBrooks Davis /// Accessor for the 'currentline' field of lua_Debug.
1444b2c3eb9SBrooks Davis ///
1454b2c3eb9SBrooks Davis /// \return Returns the 'currentline' field of the internal lua_Debug structure.
1464b2c3eb9SBrooks Davis int
current_line(void) const1474b2c3eb9SBrooks Davis lutok::debug::current_line(void) const
1484b2c3eb9SBrooks Davis {
1494b2c3eb9SBrooks Davis     return _pimpl->lua_debug.currentline;
1504b2c3eb9SBrooks Davis }
1514b2c3eb9SBrooks Davis 
1524b2c3eb9SBrooks Davis 
1534b2c3eb9SBrooks Davis /// Accessor for the 'nups' field of lua_Debug.
1544b2c3eb9SBrooks Davis ///
1554b2c3eb9SBrooks Davis /// \return Returns the 'nups' field of the internal lua_Debug structure.
1564b2c3eb9SBrooks Davis int
n_ups(void) const1574b2c3eb9SBrooks Davis lutok::debug::n_ups(void) const
1584b2c3eb9SBrooks Davis {
1594b2c3eb9SBrooks Davis     return _pimpl->lua_debug.nups;
1604b2c3eb9SBrooks Davis }
1614b2c3eb9SBrooks Davis 
1624b2c3eb9SBrooks Davis 
1634b2c3eb9SBrooks Davis /// Accessor for the 'linedefined' field of lua_Debug.
1644b2c3eb9SBrooks Davis ///
1654b2c3eb9SBrooks Davis /// \return Returns the 'linedefined' field of the internal lua_Debug structure.
1664b2c3eb9SBrooks Davis int
line_defined(void) const1674b2c3eb9SBrooks Davis lutok::debug::line_defined(void) const
1684b2c3eb9SBrooks Davis {
1694b2c3eb9SBrooks Davis     return _pimpl->lua_debug.linedefined;
1704b2c3eb9SBrooks Davis }
1714b2c3eb9SBrooks Davis 
1724b2c3eb9SBrooks Davis 
1734b2c3eb9SBrooks Davis /// Accessor for the 'lastlinedefined' field of lua_Debug.
1744b2c3eb9SBrooks Davis ///
1754b2c3eb9SBrooks Davis /// \return Returns the 'lastlinedefined' field of the internal lua_Debug
1764b2c3eb9SBrooks Davis /// structure.
1774b2c3eb9SBrooks Davis int
last_line_defined(void) const1784b2c3eb9SBrooks Davis lutok::debug::last_line_defined(void) const
1794b2c3eb9SBrooks Davis {
1804b2c3eb9SBrooks Davis     return _pimpl->lua_debug.lastlinedefined;
1814b2c3eb9SBrooks Davis }
1824b2c3eb9SBrooks Davis 
1834b2c3eb9SBrooks Davis 
1844b2c3eb9SBrooks Davis /// Accessor for the 'short_src' field of lua_Debug.
1854b2c3eb9SBrooks Davis ///
1864b2c3eb9SBrooks Davis /// \return Returns the 'short_src' field of the internal lua_Debug structure.
1874b2c3eb9SBrooks Davis std::string
short_src(void) const1884b2c3eb9SBrooks Davis lutok::debug::short_src(void) const
1894b2c3eb9SBrooks Davis {
1904b2c3eb9SBrooks Davis     assert(_pimpl->lua_debug.short_src != NULL);
1914b2c3eb9SBrooks Davis     return _pimpl->lua_debug.short_src;
1924b2c3eb9SBrooks Davis }
193