1 /* Copyright (c) 2010-2019. The SimGrid Team.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 * This file contains functions that aid users to debug their lua scripts; for instance,
9 * tables can be easily output and values are represented in a human-readable way. (For instance,
10 * a nullptr value becomes the string "nil").
13 /* SimGrid Lua debug functions */
15 #include "lua_utils.hpp"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_debug, "Lua bindings (helper functions)");
21 * @brief Returns a string representation of a value in the Lua stack.
23 * This function is for debugging purposes.
24 * It always returns the same pointer.
26 * @param L the Lua state
27 * @param index index in the stack
28 * @return a string representation of the value at this index
30 const char* sglua_tostring(lua_State* L, int index) {
34 switch (lua_type(L, index)) {
37 snprintf(buff, 4, "nil");
41 snprintf(buff, 64, "%.3f", lua_tonumber(L, index));
45 snprintf(buff, 64, "%s", lua_toboolean(L, index) ? "true" : "false");
49 snprintf(buff, 63, "'%s'", lua_tostring(L, index));
53 if (lua_iscfunction(L, index)) {
54 snprintf(buff, 11, "C-function");
57 snprintf(buff, 9, "function");
62 snprintf(buff, 64, "table(%p)", lua_topointer(L, index));
65 case LUA_TLIGHTUSERDATA:
67 snprintf(buff, 64, "userdata(%p)", lua_touserdata(L, index));
71 snprintf(buff, 7, "thread");
77 static int sglua_dump_table(lua_State* L) {
78 int argc = lua_gettop(L);
80 for (int i = 1; i < argc; i++) {
81 if (lua_istable(L, i)) {
82 lua_pushnil(L); /* table nil */
84 //lua_next pops the topmost element from the stack and
85 //gets the next pair from the table
86 while (lua_next(L, -1)) { /* table key val */
87 // we need to copy here, as a cast from "Number" to "String"
88 // could happen in Lua.
89 // see remark in the lua manual, function "lua_tolstring"
90 // http://www.lua.org/manual/5.3/manual.html#lua_tolstring
92 lua_pushvalue(L, -2); /* table key val key */
94 const char *key = lua_tostring(L, -1); /* table key val */
95 const char *val = lua_tostring(L, -1); /* table key */
97 XBT_DEBUG("%s => %s", key, val);
100 lua_settop(L, argc); // Remove everything except the initial arguments
108 * @brief Returns a string composed of the specified number of spaces.
110 * This function can be used to indent strings for debugging purposes.
111 * It always returns the same pointer.
113 * @param length length of the string
114 * @return a string of this length with only spaces
116 const char* sglua_get_spaces(int length) {
118 static char spaces[128];
120 xbt_assert(length >= 0 && length < 128,
121 "Invalid indentation length: %d", length);
123 memset(spaces, ' ', length);
125 spaces[length] = '\0';
130 * @brief Returns a string representation of a key-value pair.
132 * It always returns the same pointer.
134 * @param L the Lua state
135 * @param key_index index of the key (in the lua stack)
136 * @param value_index index of the value (in the lua stack)
137 * @return a string representation of the key-value pair
139 const char* sglua_keyvalue_tostring(lua_State* L, int key_index, int value_index) {
141 static char buff[64];
142 /* value_tostring also always returns the same pointer */
143 int len = snprintf(buff, 63, "[%s] -> ", sglua_tostring(L, key_index));
144 snprintf(buff + len, 63 - len, "%s", sglua_tostring(L, value_index));
149 * @brief Dumps the Lua stack if debug logs are enabled.
150 * @param msg a message to print
151 * @param L a Lua state
153 void sglua_stack_dump(lua_State* L, const char* msg)
155 if (XBT_LOG_ISENABLED(lua_debug, xbt_log_priority_debug)) {
158 int top = lua_gettop(L);
163 for (int i = 1; i <= top; i++) { /* repeat for each level */
164 p += snprintf(p, 2048-(p-buff), "%s ", sglua_tostring(L, i));
167 XBT_DEBUG("%s%s", msg, buff);
172 * @brief Like luaL_checkudata, with additional debug logs.
174 * This function is for debugging purposes only.
176 * @param L a lua state
177 * @param ud index of the userdata to check in the stack
178 * @param tname key of the metatable of this userdata in the registry
180 void* sglua_checkudata_debug(lua_State* L, int ud, const char* tname)
182 XBT_DEBUG("Checking the userdata: ud = %d", ud);
183 sglua_stack_dump(L, "my_checkudata: ");
184 void* p = lua_touserdata(L, ud);
185 lua_getfield(L, LUA_REGISTRYINDEX, tname);
186 const void* correct_mt = lua_topointer(L, -1);
188 int has_mt = lua_getmetatable(L, ud);
189 XBT_DEBUG("Checking the userdata: has metatable ? %d", has_mt);
190 const void* actual_mt = nullptr;
192 actual_mt = lua_topointer(L, -1);
195 XBT_DEBUG("Checking the task's metatable: expected %p, found %p", correct_mt, actual_mt);
196 sglua_stack_dump(L, "my_checkudata: ");
198 if (p == nullptr || not lua_getmetatable(L, ud) || not lua_rawequal(L, -1, -2))
199 XBT_ERROR("Error: Userdata is nullptr, couldn't find metatable or top of stack does not equal element below it.");
205 * @brief Writes the specified data into a memory buffer.
207 * This function is a valid lua_Writer that writes into a memory buffer passed
210 * @param L a lua state
211 * @param source some data
212 * @param size number of bytes of data
213 * @param userdata the memory buffer to write
215 int sglua_memory_writer(lua_State* L, const void* source, size_t size, void* userdata)
217 sglua_buffer_t buffer = static_cast<sglua_buffer_t>(userdata);
218 while (buffer->capacity < buffer->size + size) {
219 buffer->capacity *= 2;
220 buffer->data = static_cast<char*>(xbt_realloc(buffer->data, buffer->capacity));
222 memcpy(buffer->data + buffer->size, source, size);
223 buffer->size += size;