#include <stdlib.h>
#include <tracer.h>
void *StartTracerBuffer
; // Buffer Start 0
void *EndTracerBuffer
; // Buffer End 4
void *LastBeforeEndTracerBuffer
; //8
void *CurrentTracerBuffer
; //The Actual Write Point For The Next Event 12
void *FirstTracerBuffer
; //The First Valid Event 16
int TracerActive
= 0; //20
int TracerOutputType
= TRACER_NO_OUTPUT
; //24
unsigned long long TracerEventsRecorded
; //28
unsigned int TracerEventsPresent
; //36
extern unsigned int clk_per_msec
;
#define TRACER_DEBUG
/* Initialize the tracer
*
* Return 0 => Success
* Return 1 => Fail
*/
int tracer_initialize
(int MemorySize
)
{
SYS_FLAGS f
;
f
= ll_fsave
();
StartTracerBuffer
= malloc(MemorySize
);
if (!StartTracerBuffer
) {
ll_frestore
(f
);
return 1;
}
EndTracerBuffer
= StartTracerBuffer
+ MemorySize
- 1;
CurrentTracerBuffer
= StartTracerBuffer
;
FirstTracerBuffer
= StartTracerBuffer
;
LastBeforeEndTracerBuffer
= EndTracerBuffer
;
TracerActive
= 0;
TracerEventsRecorded
= 0;
TracerEventsPresent
= 0;
ll_frestore
(f
);
return 0;
}
void tracer_enable
() {
SYS_FLAGS f
;
f
= ll_fsave
();
TracerActive
= 1;
ll_frestore
(f
);
}
void tracer_disable
() {
SYS_FLAGS f
;
f
= ll_fsave
();
TracerActive
= 0;
ll_frestore
(f
);
}
int send_udp_event
(void *p
, int size
);
void send_remaining_udp_buffer
();
/* Send NumberOfEvents out of the system
* If NumberOfEvents = -1 all the events will be sent */
int tracer_send_logged_events
(int NumberOfEvents
) {
void *p
;
int s
,err
;
SYS_FLAGS f
;
f
= ll_fsave
();
if (NumberOfEvents
== 0 || TracerEventsPresent
== 0) {
ll_frestore
(f
);
return 0;
}
p
= FirstTracerBuffer
;
s
= 0;
while(((p
== CurrentTracerBuffer
&& s
== 0) || p
!= CurrentTracerBuffer
) &&
s
!= NumberOfEvents
&&
TracerEventsPresent
!= 0) {
switch (TracerOutputType
) {
case TRACER_NO_OUTPUT
:
#ifdef TRACER_DEBUG
cprintf
("Event Type %02x Sent To NULL\n",*(BYTE
*)(p
));
#endif
break;
case TRACER_UDP_OUTPUT
:
err
= send_udp_event
(p
,*(BYTE
*)(p
+ 9));
if (err
!= 0) {
ll_frestore
(f
);
return s
;
}
break;
default:
#ifdef TRACER_DEBUG
cprintf
("Error: Tracer Output Not Defined\n");
#endif
ll_frestore
(f
);
return -1;
break;
}
p
+= *(BYTE
*)(p
+ 9);
if (p
>= LastBeforeEndTracerBuffer
) p
= StartTracerBuffer
;
FirstTracerBuffer
= p
;
TracerEventsPresent
--;
s
++;
}
ll_frestore
(f
);
return s
;
}
//Flush the events that are ready to send
void tracer_flush_sent_events
() {
switch (TracerOutputType
) {
case TRACER_UDP_OUTPUT
:
send_remaining_udp_buffer
();
break;
}
}
void tracer_print_statistics
() {
SYS_FLAGS f
;
void *p
;
int i
,t
,s
;
f
= ll_fsave
();
cprintf
("\nStart Tracer Buffer: %08x\n",(int)StartTracerBuffer
);
cprintf
("End Tracer Buffer: %08x\n",(int)EndTracerBuffer
);
cprintf
("Current Tracer Buffer: %08x\n",(int)CurrentTracerBuffer
);
cprintf
("First Tracer Buffer: %08x\n",(int)FirstTracerBuffer
);
cprintf
("Last Before End Tracer Buffer: %08x\n\n",(int)LastBeforeEndTracerBuffer
);
cprintf
("Events Logged: %d\n",(int)TracerEventsRecorded
);
cprintf
("Events Present: %d\n",TracerEventsPresent
);
if (TracerEventsPresent
== 0) {
ll_frestore
(f
);
return;
}
for (i
=0; i
<0xFF; i
++) {
p
= FirstTracerBuffer
;
t
= 0;
s
= 0;
while((p
== CurrentTracerBuffer
&& s
== 0) || p
!= CurrentTracerBuffer
) {
s
++;
if (*(BYTE
*)p
== i
) t
++;
p
+= *(BYTE
*)(p
+ 9);
if (p
>= LastBeforeEndTracerBuffer
) p
= StartTracerBuffer
;
}
if (t
!= 0)
cprintf
("Event type %02x => %d\n",i
,t
);
}
ll_frestore
(f
);
}