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 <inttypes.h>
13#include <stddef.h>
14#include <stdio.h>
15
16/*! \brief Create a directory tree. */
17void __llvm_profile_recursive_mkdir(char *Pathname);
18
19/*! Set the mode used when creating profile directories. */
20void __llvm_profile_set_dir_mode(unsigned Mode);
21
22/*! Return the directory creation mode. */
23unsigned __llvm_profile_get_dir_mode(void);
24
25int lprofLockFd(int fd);
26int lprofUnlockFd(int fd);
27int lprofLockFileHandle(FILE *F);
28int lprofUnlockFileHandle(FILE *F);
29
30/*! Open file \c Filename for read+write with write
31 * lock for exclusive access. The caller will block
32 * if the lock is already held by another process. */
33FILE *lprofOpenFileEx(const char *Filename);
34
35enum MemoryStatus {
36 MS_INVALID, // Addr is not a valid address
37 MS_MMAP, // Addr was mmap'ed
38 MS_MALLOC // Addr was malloc'ed
39};
40typedef struct {
41 void *Addr;
42 enum MemoryStatus Status;
43} ManagedMemory;
44
45/* Read the content of a file using mmap or fread into a buffer.
46 * Certain files (e.g. NFS mounted) cannot be opened reliably with mmap,
47 * so we use fread in those cases. The corresponding lprofReleaseBuffer
48 * will free/munmap the buffer.
49 */
50void lprofGetFileContentBuffer(FILE *F, uint64_t FileSize, ManagedMemory *Buf);
51void lprofReleaseBuffer(ManagedMemory *FileBuffer, size_t Length);
52
53/* PS4 doesn't have setenv/getenv/fork. Define a shim. */
54#if __ORBIS__
55#include <sys/types.h>
56static inline char *getenv(const char *name) { return NULL; }
57static inline int setenv(const char *name, const char *value, int overwrite)
58{ return 0; }
59static pid_t fork() { return -1; }
60#endif /* #if __ORBIS__ */
61
62/* GCOV_PREFIX and GCOV_PREFIX_STRIP support */
63/* Return the path prefix specified by GCOV_PREFIX environment variable.
64 * If GCOV_PREFIX_STRIP is also specified, the strip level (integer value)
65 * is returned via \c *PrefixStrip. The prefix length is stored in *PrefixLen.
66 */
67const char *lprofGetPathPrefix(int *PrefixStrip, size_t *PrefixLen);
68/* Apply the path prefix specified in \c Prefix to path string in \c PathStr,
69 * and store the result to buffer pointed to by \c Buffer. If \c PrefixStrip
70 * is not zero, path prefixes are stripped from \c PathStr (the level of
71 * stripping is specified by \c PrefixStrip) before \c Prefix is added.
72 */
73void lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
74 size_t PrefixLen, int PrefixStrip);
75
76/* Returns a pointer to the first occurrence of \c DIR_SEPARATOR char in
77 * the string \c Path, or NULL if the char is not found. */
78const char *lprofFindFirstDirSeparator(const char *Path);
79/* Returns a pointer to the last occurrence of \c DIR_SEPARATOR char in
80 * the string \c Path, or NULL if the char is not found. */
81const char *lprofFindLastDirSeparator(const char *Path);
82
83int lprofGetHostName(char *Name, int Len);
84
85unsigned lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV);
86void *lprofPtrFetchAdd(void **Mem, long ByteIncr);
87
88/* Temporarily suspend SIGKILL. Return value of 1 means a restore is needed.
89 * Other return values mean no restore is needed.
90 */
91int lprofSuspendSigKill(void);
92
93/* Restore previously suspended SIGKILL. */
94void lprofRestoreSigKill(void);
95
96static inline size_t lprofRoundUpTo(size_t x, size_t boundary) {
97 return (x + boundary - 1) & ~(boundary - 1);
98}
99
100static inline size_t lprofRoundDownTo(size_t x, size_t boundary) {
101 return x & ~(boundary - 1);
102}
103
104int lprofReleaseMemoryPagesToOS(uintptr_t Begin, uintptr_t End);
105
106typedef void (*AtExit_Fn_ptr)(void);
107
108/* Call atexit and perform other platform-specific bookkeeping. */
109int lprofAtExit(AtExit_Fn_ptr);
110
111#endif /* PROFILE_INSTRPROFILINGUTIL_H */
112