-
Notifications
You must be signed in to change notification settings - Fork 379
/
ldlogger-tool.c
171 lines (147 loc) · 3.68 KB
/
ldlogger-tool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* -------------------------------------------------------------------------
* The CodeChecker Infrastructure
* This file is distributed under the University of Illinois Open Source
* License. See LICENSE.TXT for details.
* -------------------------------------------------------------------------
*/
#include "ldlogger-tool.h"
#include "ldlogger-util.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define PROG_LIST_SEPARATOR ":"
extern char** environ;
/**
* Reads a colon separated list from the given environment variable and tries
* to match to the given program name. If one program matches (from the list)
* to the given program name, then it will return a non zero value.
*
* If a (PROG_LIST_SEPARATOR separated) member of the envVar_ contains a slash
* character then we assume that this is a postfix of a compiler's path.
* Otherwise we assume that this is an infix of a compier's name.
*
* On any error or mismatch the function returns 0.
*
* @param envVar_ the name of environment variable.
* @param progPath_ program path to match.
* @return non zero on match, 0 otherwise
*/
static int matchToProgramList(
const char* envVar_,
const char* progPath_)
{
char* progList;
char* token;
const char* progListVar = getenv(envVar_);
if (!progListVar)
{
return 0;
}
progList = loggerStrDup(progListVar);
if (!progList)
{
return 0;
}
token = strtok(progList, PROG_LIST_SEPARATOR);
while (token)
{
int found = 0;
if (strchr(token, '/'))
{
const char* posOfToken = strstr(progPath_, token);
if (posOfToken)
found = strcmp(posOfToken, token) == 0;
}
else
{
const char* progName = strrchr(progPath_, '/');
if (progName)
++progName;
else
progName = progPath_;
found = strstr(progName, token) != NULL;
}
if (found)
{
/* Match! */
free(progList);
return 1;
}
token = strtok(NULL, PROG_LIST_SEPARATOR);
}
free(progList);
return 0;
}
/**
* Disable / enable recursive logging.
*/
static void turnLogging(int on)
{
int i;
for (i = 0; environ[i]; ++i)
{
if (strstr(environ[i], "LD_PRELOAD="))
{
environ[i][0] = on ? 'L' : 'X';
}
}
}
LoggerFile* loggerFileInitFromPath(LoggerFile* file_, const char* path_)
{
assert(file_ && "file_ must be not NULL!");
if (!loggerMakePathAbs(path_, file_->path, 0))
{
/* fallback to the given path */
strcpy(file_->path, path_);
}
return file_;
}
LoggerAction* loggerActionNew()
{
LoggerAction* act = (LoggerAction*) malloc(sizeof(LoggerAction));
if (!act)
{
return NULL;
}
loggerFileInitFromPath(&act->output, "./_noobj");
loggerVectorInit(&act->arguments);
loggerVectorInit(&act->sources);
return act;
}
void loggerActionFree(LoggerAction* act_)
{
if (!act_)
{
return;
}
loggerVectorClear(&act_->arguments);
loggerVectorClear(&act_->sources);
free(act_);
}
int loggerCollectActionsByProgName(
const char* prog_,
const char* const argv_[],
LoggerVector* actions_)
{
if (matchToProgramList("CC_LOGGER_GCC_LIKE", prog_))
{
int ret;
turnLogging(0);
ret = loggerGccParserCollectActions(prog_, argv_, actions_);
turnLogging(1);
return ret;
}
else if (matchToProgramList("CC_LOGGER_JAVAC_LIKE", prog_))
{
return loggerJavacParserCollectActions(prog_, argv_, actions_);
} else {
LOG_INFO("'%s' does not match any program name! Current environment "
"variables are: CC_LOGGER_GCC_LIKE (%s), "
"CC_LOGGER_JAVAC_LIKE(%s)",
prog_,
getenv("CC_LOGGER_GCC_LIKE"),
getenv("CC_LOGGER_JAVAC_LIKE"))
}
return 0;
}