xref: /aosp_15_r20/art/libartbase/base/fast_exit.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBARTBASE_BASE_FAST_EXIT_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBARTBASE_BASE_FAST_EXIT_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker // Header-only definition of `art::FastExit`.
21*795d594fSAndroid Build Coastguard Worker //
22*795d594fSAndroid Build Coastguard Worker // Ideally, this routine should be declared in `base/os.h` and defined in
23*795d594fSAndroid Build Coastguard Worker // `base/os_linux.cc`, but as `libartbase` is not linked (directly) with
24*795d594fSAndroid Build Coastguard Worker // `dalvikvm`, we would not be able to easily use `art::FastExit` in
25*795d594fSAndroid Build Coastguard Worker // `dex2oat`. Use a header-only approach and define `art::FastExit` in its own
26*795d594fSAndroid Build Coastguard Worker // file for clarity.
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker #include <base/macros.h>
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker namespace art {
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker #ifdef __ANDROID_CLANG_COVERAGE__
33*795d594fSAndroid Build Coastguard Worker static constexpr bool kAndroidClangCoverage = true;
34*795d594fSAndroid Build Coastguard Worker #else
35*795d594fSAndroid Build Coastguard Worker static constexpr bool kAndroidClangCoverage = false;
36*795d594fSAndroid Build Coastguard Worker #endif
37*795d594fSAndroid Build Coastguard Worker 
38*795d594fSAndroid Build Coastguard Worker // Terminate program without completely cleaning the resources (e.g. without
39*795d594fSAndroid Build Coastguard Worker // calling destructors), unless ART is built with Clang (native) code coverage
40*795d594fSAndroid Build Coastguard Worker // instrumentation; in that case, exit normally to allow LLVM's code coverage
41*795d594fSAndroid Build Coastguard Worker // profile dumping routine (`__llvm_profile_write_file`), registered via
42*795d594fSAndroid Build Coastguard Worker // `atexit` in Android when Clang instrumentation is enabled, to be called
43*795d594fSAndroid Build Coastguard Worker // before terminating the program.
FastExit(int exit_code)44*795d594fSAndroid Build Coastguard Worker NO_RETURN inline void FastExit(int exit_code) {
45*795d594fSAndroid Build Coastguard Worker   if (kAndroidClangCoverage) {
46*795d594fSAndroid Build Coastguard Worker     exit(exit_code);
47*795d594fSAndroid Build Coastguard Worker   } else {
48*795d594fSAndroid Build Coastguard Worker     _exit(exit_code);
49*795d594fSAndroid Build Coastguard Worker   }
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker }  // namespace art
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBARTBASE_BASE_FAST_EXIT_H_
55