Back to index

libcitadel  8.12
wildfire.c File Reference
#include "sysdep.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "libcitadel.h"
#include "libcitadellocal.h"

Go to the source code of this file.

ConstStr WF_MsgStrs []
int addr2line_write_pipe [2]
int addr2line_read_pipe [2]
pid_t addr2line_pid
long BaseFrames = 0
StrBufFullBinaryName = NULL
static JsonValueWFInfo (const char *Filename, long fnlen, long LineNo, WF_MessageType Type)
JsonValueWildFireMessage (const char *Filename, long fnlen, long LineNo, StrBuf *Msg, WF_MessageType Type)
JsonValueWildFireMessagePlain (const char *Filename, long fnlen, long LineNo, const char *Message, long len, WF_MessageType Type)
void WildFireAddArray (JsonValue *ReportBase, JsonValue *Array, WF_MessageType Type)
void WildFireShutdown (void)
void WildFireInitBacktrace (const char *argvNull, int AddBaseFrameSkip)
JsonValueWildFireException (const char *Filename, long FileLen, long LineNo, StrBuf *Message, int StackOffset)
void WildFireSerializePayload (StrBuf *JsonBuffer, StrBuf *OutBuf, int *MsgCount, AddHeaderFunc AddHdr)

Function Documentation

static JsonValue* WFInfo ( const char *  Filename,
long  fnlen,
long  LineNo,
WF_MessageType  Type 
) [static]

Definition at line 30 of file wildfire.c.

{
       JsonValue *Val;

       Val = NewJsonObject(NULL, 0);
       JsonObjectAppend(Val, 
                      NewJsonPlainString(HKEY("Type"),
                                       WF_MsgStrs[Type].Key, 
                                       WF_MsgStrs[Type].len));
       JsonObjectAppend(Val, 
                      NewJsonPlainString(HKEY("File"), 
                                       Filename, fnlen));
       JsonObjectAppend(Val, 
                      NewJsonNumber(HKEY("Line"), LineNo));
       return Val;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void WildFireAddArray ( JsonValue ReportBase,
JsonValue Array,
WF_MessageType  Type 
)

Definition at line 81 of file wildfire.c.

{
       JsonValue *Val;
       Val = NewJsonArray(NULL, 0);
       JsonArrayAppend(Val, 
                     NewJsonPlainString(NULL, 0, 
                                      WF_MsgStrs[Type].Key, 
                                      WF_MsgStrs[Type].len));

       JsonArrayAppend(Val, Array);
}

Here is the call graph for this function:

JsonValue* WildFireException ( const char *  Filename,
long  FileLen,
long  LineNo,
StrBuf Message,
int  StackOffset 
)

Definition at line 269 of file wildfire.c.

{
       JsonValue *ExcClass;
       JsonValue *Val;
       Val = NewJsonArray(NULL, 0);

       JsonArrayAppend(Val, WFInfo(Filename, FileLen,
                                LineNo, eEXCEPTION));

       ExcClass = NewJsonObject(WF_MsgStrs[eTRACE].Key, 
                             WF_MsgStrs[eTRACE].len);
       
       JsonArrayAppend(Val, ExcClass);
       JsonObjectAppend(ExcClass, 
                      NewJsonPlainString(HKEY("Class"), 
                                       HKEY("Exception")));
       JsonObjectAppend(ExcClass, 
                      NewJsonString(HKEY("Message"), Message));
       JsonObjectAppend(ExcClass, 
                      NewJsonPlainString(HKEY("File"), 
                                       Filename, FileLen));
/*
       JsonObjectAppend(ExcClass, 
                      NewJsonPlainString(HKEY("Type"), 
                                       HKEY("throw")));
*/
       JsonObjectAppend(ExcClass, 
                      NewJsonNumber(HKEY("Line"), LineNo));

#ifdef HAVE_BACKTRACE
       {
              void *stack_frames[100];
              size_t size;
              long i;
              char **strings;
              JsonValue *Trace;
              JsonValue *Frame;
              StrBuf *FileName;
              StrBuf *Function;
              StrBuf *Pointer;
              StrBuf *Buf;
              unsigned int FunctionLine;

              Trace = NewJsonArray(HKEY("Trace"));
              JsonObjectAppend(ExcClass, Trace);
              FileName = NewStrBuf();
              Function = NewStrBuf();
              Pointer = NewStrBuf();
              Buf = NewStrBuf();

              size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
              strings = backtrace_symbols(stack_frames, size);
              for (i = StackOffset + 1; i < size; i++) {
                     if (strings != NULL){
                            ParseBacktrace(strings[i], Function, 
                                          FileName,
                                          &FunctionLine);
                            
                     }
                     StrBufPrintf(Pointer, "%p\n", stack_frames[i]);
                     
                     addr2lineBacktrace(Function, 
                                      FileName, 
                                      Pointer, 
                                      Buf, 
                                      &FunctionLine);

                     Frame = NewJsonObject(NULL, 0);
                     JsonArrayAppend(Trace, Frame);
                     JsonObjectAppend(Frame, 
                                    NewJsonString(HKEY("function"), Function));
                     JsonObjectAppend(Frame, 
                                    NewJsonString(HKEY("file"), FileName));
                     JsonObjectAppend(Frame, 
                                    NewJsonNumber(HKEY("line"), FunctionLine));
                     JsonObjectAppend(Frame, 
                                    NewJsonArray(HKEY("args")));/* not supportet... */

                     FunctionLine = 0;
                     FlushStrBuf(FileName);
                     FlushStrBuf(Function);
                     FlushStrBuf(Pointer);
              }
              free(strings);
              FreeStrBuf(&FileName);
              FreeStrBuf(&Function);
              FreeStrBuf(&Pointer);
              FreeStrBuf(&Buf);
       }
#endif
       return Val;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void WildFireInitBacktrace ( const char *  argvNull,
int  AddBaseFrameSkip 
)

Definition at line 205 of file wildfire.c.

{

#ifdef HAVE_BACKTRACE
       void *stack_frames[100];
       size_t size;
       long i;
       char **strings;
       StrBuf *FileName;
       StrBuf *Function;
       StrBuf *Pointer;
       StrBuf *Buf;
       unsigned int FunctionLine;
       struct stat filestats;

       FileName = NewStrBuf();
       Function = NewStrBuf();
       Pointer = NewStrBuf();
       Buf = NewStrBuf();

       BaseFrames = size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
       BaseFrames --;
       BaseFrames += AddBaseFrameSkip;
       strings = backtrace_symbols(stack_frames, size);
       for (i = 1; i < size; i++) {
              if (strings != NULL){
                     ParseBacktrace(strings[i], Function, 
                                   FileName, 
                                   &FunctionLine);
                     FullBinaryName = NewStrBufDup(FileName);
                     size = i;
              }
              else {
                     char path[256];
                     getcwd(path, sizeof(path));
                     FullBinaryName = NewStrBufPlain(path, -1);
                     StrBufAppendBufPlain(FullBinaryName, HKEY("/"), 0);
                     StrBufAppendBufPlain(FullBinaryName, argvNull, -1, 0);
                     i = size;
               }
       }
       if ((stat(ChrPtr(FullBinaryName), &filestats)==-1) ||
           (filestats.st_size==0)){
              FlushStrBuf(FullBinaryName);
              StrBufAppendBufPlain(FullBinaryName, argvNull, -1, 0);
              if ((stat(ChrPtr(FullBinaryName), &filestats)==-1) ||
                  (filestats.st_size==0)){
                     FlushStrBuf(FullBinaryName);
                     fprintf(stderr, "unable to open my binary for addr2line checking, verbose backtraces won't work.\n");
              }
       }
       free(strings);
       FreeStrBuf(&FileName);
       FreeStrBuf(&Function);
       FreeStrBuf(&Pointer);
       FreeStrBuf(&Buf);
       if (StrLength(FullBinaryName) > 0)
              start_addr2line_daemon(ChrPtr(FullBinaryName));
#endif


}

Here is the call graph for this function:

Here is the caller graph for this function:

JsonValue* WildFireMessage ( const char *  Filename,
long  fnlen,
long  LineNo,
StrBuf Msg,
WF_MessageType  Type 
)

Definition at line 50 of file wildfire.c.

{
       JsonValue *Ret;

       Ret = NewJsonArray(NULL, 0);
       JsonArrayAppend(Ret, WFInfo(Filename, fnlen,
                                LineNo, Type));

       JsonArrayAppend(Ret, 
                     NewJsonString(NULL, 0, Msg));
       return Ret;
}

Here is the call graph for this function:

JsonValue* WildFireMessagePlain ( const char *  Filename,
long  fnlen,
long  LineNo,
const char *  Message,
long  len,
WF_MessageType  Type 
)

Definition at line 66 of file wildfire.c.

{
       JsonValue *Val;
       Val = NewJsonArray(NULL, 0);

       JsonArrayAppend(Val, WFInfo(Filename, fnlen,
                                LineNo, Type));
       JsonArrayAppend(Val, 
                     NewJsonPlainString(NULL, 0, Message, len));
       return Val;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void WildFireSerializePayload ( StrBuf JsonBuffer,
StrBuf OutBuf,
int *  MsgCount,
AddHeaderFunc  AddHdr 
)

Definition at line 365 of file wildfire.c.

{
       int n = *MsgCount;
       StrBuf *Buf;
       StrBuf *HeaderName;
       StrBuf *N; 
       const char Concatenate[] = "\\";
       const char empty[] = "";
       const char *Cat;
       StrBuf *Header;

       Header = NewStrBuf();
       if (*MsgCount == 0) {
              if (OutBuf != NULL) {
                     StrBufAppendBufPlain(OutBuf, 
                                        HKEY( 
                                               "X-Wf-Protocol-1" 
                                               ": "
                                               "http://meta.wildfirehq.org/Protocol/JsonStream/0.2\r\n"), 0);
                     StrBufAppendBufPlain(OutBuf, 
                                        HKEY( 
                                               "X-Wf-1-Plugin-1" 
                                               ": " 
                                               "http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.2.0\r\n"), 0);
                     StrBufAppendBufPlain(OutBuf, 
                                        HKEY(
                                               "X-Wf-1-Structure-1"
                                               ": "
                                               "http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1\r\n"), 0);
              }
              else {
                     AddHdr("X-Wf-Protocol-1", 
                            "http://meta.wildfirehq.org/Protocol/JsonStream/0.2");
                     AddHdr("X-Wf-1-Plugin-1",
                            "http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.2.0");
                     AddHdr("X-Wf-1-Structure-1",
                            "http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1");
              }
       }

       N = NewStrBuf();
       StrBufPrintf(N, "%d", StrLength(JsonBuffer));
       Buf = NewStrBufPlain(NULL, 1024);
       HeaderName = NewStrBuf();

       while (StrLength(JsonBuffer) > 0) {
              FlushStrBuf(Buf);
              StrBufPrintf(HeaderName, "X-Wf-"WF_MAJOR"-"WF_STRUCTINDEX"-"WF_SUB"-%d", n);
              if (StrLength(JsonBuffer) > 800) {
                     StrBufAppendBufPlain(Buf, ChrPtr(JsonBuffer), 800, 0);
                     StrBufCutLeft(JsonBuffer, 800);
                     Cat = Concatenate;
              }
              else {
                     StrBufAppendBuf(Buf, JsonBuffer, 0);
                     FlushStrBuf(JsonBuffer);
                     Cat = empty;
              }
              if (OutBuf != NULL) {
                     StrBufAppendPrintf(OutBuf, 
                                      "%s: %s|%s|%s\r\n", 
                                      ChrPtr(HeaderName), 
                                      ChrPtr(N),
                                      ChrPtr(Buf), 
                                      Cat);
              }
              else {
                     StrBufAppendPrintf(Header, 
                                      "%s|%s|%s", 
                                      ChrPtr(N),
                                      ChrPtr(Buf), 
                                      Cat);
                     AddHdr(ChrPtr(HeaderName), ChrPtr(Header));
                     
              }

              FlushStrBuf(N);
              n++;
       }
       *MsgCount = n;
       if (OutBuf == NULL) {
              FreeStrBuf(&Header);
       }
       FreeStrBuf(&N);
       FreeStrBuf(&Buf);
       FreeStrBuf(&HeaderName);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void WildFireShutdown ( void  )

Definition at line 197 of file wildfire.c.

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 95 of file wildfire.c.

Definition at line 94 of file wildfire.c.

Definition at line 93 of file wildfire.c.

long BaseFrames = 0

Definition at line 194 of file wildfire.c.

Definition at line 195 of file wildfire.c.

Initial value:
 {
       {HKEY("LOG")},
       {HKEY("INFO")},
       {HKEY("WARN")},
       {HKEY("ERROR")},
       {HKEY("TRACE")},
       {HKEY("EXCEPTION")}
}

Definition at line 21 of file wildfire.c.