OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" | 5 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 | 9 |
10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 if (backtrace == backtrace_end) { | 193 if (backtrace == backtrace_end) { |
194 break; | 194 break; |
195 } | 195 } |
196 *backtrace++ = | 196 *backtrace++ = |
197 StackFrame::FromTraceEventName(stack_frame.trace_event_name); | 197 StackFrame::FromTraceEventName(stack_frame.trace_event_name); |
198 } | 198 } |
199 break; | 199 break; |
200 } | 200 } |
201 case CaptureMode::NATIVE_STACK: | 201 case CaptureMode::NATIVE_STACK: |
202 { | 202 { |
203 // Backtrace contract requires us to return bottom frames, i.e. | 203 // Backtrace contract requires us to return bottom frames, i.e. |
204 // from main() and up. Stack unwinding produces top frames, i.e. | 204 // from main() and up. Stack unwinding produces top frames, i.e. |
205 // from this point and up until main(). We request many frames to | 205 // from this point and up until main(). We intentionally request |
206 // make sure we reach main(), and then copy bottom portion of them. | 206 // kMaxFrameCount + 1 frames, so that we know if there are more frames |
| 207 // than our backtrace capacity. |
207 #if !defined(OS_NACL) // We don't build base/debug/stack_trace.cc for NaCl. | 208 #if !defined(OS_NACL) // We don't build base/debug/stack_trace.cc for NaCl. |
208 #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) | 209 #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
209 const void* frames[128]; | 210 const void* frames[Backtrace::kMaxFrameCount + 1]; |
210 static_assert(arraysize(frames) >= Backtrace::kMaxFrameCount, | 211 static_assert(arraysize(frames) >= Backtrace::kMaxFrameCount, |
211 "not requesting enough frames to fill Backtrace"); | 212 "not requesting enough frames to fill Backtrace"); |
212 size_t frame_count = debug::TraceStackFramePointers( | 213 size_t frame_count = debug::TraceStackFramePointers( |
213 frames, arraysize(frames), | 214 frames, arraysize(frames), |
214 1 /* exclude this function from the trace */); | 215 1 /* exclude this function from the trace */); |
215 #else // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) | 216 #else // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
216 // Fall-back to capturing the stack with base::debug::StackTrace, | 217 // Fall-back to capturing the stack with base::debug::StackTrace, |
217 // which is likely slower, but more reliable. | 218 // which is likely slower, but more reliable. |
218 base::debug::StackTrace stack_trace(Backtrace::kMaxFrameCount); | 219 base::debug::StackTrace stack_trace(Backtrace::kMaxFrameCount + 1); |
219 size_t frame_count = 0u; | 220 size_t frame_count = 0u; |
220 const void* const* frames = stack_trace.Addresses(&frame_count); | 221 const void* const* frames = stack_trace.Addresses(&frame_count); |
221 #endif // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) | 222 #endif // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
222 | 223 |
223 // Copy frames backwards | 224 // If there are too many frames, keep the ones furthest from main(). |
224 size_t backtrace_capacity = backtrace_end - backtrace; | 225 size_t backtrace_capacity = backtrace_end - backtrace; |
225 int32_t top_frame_index = (backtrace_capacity >= frame_count) | 226 int32_t starting_frame_index = frame_count; |
226 ? 0 | 227 if (frame_count > backtrace_capacity) { |
227 : frame_count - backtrace_capacity; | 228 starting_frame_index = backtrace_capacity - 1; |
228 for (int32_t i = frame_count - 1; i >= top_frame_index; --i) { | 229 *backtrace++ = StackFrame::FromTraceEventName("<truncated>"); |
229 const void* frame = frames[i]; | 230 } |
230 *backtrace++ = StackFrame::FromProgramCounter(frame); | 231 for (int32_t i = starting_frame_index - 1; i >= 0; --i) { |
231 } | 232 const void* frame = frames[i]; |
| 233 *backtrace++ = StackFrame::FromProgramCounter(frame); |
| 234 } |
232 #endif // !defined(OS_NACL) | 235 #endif // !defined(OS_NACL) |
233 break; | 236 break; |
234 } | 237 } |
235 } | 238 } |
236 | 239 |
237 ctx->backtrace.frame_count = backtrace - std::begin(ctx->backtrace.frames); | 240 ctx->backtrace.frame_count = backtrace - std::begin(ctx->backtrace.frames); |
238 | 241 |
239 // TODO(ssid): Fix crbug.com/594803 to add file name as 3rd dimension | 242 // TODO(ssid): Fix crbug.com/594803 to add file name as 3rd dimension |
240 // (component name) in the heap profiler and not piggy back on the type name. | 243 // (component name) in the heap profiler and not piggy back on the type name. |
241 if (!task_contexts_.empty()) { | 244 if (!task_contexts_.empty()) { |
242 ctx->type_name = task_contexts_.back(); | 245 ctx->type_name = task_contexts_.back(); |
243 } else if (!pseudo_stack_.empty()) { | 246 } else if (!pseudo_stack_.empty()) { |
244 // If task context was unavailable, then the category names are taken from | 247 // If task context was unavailable, then the category names are taken from |
245 // trace events. | 248 // trace events. |
246 ctx->type_name = pseudo_stack_.back().trace_event_category; | 249 ctx->type_name = pseudo_stack_.back().trace_event_category; |
247 } else { | 250 } else { |
248 ctx->type_name = nullptr; | 251 ctx->type_name = nullptr; |
249 } | 252 } |
250 | 253 |
251 return true; | 254 return true; |
252 } | 255 } |
253 | 256 |
254 } // namespace trace_event | 257 } // namespace trace_event |
255 } // namespace base | 258 } // namespace base |
OLD | NEW |