Skip to content

Commit f67853f

Browse files
committed
Work in progress
1 parent 4eba325 commit f67853f

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

src/common/commonutils/CommonUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ long GetPerfClockTime(PerfClock* clock, OsConfigLogHandle log);
264264
void LogPerfClock(PerfClock* clock, const char* componentName, const char* objectName, int objectResult, long limit, OsConfigLogHandle log);
265265

266266
void InstallCrashHandler(const char* logFileName);
267+
void ParseLogForPreviousCrashIfAny(const char* logFileName, OsConfigLogHandle log);
267268

268269
#ifdef __cplusplus
269270
}

src/common/commonutils/CrashHandler.c

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#define DEFAULT_LOG_FILE "/var/log/osconfig_nrp.log"
1414
#define MSG_STACK_HDR "[ERROR] Stack trace:" EOL_TERMINATOR
1515

16-
#define OSCONFIG_MAX_FRAMES 32
16+
#define OSCONFIG_MAX_FRAMES 10
1717

1818
static const char* g_logFileName = DEFAULT_LOG_FILE;
1919

@@ -84,3 +84,94 @@ void InstallCrashHandler(const char* logFileName)
8484
sigaction(SIGFPE, &sa, NULL);
8585
sigaction(SIGILL, &sa, NULL);
8686
}
87+
88+
static char* LoadEndOfFile(const char* logFileName, OsConfigLogHandle log)
89+
{
90+
const int maxSize = 2048;
91+
int size = 0;
92+
long offset = 0;
93+
FILE* file = NULL;
94+
char* string = NULL;
95+
96+
if (false == FileExists(logFileName))
97+
{
98+
return string;
99+
}
100+
101+
if (NULL != (file = fopen(logFileName, "r")))
102+
{
103+
if (LockFile(file, log))
104+
{
105+
fseek(file, 0, SEEK_END);
106+
size = (int)ftell(file);
107+
108+
if (size > maxSize)
109+
{
110+
size = maxSize;
111+
}
112+
113+
offset = (long)(ftell(file) - size);
114+
fseek(file, offset, SEEK_SET);
115+
116+
if (NULL != (string = (char*)malloc(size + 1)))
117+
{
118+
memset(string, 0, size + 1);
119+
fread(string, sizeof(char), size, file);
120+
}
121+
else
122+
{
123+
OsConfigLogError(log, "LoadEndOfFile: unable to allocate memory");
124+
}
125+
126+
UnlockFile(file, log);
127+
}
128+
129+
fclose(file);
130+
}
131+
132+
OsConfigLogDebug(log, "LoadEndOfFile: '%s' ends in '%s'", logFileName, string);
133+
134+
return string;
135+
}
136+
137+
static char* GetCrashString(const char* content, const char* marker)
138+
{
139+
char* found = NULL;
140+
141+
if ((NULL == content) || (NULL == marker))
142+
{
143+
return found;
144+
}
145+
146+
return (found = strstr(content, marker));
147+
}
148+
149+
void ParseLogForPreviousCrashIfAny(const char* logFileName, OsConfigLogHandle log)
150+
{
151+
const char* crashDueToMarker = "[ERROR] Crash due to";
152+
const char* stackTraceMarker = "[ERROR] Stack trace:\n";
153+
char* endOfFile = NULL;
154+
char* crashStart = NULL;
155+
char* stackStart = NULL;
156+
char* endOfLine = NULL;
157+
158+
if (NULL != (endOfFile = LoadEndOfFile(logFileName, log)))
159+
{
160+
if (NULL != (crashStart = GetCrashString(endOfFile, crashDueToMarker)))
161+
{
162+
// Search for stack trace before mutating crashStart
163+
stackStart = GetCrashString(endOfFile, stackTraceMarker);
164+
165+
// Null-terminate the crash header line
166+
if (NULL != (endOfLine = strchr(crashStart, '\n')))
167+
{
168+
endOfLine[0] = 0;
169+
}
170+
171+
OsConfigLogInfo(log, "### Crash start: '%s'", crashStart);
172+
OsConfigLogInfo(log, "### Stack start: '%s'", stackStart);
173+
}
174+
}
175+
176+
FREE_MEMORY(endOfFile);
177+
}

src/common/tests/CommonUtilsUT.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3261,6 +3261,8 @@ TEST_F(CommonUtilsTest, CrashHandler)
32613261
EXPECT_NE(nullptr, result = strstr(contents, "[ERROR] Crash due to segmentation fault (SIGSEGV)"));
32623262
EXPECT_NE(nullptr, result = strstr(contents, "[ERROR] Stack trace:"));
32633263

3264+
ParseLogForPreviousCrashIfAny(m_path, nullptr);
3265+
32643266
CloseLog(&log);
32653267
EXPECT_TRUE(Cleanup(m_path));
32663268
}

0 commit comments

Comments
 (0)