Skip to content

Commit f2abaa6

Browse files
committed
Added some documentation
1 parent 7e345da commit f2abaa6

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

README.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,86 @@
11
# rtcheck
22
Dynamic library to catch run-time safety violations heavily inspired by [RADSan](https://github.com/realtime-sanitizer/radsan)
33

4+
## Contents
5+
- [Adding rtcheck to a project](#adding-rtcheck-to-a-project)
6+
- [Using rtcheck](#using-rtcheck)
7+
- [Disabling checks](#disabling-checks)
8+
- [Catching your own violations](#catching-your-own-violations)
9+
- [Error modes](#error-modes)
10+
11+
## Adding rtcheck to a project
12+
13+
## Using rtcheck
14+
Simply add an instance of rtc::realtime_context at the start of the scope you want to check for real-time safety violations like this:
15+
```c++
16+
int main()
17+
{
18+
std::thread t ([]
19+
{
20+
rtc::realtime_context rc;
21+
malloc (1024); // Allocating memory is a real-time safety violation!
22+
});
23+
24+
t.join();
25+
26+
return 0;
27+
}
28+
```
29+
The call to malloc in the above code will then be caught and logged as so:
30+
```
31+
Real-time violation: intercepted call to real-time unsafe function malloc in real-time context! Stack trace:
32+
0 librt_check.dylib 0x000000010543f7d4 _ZN3rtc14get_stacktraceEv + 84
33+
1 librt_check.dylib 0x000000010543f450 _ZN3rtc32log_function_if_realtime_contextEPKc + 300
34+
2 librt_check.dylib 0x000000010543fe00 _Z44log_function_if_realtime_context_and_enabledN3rtc11check_flagsEPKc + 64
35+
3 librt_check.dylib 0x000000010543fe30 wrap_malloc + 32
36+
4 fail_malloc 0x0000000104fcff14 main + 32
37+
5 dyld 0x00000001901760e0 start + 2360
38+
```
39+
40+
## Disabling checks
41+
There are two ways to disable checks. You can either disable all checks, which can be useful if you
42+
know you'll be calling a potentially unsafe function but in a safe way (e.g. an un-contented lock), or you want to log something:
43+
```c++
44+
{
45+
rtc::non_realtime_context nrc;
46+
std::cout << "need to get this message on the screen!";
47+
}
48+
```
49+
Or you can selectively disable checks:
50+
```c++
51+
{
52+
rtc::disable_checks_for_thread (check_flags::threads);
53+
mutex.lock(); // I know this is uncontended, don't for get to unlock!
54+
}
55+
```
56+
57+
## Catching your own violations
58+
If you have some code which you know is non-real-time safe e.g. an unbounded distribution function or some other async call, you can opt-in to let rtcheck catch it by calling the following function:
59+
```c++
60+
void my_unsafe_function()
61+
{
62+
log_function_if_realtime_context (__func__);
63+
}
64+
```
65+
This will then get logged if called whilst a `rtc::realtime_context` is alive.
66+
67+
## Error Modes
68+
There are two currently supported error modes
69+
- Exit with error code 1 (default)
70+
- Log and continue
71+
72+
Exiting it useful for CI runs where you want the program to terminate in an obvious way (non-zero exit code) to fail the run.
73+
In normal use though, you may just want to log the violation and continue.
74+
75+
You can change between these globally using the following function:
76+
```c++
77+
/** Sets the global error more to determine the behaviour when a real-time
78+
violation is detected.
79+
*/
80+
void set_error_mode (error_mode);
81+
```
82+
---
83+
# Notes:
484
## Features
585
- [x] Enable in scope
686
- [x] Disable in scope
@@ -78,7 +158,7 @@ Dynamic library to catch run-time safety violations heavily inspired by [RADSan]
78158
- Passes
79159
- [x] Atomic double
80160
- [x] Small std::function
81-
- [ ] thread_local? (pass on linux, fail on macOS)
161+
- [x] thread_local? (pass on linux, fail on macOS)
82162
- [x] Running on CI Linux
83163
- [x] Running on CI macOS
84164
- [x] Tests via CTest (can catch failues)

0 commit comments

Comments
 (0)