1 /*===- InstrProfilingUtil.h - Support library for PGO instrumentation -----===*\
2 |*
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 |* See https://llvm.org/LICENSE.txt for license information.
5 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 |*
7 \*===----------------------------------------------------------------------===*/
8 
9 #ifndef PROFILE_INSTRPROFILINGUTIL_H
10 #define PROFILE_INSTRPROFILINGUTIL_H
11 
12 #include <stddef.h>
13 #include <stdio.h>
14 
15 /*! \brief Create a directory tree. */
16 void __llvm_profile_recursive_mkdir(char *Pathname);
17 
18 /*! Set the mode used when creating profile directories. */
19 void __llvm_profile_set_dir_mode(unsigned Mode);
20 
21 /*! Return the directory creation mode. */
22 unsigned __llvm_profile_get_dir_mode(void);
23 
24 int lprofLockFd(int fd);
25 int lprofUnlockFd(int fd);
26 int lprofLockFileHandle(FILE *F);
27 int lprofUnlockFileHandle(FILE *F);
28 
29 /*! Open file \c Filename for read+write with write
30  * lock for exclusive access. The caller will block
31  * if the lock is already held by another process. */
32 FILE *lprofOpenFileEx(const char *Filename);
33 /* PS4 doesn't have setenv/getenv. Define a shim. */
34 #if __ORBIS__
35 static inline char *getenv(const char *name) { return NULL; }
36 static inline int setenv(const char *name, const char *value, int overwrite)
37 { return 0; }
38 #endif /* #if __ORBIS__ */
39 
40 /* GCOV_PREFIX and GCOV_PREFIX_STRIP support */
41 /* Return the path prefix specified by GCOV_PREFIX environment variable.
42  * If GCOV_PREFIX_STRIP is also specified, the strip level (integer value)
43  * is returned via \c *PrefixStrip. The prefix length is stored in *PrefixLen.
44  */
45 const char *lprofGetPathPrefix(int *PrefixStrip, size_t *PrefixLen);
46 /* Apply the path prefix specified in \c Prefix to path string in \c PathStr,
47  * and store the result to buffer pointed to by \c Buffer. If \c PrefixStrip
48  * is not zero, path prefixes are stripped from \c PathStr (the level of
49  * stripping is specified by \c PrefixStrip) before \c Prefix is added.
50  */
51 void lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
52                           size_t PrefixLen, int PrefixStrip);
53 
54 /* Returns a pointer to the first occurrence of \c DIR_SEPARATOR char in
55  * the string \c Path, or NULL if the char is not found. */
56 const char *lprofFindFirstDirSeparator(const char *Path);
57 /* Returns a pointer to the last occurrence of \c DIR_SEPARATOR char in
58  * the string \c Path, or NULL if the char is not found. */
59 const char *lprofFindLastDirSeparator(const char *Path);
60 
61 int lprofGetHostName(char *Name, int Len);
62 
63 unsigned lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV);
64 void *lprofPtrFetchAdd(void **Mem, long ByteIncr);
65 
66 /* Temporarily suspend SIGKILL. Return value of 1 means a restore is needed.
67  * Other return values mean no restore is needed.
68  */
69 int lprofSuspendSigKill();
70 
71 /* Restore previously suspended SIGKILL. */
72 void lprofRestoreSigKill();
73 
74 #endif /* PROFILE_INSTRPROFILINGUTIL_H */
75