2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/common/mutex-trace.h>
21 #ifdef LOCK_BACKTRACE_ENABLED
27 #include <dali/integration-api/debug.h>
28 #include <dali/public-api/common/dali-common.h>
30 #endif // LOCK_BACKTRACE_ENABLED
33 #include <dali/integration-api/debug.h>
37 #ifdef LOCK_BACKTRACE_ENABLED
38 extern std::string Demangle(const char* symbol);
39 #endif // LOCK_BACKTRACE_ENABLED
47 #ifdef LOCK_BACKTRACE_ENABLED
50 const unsigned int MAX_NUM_STACK_FRAMES = 4;
51 const unsigned int MAX_LOCK_SUPPORT = 5;
55 void* frameArray[MAX_NUM_STACK_FRAMES]; ///< Stores the frame array where the lock occurred
56 int size; ///< Number of frames in the frame array (can be less than MAX_NUM_STACK_FRAMES)
59 thread_local BackTraceInfo gBackTraceInfo[MAX_LOCK_SUPPORT]; ///< Thread local storage for the backtrace of the locks
61 #endif // LOCK_BACKTRACE_ENABLED
63 thread_local unsigned int gThreadLockCount = 0; ///< Thread local storage for the backtrace of the locks
64 } // unnamed namespace
70 #ifdef LOCK_BACKTRACE_ENABLED
72 if(gThreadLockCount <= MAX_LOCK_SUPPORT)
74 // Store the frame array for this lock
75 int backTraceIndex = gThreadLockCount - 1;
76 gBackTraceInfo[backTraceIndex].size = backtrace(gBackTraceInfo[backTraceIndex].frameArray, MAX_NUM_STACK_FRAMES);
80 DALI_LOG_ERROR("Reached Maximum lock backtrace support. Previous Locks:\n");
83 // If we've got more than one lock, then show a warning with a backtrace for all locks that we currently hold
84 if(gThreadLockCount > 1)
86 for(unsigned int i = 0; (i < gThreadLockCount) && (i < MAX_LOCK_SUPPORT); ++i)
88 DALI_LOG_WARNING("[Lock %d]\n", i + 1);
89 char** symbols = backtrace_symbols(gBackTraceInfo[i].frameArray, gBackTraceInfo[i].size);
90 for(int j = 1; j < gBackTraceInfo[i].size; ++j)
92 std::string demangled_symbol = Demangle(symbols[j]);
93 DALI_LOG_WARNING(" [%02d] %s\n", j, demangled_symbol.c_str());
97 DALI_LOG_WARNING("====================================\n");
102 // TODO: Uncomment when locks held per thread at any given time are 1
103 //DALI_ASSERT_DEBUG( gThreadLockCount == 1 );
105 #endif // LOCK_BACKTRACE_ENABLED
113 } // namespace MutexTrace
115 } // namespace Internal