feature: Tracer O(1) ring buffer refactor
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

- Replaced O(n) shift loop + StringCopy/kalloc/kfree with O(1) ring
  buffer storing PChar pointers directly (zero allocation).
- Eliminated mod operator (causes int64 promotion + broken RTL helpers
  on i386 bare-metal); uses pure uint32 branch arithmetic.
- Removed dead code: PTracerEntry/TTracerEntry linked list types,
  head/tail PTracerEntry vars, c_lock boolean
- Dropped lmemorymanager and serial from uses clause
- pop_trace remains as no-op stub for ABI compat
- get_trace_N uses underflow-safe index: if head >= idx then
  head - idx else MAX_TRACE - (idx - head)
- Added print_traces debug helper
This commit is contained in:
2026-03-01 21:26:35 +00:00
parent afdc83eaf6
commit f53afc1d2d
2 changed files with 51 additions and 33 deletions
+9 -2
View File
@@ -581,12 +581,13 @@ begin
syslog.writestring(' EIP: '); syslog.writehex(IntSpec^.EIP); syslog.writestring(' CS: '); syslog.writehex(IntSpec^.CS); syslog.writestring(' EFLAGS: '); syslog.writehexln(IntSpec^.EFLAGS);
syslog.writestringln(' ');
end;
syslog.writestring('Call Stack: ');
tracer.freeze;
syslog.writestring('Call Stack: ');
trace:= tracer.get_last_trace;
if trace <> nil then begin
syslog.writestring('[-0] ');
syslog.writestringln(trace);
for i:=1 to tracer.get_trace_count-7 do begin
for i:=1 to tracer.get_trace_count-1 do begin
trace:= tracer.get_trace_N(i);
if trace <> nil then begin
syslog.writestring(' [');
@@ -594,6 +595,12 @@ begin
syslog.writeint(i);
syslog.writestring('] ');
syslog.writestringln(trace);
end else begin
syslog.writestring(' [');
syslog.writestring('-');
syslog.writeint(i);
syslog.writestring('] ');
syslog.writestringln('?????????');
end;
end;
end else begin
+42 -31
View File
@@ -13,8 +13,12 @@
// limitations under the License.
{
Tracer - Trace stack for debugging method calls.
Tracer - Ring buffer trace log for debugging method calls.
IMPORTANT: push_trace MUST only be called with pointers to
static/persistent data (e.g. string literals). The pointer is
stored directly - no copy is made.
@author(Kieron Morris <[email protected]>)
}
unit tracer;
@@ -28,19 +32,12 @@ function get_last_trace : pchar;
procedure freeze;
function get_trace_count : uint32;
function get_trace_N(idx : uint32) : pchar;
procedure print_traces;
implementation
uses
lmemorymanager, util, strings, serial, stdio;
type
PTracerEntry = ^TTracerEntry;
TTracerEntry = record
Next : PTracerEntry;
Data : pchar;
Previous : PTracerEntry;
end;
util, strings, stdio, syslog;
const
MAX_TRACE = 40;
@@ -48,16 +45,12 @@ const
var
t_ready : Boolean;
Locked : Boolean;
head : uint32;
Traces : Array[0..MAX_TRACE-1] of PChar;
c_lock : Boolean = false;
var
head : PTracerEntry;
tail : PTracerEntry;
procedure terminal_command_tracer(Params : PParamList; stdin_buf, stdout_buf, stderr_buf : POutBuf);
var
p1, p2 : PChar;
p1 : PChar;
count : uint32;
i : uint32;
t : PChar;
@@ -113,22 +106,15 @@ begin
end;
procedure push_trace(t_name : pchar);
var
i : uint32;
begin
if TRACER_ENABLE then begin
if t_ready then begin
if not Locked then begin
if not c_lock then begin
Locked:= true;
if Traces[MAX_TRACE-1] <> nil then kfree(void(Traces[MAX_TRACE-1]));
for i:=MAX_TRACE-1 downto 1 do begin
Traces[i]:= Traces[i-1];
end;
Traces[0]:= StringCopy(t_name);
Locked:= false;
end;
Locked:= true;
head:= head + 1;
if head >= MAX_TRACE then head:= 0;
Traces[head]:= t_name;
Locked:= false;
end;
end;
end;
@@ -141,7 +127,7 @@ end;
function get_last_trace : pchar;
begin
get_last_trace:= Traces[0];
get_last_trace:= Traces[head];
end;
procedure init;
@@ -151,8 +137,10 @@ var
begin
if TRACER_ENABLE then begin
for i:=0 to MAX_TRACE-1 do begin
traces[i]:= nil;
Traces[i]:= nil;
end;
Locked:= false;
head:= MAX_TRACE - 1;
t_ready:= true;
push_trace('kmain');
end;
@@ -166,11 +154,34 @@ begin
end;
end;
procedure print_traces;
var
i : uint32;
begin
for i:=0 to MAX_TRACE-1 do begin
syslog.log('TRACER', '[');
syslog.writeint(i);
syslog.writestring('] ');
if Traces[i] <> nil then begin
syslog.writestringln(Traces[i]);
end else begin
syslog.writestringln('?????????');
end;
end;
end;
function get_trace_N(idx : uint32) : pchar;
var
slot : uint32;
begin
if idx > MAX_TRACE-1 then exit;
if TRACER_ENABLE then begin
get_trace_N:= traces[idx];
if head >= idx then
slot:= head - idx
else
slot:= MAX_TRACE - (idx - head);
get_trace_N:= Traces[slot];
end;
end;