| 126 | BAIDU_THREAD_LOCAL TaskMeta* pthread_fake_meta = NULL; |
| 127 | |
| 128 | bthread_t init_for_pthread_stack_trace() { |
| 129 | if (NULL != pthread_fake_meta) { |
| 130 | return pthread_fake_meta->tid; |
| 131 | } |
| 132 | |
| 133 | TaskControl* c = get_task_control(); |
| 134 | if (NULL == c) { |
| 135 | LOG(ERROR) << "TaskControl has not been created, " |
| 136 | "please use bthread_start_xxx before call this function"; |
| 137 | return INVALID_BTHREAD; |
| 138 | } |
| 139 | |
| 140 | butil::ResourceId<TaskMeta> slot; |
| 141 | pthread_fake_meta = butil::get_resource(&slot); |
| 142 | if (BAIDU_UNLIKELY(NULL == pthread_fake_meta)) { |
| 143 | LOG(ERROR) << "Fail to get TaskMeta"; |
| 144 | return INVALID_BTHREAD; |
| 145 | } |
| 146 | |
| 147 | pthread_fake_meta->attr = BTHREAD_ATTR_PTHREAD; |
| 148 | pthread_fake_meta->tid = make_tid(*pthread_fake_meta->version_butex, slot); |
| 149 | // Make TaskTracer use signal trace mode for pthread. |
| 150 | c->_task_tracer.set_running_status(syscall(SYS_gettid), pthread_fake_meta); |
| 151 | |
| 152 | // Release the TaskMeta at exit of pthread. |
| 153 | butil::thread_atexit([]() { |
| 154 | // Similar to TaskGroup::task_runner. |
| 155 | bool tracing; |
| 156 | { |
| 157 | BAIDU_SCOPED_LOCK(pthread_fake_meta->version_lock); |
| 158 | tracing = TaskTracer::set_end_status_unsafe(pthread_fake_meta); |
| 159 | // If resulting version is 0, |
| 160 | // change it to 1 to make bthread_t never be 0. |
| 161 | if (0 == ++*pthread_fake_meta->version_butex) { |
| 162 | ++*pthread_fake_meta->version_butex; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (tracing) { |
| 167 | // Wait for tracing completion. |
| 168 | get_task_control()->_task_tracer.WaitForTracing(pthread_fake_meta); |
| 169 | } |
| 170 | get_task_control()->_task_tracer.set_status( |
| 171 | TASK_STATUS_UNKNOWN, pthread_fake_meta); |
| 172 | |
| 173 | butil::return_resource(get_slot(pthread_fake_meta->tid)); |
| 174 | pthread_fake_meta = NULL; |
| 175 | }); |
| 176 | |
| 177 | return pthread_fake_meta->tid; |
| 178 | } |
| 179 | |
| 180 | void stack_trace(std::ostream& os, bthread_t tid) { |
| 181 | TaskControl* c = get_task_control(); |
nothing calls this directly
no test coverage detected