xref: /llvm-project/compiler-rt/test/profile/ContinuousSyncMode/pid-substitution.c (revision de294c968bf292794ca9f0a6a481d3dff3bcc2eb)
1*de294c96SNAKAMURA Takumi // REQUIRES: continuous-mode
2d3db13afSPetr Hosek 
3d889d1efSVedant Kumar // RUN: rm -rf %t.dir && mkdir -p %t.dir
44f2651c3SWael Yehia // RUN: %clang_pgogen_cont -o %t.exe %s
5d889d1efSVedant Kumar //
6d889d1efSVedant Kumar // Note: %%p is needed here, not %p, because of lit's path substitution.
7d889d1efSVedant Kumar // RUN: env LLVM_PROFILE_FILE="%t.dir/%c-%%p" %run %t.exe
8d889d1efSVedant Kumar 
9d889d1efSVedant Kumar #include <stdlib.h>
10d889d1efSVedant Kumar #include <string.h>
11d889d1efSVedant Kumar 
12d889d1efSVedant Kumar extern int __llvm_profile_is_continuous_mode_enabled(void);
13d889d1efSVedant Kumar extern const char *__llvm_profile_get_filename(void);
14d889d1efSVedant Kumar extern int getpid(void);
15d889d1efSVedant Kumar 
16d889d1efSVedant Kumar int main() {
17d889d1efSVedant Kumar   // Check that continuous mode is enabled.
18d889d1efSVedant Kumar   if (!__llvm_profile_is_continuous_mode_enabled())
19d889d1efSVedant Kumar     return 1;
20d889d1efSVedant Kumar 
21d889d1efSVedant Kumar   // Check that the PID is actually in the filename.
22d889d1efSVedant Kumar   const char *Filename = __llvm_profile_get_filename();
23d889d1efSVedant Kumar 
24d889d1efSVedant Kumar   int Len = strlen(Filename);
25d889d1efSVedant Kumar   --Len;
26d889d1efSVedant Kumar   while (Filename[Len] != '-')
27d889d1efSVedant Kumar     --Len;
28d889d1efSVedant Kumar 
29d889d1efSVedant Kumar   const char *PidStr = Filename + Len + 1;
30d889d1efSVedant Kumar   int Pid = atoi(PidStr);
31d889d1efSVedant Kumar 
32d889d1efSVedant Kumar   if (Pid != getpid())
33d889d1efSVedant Kumar     return 1;
34d889d1efSVedant Kumar 
35d889d1efSVedant Kumar   return 0;
36d889d1efSVedant Kumar }
37