1 #ifndef __TRACYLUA_HPP__
2 #define __TRACYLUA_HPP__
15 static inline int noop( lua_State* L ) {
return 0; }
22 lua_setfield( L, -2,
"ZoneBegin" );
24 lua_setfield( L, -2,
"ZoneBeginN" );
26 lua_setfield( L, -2,
"ZoneBeginS" );
28 lua_setfield( L, -2,
"ZoneBeginNS" );
30 lua_setfield( L, -2,
"ZoneEnd" );
32 lua_setfield( L, -2,
"ZoneText" );
34 lua_setfield( L, -2,
"ZoneName" );
36 lua_setfield( L, -2,
"Message" );
37 lua_setglobal( L,
"tracy" );
45 if( *ptr ==
'(' ) cnt++;
46 else if( *ptr ==
')' ) cnt--;
56 if( strncmp( script,
"tracy.", 6 ) == 0 )
58 if( strncmp( script + 6,
"Zone", 4 ) == 0 )
60 if( strncmp( script + 10,
"End()", 5 ) == 0 )
62 memset( script,
' ', 15 );
65 else if( strncmp( script + 10,
"Begin()", 7 ) == 0 )
67 memset( script,
' ', 17 );
70 else if( strncmp( script + 10,
"Text(", 5 ) == 0 )
73 memset( script,
' ',
end - script );
76 else if( strncmp( script + 10,
"Name(", 5 ) == 0 )
79 memset( script,
' ',
end - script );
82 else if( strncmp( script + 10,
"BeginN(", 7 ) == 0 )
85 memset( script,
' ',
end - script );
88 else if( strncmp( script + 10,
"BeginS(", 7 ) == 0 )
91 memset( script,
' ',
end - script );
94 else if( strncmp( script + 10,
"BeginNS(", 8 ) == 0 )
97 memset( script,
' ',
end - script );
105 else if( strncmp( script + 6,
"Message(", 8 ) == 0 )
108 memset( script,
' ',
end - script );
139 #ifdef TRACY_ON_DEMAND
140 TRACY_API LuaZoneState& GetLuaZoneState();
146 #ifdef TRACY_HAS_CALLSTACK
149 assert( depth <= 64 );
151 const char* func[64];
156 uint16_t spaceNeeded =
sizeof( cnt );
157 for( cnt=0; cnt<
depth; cnt++ )
159 if( lua_getstack( L, cnt+1, dbg+cnt ) == 0 )
break;
160 lua_getinfo( L,
"Snl", dbg+cnt );
161 func[cnt] = dbg[cnt].name ? dbg[cnt].name : dbg[cnt].short_src;
162 fsz[cnt] =
uint32_t( strlen( func[cnt] ) );
164 spaceNeeded += fsz[cnt] + ssz[cnt];
166 spaceNeeded += cnt * ( 4 + 2 + 2 );
170 memcpy(
dst, &spaceNeeded, 2 );
dst += 2;
171 memcpy(
dst, &cnt, 1 );
dst++;
174 const uint32_t line = dbg[i].currentline;
175 memcpy(
dst, &line, 4 );
dst += 4;
177 memcpy(
dst, fsz+i, 2 );
dst += 2;
178 memcpy(
dst, func[i], fsz[i] );
dst += fsz[i];
180 memcpy(
dst, ssz+i, 2 );
dst += 2;
183 assert(
dst - ptr == spaceNeeded + 2 );
187 MemWrite( &item->callstackAllocFat.nativePtr, (
uint64_t)Callstack( depth ) );
191 static inline int LuaZoneBeginS( lua_State* L )
193 #ifdef TRACY_ON_DEMAND
194 const auto zoneCnt = GetLuaZoneState().counter++;
195 if( zoneCnt != 0 && !GetLuaZoneState().active )
return 0;
197 if( !GetLuaZoneState().active )
return 0;
202 lua_getstack( L, 1, &dbg );
203 lua_getinfo( L,
"Snl", &dbg );
206 MemWrite( &item->zoneBegin.srcloc, srcloc );
209 #ifdef TRACY_CALLSTACK
210 const uint32_t depth = TRACY_CALLSTACK;
212 const auto depth =
uint32_t( lua_tointeger( L, 1 ) );
214 SendLuaCallstack( L, depth );
219 static inline int LuaZoneBeginNS( lua_State* L )
221 #ifdef TRACY_ON_DEMAND
222 const auto zoneCnt = GetLuaZoneState().counter++;
223 if( zoneCnt != 0 && !GetLuaZoneState().active )
return 0;
225 if( !GetLuaZoneState().active )
return 0;
230 lua_getstack( L, 1, &dbg );
231 lua_getinfo( L,
"Snl", &dbg );
233 const auto name = lua_tolstring( L, 1, &nsz );
236 MemWrite( &item->zoneBegin.srcloc, srcloc );
239 #ifdef TRACY_CALLSTACK
240 const uint32_t depth = TRACY_CALLSTACK;
242 const auto depth =
uint32_t( lua_tointeger( L, 2 ) );
244 SendLuaCallstack( L, depth );
250 static inline int LuaZoneBegin( lua_State* L )
252 #if defined TRACY_HAS_CALLSTACK && defined TRACY_CALLSTACK
253 return LuaZoneBeginS( L );
255 #ifdef TRACY_ON_DEMAND
256 const auto zoneCnt = GetLuaZoneState().counter++;
257 if( zoneCnt != 0 && !GetLuaZoneState().active )
return 0;
259 if( !GetLuaZoneState().active )
return 0;
264 lua_getstack( L, 1, &dbg );
265 lua_getinfo( L,
"Snl", &dbg );
268 MemWrite( &item->zoneBegin.srcloc, srcloc );
274 static inline int LuaZoneBeginN( lua_State* L )
276 #if defined TRACY_HAS_CALLSTACK && defined TRACY_CALLSTACK
277 return LuaZoneBeginNS( L );
279 #ifdef TRACY_ON_DEMAND
280 const auto zoneCnt = GetLuaZoneState().counter++;
281 if( zoneCnt != 0 && !GetLuaZoneState().active )
return 0;
283 if( !GetLuaZoneState().active )
return 0;
288 lua_getstack( L, 1, &dbg );
289 lua_getinfo( L,
"Snl", &dbg );
291 const auto name = lua_tolstring( L, 1, &nsz );
294 MemWrite( &item->zoneBegin.srcloc, srcloc );
300 static inline int LuaZoneEnd( lua_State* L )
302 #ifdef TRACY_ON_DEMAND
303 assert( GetLuaZoneState().
counter != 0 );
304 GetLuaZoneState().counter--;
305 if( !GetLuaZoneState().active )
return 0;
308 GetLuaZoneState().active =
false;
319 static inline int LuaZoneText( lua_State* L )
321 #ifdef TRACY_ON_DEMAND
322 if( !GetLuaZoneState().active )
return 0;
325 GetLuaZoneState().active =
false;
330 auto txt = lua_tostring( L, 1 );
331 const auto size = strlen( txt );
335 memcpy( ptr, txt,
size );
343 static inline int LuaZoneName( lua_State* L )
345 #ifdef TRACY_ON_DEMAND
346 if( !GetLuaZoneState().active )
return 0;
349 GetLuaZoneState().active =
false;
354 auto txt = lua_tostring( L, 1 );
355 const auto size = strlen( txt );
359 memcpy( ptr, txt,
size );
367 static inline int LuaMessage( lua_State* L )
369 #ifdef TRACY_ON_DEMAND
373 auto txt = lua_tostring( L, 1 );
374 const auto size = strlen( txt );
379 memcpy( ptr, txt,
size );
392 lua_pushcfunction( L, detail::LuaZoneBegin );
393 lua_setfield( L, -2,
"ZoneBegin" );
394 lua_pushcfunction( L, detail::LuaZoneBeginN );
395 lua_setfield( L, -2,
"ZoneBeginN" );
396 #ifdef TRACY_HAS_CALLSTACK
397 lua_pushcfunction( L, detail::LuaZoneBeginS );
398 lua_setfield( L, -2,
"ZoneBeginS" );
399 lua_pushcfunction( L, detail::LuaZoneBeginNS );
400 lua_setfield( L, -2,
"ZoneBeginNS" );
402 lua_pushcfunction( L, detail::LuaZoneBegin );
403 lua_setfield( L, -2,
"ZoneBeginS" );
404 lua_pushcfunction( L, detail::LuaZoneBeginN );
405 lua_setfield( L, -2,
"ZoneBeginNS" );
407 lua_pushcfunction( L, detail::LuaZoneEnd );
408 lua_setfield( L, -2,
"ZoneEnd" );
409 lua_pushcfunction( L, detail::LuaZoneText );
410 lua_setfield( L, -2,
"ZoneText" );
411 lua_pushcfunction( L, detail::LuaZoneName );
412 lua_setfield( L, -2,
"ZoneName" );
413 lua_pushcfunction( L, detail::LuaMessage );
414 lua_setfield( L, -2,
"Message" );
415 lua_setglobal( L,
"tracy" );
418 static inline void LuaRemove(
char* script ) {}