Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.

Commit 53e2f16

Browse files
committed
Code refactoring: extract path prefix handling code
.. into reusable interfaces. No functional change is expected. git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@275807 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 3e87a26 commit 53e2f16

File tree

3 files changed

+74
-36
lines changed

3 files changed

+74
-36
lines changed

lib/profile/GCDAProfiling.c

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
|*
2121
\*===----------------------------------------------------------------------===*/
2222

23-
#include "InstrProfilingUtil.h"
23+
#include "InstrProfilingInternal.h"
2424
#include "InstrProfilingPort.h"
25+
#include "InstrProfilingUtil.h"
2526

2627
#include <errno.h>
2728
#include <fcntl.h>
@@ -171,45 +172,16 @@ static uint64_t read_64bit_value() {
171172

172173
static char *mangle_filename(const char *orig_filename) {
173174
char *new_filename;
174-
size_t filename_len, prefix_len;
175+
size_t prefix_len;
175176
int prefix_strip;
176-
int level = 0;
177-
const char *fname, *ptr;
178-
const char *prefix = getenv("GCOV_PREFIX");
179-
const char *prefix_strip_str = getenv("GCOV_PREFIX_STRIP");
177+
const char *prefix = lprofGetPathPrefix(&prefix_strip, &prefix_len);
180178

181-
if (prefix == NULL || prefix[0] == '\0')
179+
if (prefix == NULL)
182180
return strdup(orig_filename);
183181

184-
if (prefix_strip_str) {
185-
prefix_strip = atoi(prefix_strip_str);
186-
187-
/* Negative GCOV_PREFIX_STRIP values are ignored */
188-
if (prefix_strip < 0)
189-
prefix_strip = 0;
190-
} else {
191-
prefix_strip = 0;
192-
}
193-
194-
fname = orig_filename;
195-
for (level = 0, ptr = fname + 1; level < prefix_strip; ++ptr) {
196-
if (*ptr == '\0')
197-
break;
198-
199-
if (!IS_DIR_SEPARATOR(*ptr))
200-
continue;
201-
fname = ptr;
202-
++level;
203-
}
204-
205-
filename_len = strlen(fname);
206-
prefix_len = strlen(prefix);
207-
new_filename = malloc(prefix_len + 1 + filename_len + 1);
208-
memcpy(new_filename, prefix, prefix_len);
209-
210-
if (!IS_DIR_SEPARATOR(prefix[prefix_len - 1]))
211-
new_filename[prefix_len++] = DIR_SEPARATOR;
212-
memcpy(new_filename + prefix_len, fname, filename_len + 1);
182+
new_filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1);
183+
lprofApplyPathPrefix(new_filename, orig_filename, prefix, prefix_len,
184+
prefix_strip);
213185

214186
return new_filename;
215187
}

lib/profile/InstrProfilingInternal.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,20 @@ void lprofSetupValueProfiler();
163163
* to dump merged profile data into its own profile file. */
164164
uint64_t lprofGetLoadModuleSignature();
165165

166+
/* GCOV_PREFIX and GCOV_PREFIX_STRIP support */
167+
/* Return the path prefix specified by GCOV_PREFIX environment variable.
168+
* If GCOV_PREFIX_STRIP is also specified, the strip level (integer value)
169+
* is returned via \c *PrefixStrip. The prefix length is stored in *PrefixLen.
170+
*/
171+
const char *lprofGetPathPrefix(int *PrefixStrip, size_t *PrefixLen);
172+
/* Apply the path prefix specified in \c Prefix to path string in \c PathStr,
173+
* and store the result to buffer pointed to by \c Buffer. If \c PrefixStrip
174+
* is not zero, path prefixes are stripped from \c PathStr (the level of
175+
* stripping is specified by \c PrefixStrip) before \c Prefix is added.
176+
*/
177+
void lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
178+
size_t PrefixLen, int PrefixStrip);
179+
166180
COMPILER_RT_VISIBILITY extern char *(*GetEnvHook)(const char *);
167181
COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *);
168182
COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer;

lib/profile/InstrProfilingUtil.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <sys/utsname.h>
2727
#endif
2828

29+
#include <stdlib.h>
2930
#include <string.h>
3031

3132
COMPILER_RT_VISIBILITY
@@ -132,3 +133,54 @@ COMPILER_RT_VISIBILITY FILE *lprofOpenFileEx(const char *ProfileName) {
132133

133134
return f;
134135
}
136+
137+
COMPILER_RT_VISIBILITY const char *lprofGetPathPrefix(int *PrefixStrip,
138+
size_t *PrefixLen) {
139+
const char *Prefix = getenv("GCOV_PREFIX");
140+
const char *PrefixStripStr = getenv("GCOV_PREFIX_STRIP");
141+
142+
*PrefixLen = 0;
143+
*PrefixStrip = 0;
144+
if (Prefix == NULL || Prefix[0] == '\0')
145+
return NULL;
146+
147+
if (PrefixStripStr) {
148+
*PrefixStrip = atoi(PrefixStripStr);
149+
150+
/* Negative GCOV_PREFIX_STRIP values are ignored */
151+
if (*PrefixStrip < 0)
152+
*PrefixStrip = 0;
153+
} else {
154+
*PrefixStrip = 0;
155+
}
156+
*PrefixLen = strlen(Prefix);
157+
158+
return Prefix;
159+
}
160+
161+
COMPILER_RT_VISIBILITY void
162+
lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
163+
size_t PrefixLen, int PrefixStrip) {
164+
165+
const char *Ptr;
166+
int Level;
167+
const char *StrippedPathStr = PathStr;
168+
169+
for (Level = 0, Ptr = PathStr + 1; Level < PrefixStrip; ++Ptr) {
170+
if (*Ptr == '\0')
171+
break;
172+
173+
if (!IS_DIR_SEPARATOR(*Ptr))
174+
continue;
175+
176+
StrippedPathStr = Ptr;
177+
++Level;
178+
}
179+
180+
memcpy(Dest, Prefix, PrefixLen);
181+
182+
if (!IS_DIR_SEPARATOR(Prefix[PrefixLen - 1]))
183+
Dest[PrefixLen++] = DIR_SEPARATOR;
184+
185+
memcpy(Dest + PrefixLen, StrippedPathStr, strlen(StrippedPathStr) + 1);
186+
}

0 commit comments

Comments
 (0)