Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / common / data_common / r2 / r2_debug.lua
blob78501dcab6441f3714d788626ab01a1493c6f683
1 -- debugging aid
3 ---------------
4 -- FUNCTIONS --
5 ---------------
8 ------------------------------------------------------------------------------------------------------------
9 -- function tracing asked in config file ?
10 -- if traceFunctions == true then
11 -- local function traceFunction(what)
12 -- debug.sethook()
13 -- local di = debug.getinfo(2)
14 -- --luaObject(di)
15 -- if di.name ~= nil and di.name ~= 'getmetatable' then
16 -- debugInfo(what .. " : " .. tostring(di.name) .. " : " .. tostring(di.short_src) .. " : " .. tostring(di.currentline))
17 -- end
18 -- debug.sethook(traceFunction, "cr")
19 -- end
20 -- debugInfo("turning debug hook on")
21 -- debug.sethook(traceFunction, "c")
22 --else
23 -- --debugInfo("turning debug hook off")
24 -- debug.sethook()
25 --end
27 ------------------------------------------------------------------------------------------------------------
28 -- dump objects cycles for the given object
29 function dumpCycles(base, visitedSet, infos)
30 local function fullName(infos)
31 local result = ""
32 while infos do
33 result = infos.Name .. "." .. result
34 infos = infos.Parent
35 end
36 return result
37 end
38 if visitedSet == nil then
39 visitedSet = {}
40 end
41 if infos == nil then
42 infos = { Name = "root", Parent = nil }
43 end
44 for k, v in pairs(base) do
45 if v ~= _G._G then
46 if type(v) == "table" then
47 local newInfos = { Name = tostring(k), Parent = infos }
48 if visitedSet[v] then
49 debugInfo(fullName(visitedSet[v]) .. "is referenced by " .. fullName(newInfos))
50 else
51 visitedSet[v] = newInfos -- mark as visited
52 dumpCycles(v, visitedSet, newInfos)
53 visitedSet[v] = nil -- only intersted in cycles
54 end
55 end
56 end
57 end
58 end
60 ------------------------------------------------------------------------------------------------------------
61 -- display time taken to execute a function
62 function profileFunction(func, name)
63 assert(type(func) == "function")
64 if name == nil then
65 name = debug.getinfo(func).name
66 end
67 local startTime = nltime.getPreciseLocalTime()
68 func()
69 local endTime = nltime.getPreciseLocalTime()
70 --debugInfo(string.format("time for %s is %d", tostring(name), endTime - startTime))
71 end
73 -- display time taken to execute a function
74 function profileMethod(table, funcName, name)
75 assert(table)
76 assert(type(funcName) == "string")
77 assert(type(table[funcName]) == "function")
78 if name == nil then
79 name = select(debug.getinfo(table[funcName]).name, funcName)
80 end
81 local startTime = nltime.getLocalTime()
82 table[funcName](table)
83 local endTime = nltime.getLocalTime()
84 debugInfo(string.format("time for %s is %f", tostring(name), (endTime - startTime) / 1000))
85 end
87 ------------------------------------------------------------------------------------------------------------
88 -- add a break that is triggered when a value in a table has been changed
89 function addBreakOnChange(table, watchedKey)
90 assert(type(table) == "table")
91 local value = table[watchedKey]
92 assert(value ~= nil)
93 table[watchedKey] = nil
94 debugInfo("Adding break on change of key " .. tostring(watchedKey))
95 local mt = getmetatable(table)
96 local oldNewIndex
97 if mt then
98 oldNewIndex = mt.__newindex
99 end
100 local newMT
101 if mt then newMT = clone(mt) else newMT = {} end
102 -- WRITE
103 newMT.__newindex = function(table, key, newValue)
104 debugInfo('write')
105 if key == watchedKey then
106 value = newValue
107 debugInfo(debug.traceback())
108 debugWarning("<addBreakOnChange> Key " .. tostring(watchedKey) .. " changed to " .. tostring(value))
109 assert(nil)
110 elseif mt and mt.__newindex then
111 mt.__newindex(table, key, newValue)
112 else
113 rawset(table, key, newValue)
114 end
116 -- READ
117 newMT.__index = function(table, key)
118 debugInfo('read')
119 if key == watchedKey then
120 return value
121 elseif mt and mt.__index then
122 return mt.__index(table, key, value)
123 else
124 return rawget(table, key)
125 end
128 setmetatable(table, newMT)
129 end
131 ----------
132 -- INIT --
133 ----------
136 -- replace the assert function with a more verbose one
137 if oldAssert == nil then
138 oldAssert = assert
141 function assert(cond)
142 if not cond then
143 -- rawDebugInfo(colorTag(255, 0, 255) .. "ASSERTION FAILED !! ")
144 rawDebugInfo("@{FOFF}ASSERTION FAILED !! ")
145 dumpCallStack(2);
146 error("")
151 local gcStartTime
153 -- special nico stuff : modified lua version calls "__notify_debug" when a garbage collection cycle occurs
154 function __notify_gc()
155 gcStartTime = nltime.getLocalTime()
156 end
157 function __notify_post_gc()
158 local deltaTime = nltime.getLocalTime() - gcStartTime
159 local used, threshold = gcinfo()
160 debugInfo(colorTag(255, 0, 0) .. string.format("** GC ** (%d ms) (used = %d kb, threshold = %d kb", deltaTime, used, threshold))
164 --testTable = { tata = 1, toto = 2 }
166 --addBreakOnChange(testTable, "tata")
168 --testTable.tutu = 2
169 --testTable.tata = testTable.tata + 20