annotate libsanitizer/sanitizer_common/sanitizer_file.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 //===-- sanitizer_file.h ---------------------------------------*- C++ -*-===//
kono
parents:
diff changeset
2 //
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
111
kono
parents:
diff changeset
6 //
kono
parents:
diff changeset
7 //===---------------------------------------------------------------------===//
kono
parents:
diff changeset
8 //
kono
parents:
diff changeset
9 // This file is shared between run-time libraries of sanitizers.
kono
parents:
diff changeset
10 // It declares filesystem-related interfaces. This is separate from
kono
parents:
diff changeset
11 // sanitizer_common.h so that it's simpler to disable all the filesystem
kono
parents:
diff changeset
12 // support code for a port that doesn't use it.
kono
parents:
diff changeset
13 //
kono
parents:
diff changeset
14 //===---------------------------------------------------------------------===//
kono
parents:
diff changeset
15 #ifndef SANITIZER_FILE_H
kono
parents:
diff changeset
16 #define SANITIZER_FILE_H
kono
parents:
diff changeset
17
kono
parents:
diff changeset
18 #include "sanitizer_interface_internal.h"
kono
parents:
diff changeset
19 #include "sanitizer_internal_defs.h"
kono
parents:
diff changeset
20 #include "sanitizer_libc.h"
kono
parents:
diff changeset
21 #include "sanitizer_mutex.h"
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 namespace __sanitizer {
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 struct ReportFile {
kono
parents:
diff changeset
26 void Write(const char *buffer, uptr length);
kono
parents:
diff changeset
27 bool SupportsColors();
kono
parents:
diff changeset
28 void SetReportPath(const char *path);
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 // Don't use fields directly. They are only declared public to allow
kono
parents:
diff changeset
31 // aggregate initialization.
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 // Protects fields below.
kono
parents:
diff changeset
34 StaticSpinMutex *mu;
kono
parents:
diff changeset
35 // Opened file descriptor. Defaults to stderr. It may be equal to
kono
parents:
diff changeset
36 // kInvalidFd, in which case new file will be opened when necessary.
kono
parents:
diff changeset
37 fd_t fd;
kono
parents:
diff changeset
38 // Path prefix of report file, set via __sanitizer_set_report_path.
kono
parents:
diff changeset
39 char path_prefix[kMaxPathLength];
kono
parents:
diff changeset
40 // Full path to report, obtained as <path_prefix>.PID
kono
parents:
diff changeset
41 char full_path[kMaxPathLength];
kono
parents:
diff changeset
42 // PID of the process that opened fd. If a fork() occurs,
kono
parents:
diff changeset
43 // the PID of child will be different from fd_pid.
kono
parents:
diff changeset
44 uptr fd_pid;
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 private:
kono
parents:
diff changeset
47 void ReopenIfNecessary();
kono
parents:
diff changeset
48 };
kono
parents:
diff changeset
49 extern ReportFile report_file;
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 enum FileAccessMode {
kono
parents:
diff changeset
52 RdOnly,
kono
parents:
diff changeset
53 WrOnly,
kono
parents:
diff changeset
54 RdWr
kono
parents:
diff changeset
55 };
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 // Returns kInvalidFd on error.
kono
parents:
diff changeset
58 fd_t OpenFile(const char *filename, FileAccessMode mode,
kono
parents:
diff changeset
59 error_t *errno_p = nullptr);
kono
parents:
diff changeset
60 void CloseFile(fd_t);
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 // Return true on success, false on error.
kono
parents:
diff changeset
63 bool ReadFromFile(fd_t fd, void *buff, uptr buff_size,
kono
parents:
diff changeset
64 uptr *bytes_read = nullptr, error_t *error_p = nullptr);
kono
parents:
diff changeset
65 bool WriteToFile(fd_t fd, const void *buff, uptr buff_size,
kono
parents:
diff changeset
66 uptr *bytes_written = nullptr, error_t *error_p = nullptr);
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 // Scoped file handle closer.
kono
parents:
diff changeset
69 struct FileCloser {
kono
parents:
diff changeset
70 explicit FileCloser(fd_t fd) : fd(fd) {}
kono
parents:
diff changeset
71 ~FileCloser() { CloseFile(fd); }
kono
parents:
diff changeset
72 fd_t fd;
kono
parents:
diff changeset
73 };
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 bool SupportsColoredOutput(fd_t fd);
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 // OS
kono
parents:
diff changeset
78 const char *GetPwd();
kono
parents:
diff changeset
79 bool FileExists(const char *filename);
kono
parents:
diff changeset
80 char *FindPathToBinary(const char *name);
kono
parents:
diff changeset
81 bool IsPathSeparator(const char c);
kono
parents:
diff changeset
82 bool IsAbsolutePath(const char *path);
kono
parents:
diff changeset
83 // Starts a subprocess and returs its pid.
kono
parents:
diff changeset
84 // If *_fd parameters are not kInvalidFd their corresponding input/output
kono
parents:
diff changeset
85 // streams will be redirect to the file. The files will always be closed
kono
parents:
diff changeset
86 // in parent process even in case of an error.
kono
parents:
diff changeset
87 // The child process will close all fds after STDERR_FILENO
kono
parents:
diff changeset
88 // before passing control to a program.
kono
parents:
diff changeset
89 pid_t StartSubprocess(const char *filename, const char *const argv[],
kono
parents:
diff changeset
90 fd_t stdin_fd = kInvalidFd, fd_t stdout_fd = kInvalidFd,
kono
parents:
diff changeset
91 fd_t stderr_fd = kInvalidFd);
kono
parents:
diff changeset
92 // Checks if specified process is still running
kono
parents:
diff changeset
93 bool IsProcessRunning(pid_t pid);
kono
parents:
diff changeset
94 // Waits for the process to finish and returns its exit code.
kono
parents:
diff changeset
95 // Returns -1 in case of an error.
kono
parents:
diff changeset
96 int WaitForProcess(pid_t pid);
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 // Maps given file to virtual memory, and returns pointer to it
kono
parents:
diff changeset
99 // (or NULL if mapping fails). Stores the size of mmaped region
kono
parents:
diff changeset
100 // in '*buff_size'.
kono
parents:
diff changeset
101 void *MapFileToMemory(const char *file_name, uptr *buff_size);
kono
parents:
diff changeset
102 void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset);
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 } // namespace __sanitizer
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 #endif // SANITIZER_FILE_H