| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/memory_tracing_observer.h" | 5 #include "base/trace_event/memory_tracing_observer.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" |
| 7 #include "base/trace_event/memory_dump_manager.h" | 8 #include "base/trace_event/memory_dump_manager.h" |
| 8 #include "base/trace_event/trace_event_argument.h" | 9 #include "base/trace_event/trace_event_argument.h" |
| 9 | 10 |
| 10 namespace base { | 11 namespace base { |
| 11 namespace trace_event { | 12 namespace trace_event { |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 const int kTraceEventNumArgs = 1; | 16 const int kTraceEventNumArgs = 1; |
| 16 const char* kTraceEventArgNames[] = {"dumps"}; | 17 const char* kTraceEventArgNames[] = {"dumps"}; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 | 50 |
| 50 // Initialize the TraceLog for the current thread. This is to avoids that the | 51 // Initialize the TraceLog for the current thread. This is to avoids that the |
| 51 // TraceLog memory dump provider is registered lazily during the MDM Enable() | 52 // TraceLog memory dump provider is registered lazily during the MDM Enable() |
| 52 TraceLog::GetInstance()->InitializeThreadLocalEventBufferIfSupported(); | 53 TraceLog::GetInstance()->InitializeThreadLocalEventBufferIfSupported(); |
| 53 | 54 |
| 54 const TraceConfig& trace_config = | 55 const TraceConfig& trace_config = |
| 55 TraceLog::GetInstance()->GetCurrentTraceConfig(); | 56 TraceLog::GetInstance()->GetCurrentTraceConfig(); |
| 56 const TraceConfig::MemoryDumpConfig& memory_dump_config = | 57 const TraceConfig::MemoryDumpConfig& memory_dump_config = |
| 57 trace_config.memory_dump_config(); | 58 trace_config.memory_dump_config(); |
| 58 | 59 |
| 60 memory_dump_config_ = |
| 61 MakeUnique<TraceConfig::MemoryDumpConfig>(memory_dump_config); |
| 62 |
| 59 memory_dump_manager_->Enable(memory_dump_config); | 63 memory_dump_manager_->Enable(memory_dump_config); |
| 60 } | 64 } |
| 61 | 65 |
| 62 void MemoryTracingObserver::OnTraceLogDisabled() { | 66 void MemoryTracingObserver::OnTraceLogDisabled() { |
| 63 memory_dump_manager_->Disable(); | 67 memory_dump_manager_->Disable(); |
| 68 memory_dump_config_.reset(); |
| 64 } | 69 } |
| 65 | 70 |
| 66 bool MemoryTracingObserver::AddDumpToTraceIfEnabled( | 71 bool MemoryTracingObserver::AddDumpToTraceIfEnabled( |
| 67 const MemoryDumpRequestArgs* req_args, | 72 const MemoryDumpRequestArgs* req_args, |
| 68 const ProcessId pid, | 73 const ProcessId pid, |
| 69 const ProcessMemoryDump* process_memory_dump) { | 74 const ProcessMemoryDump* process_memory_dump) { |
| 70 // If tracing has been disabled early out to avoid the cost of serializing the | 75 // If tracing has been disabled early out to avoid the cost of serializing the |
| 71 // dump then ignoring the result. | 76 // dump then ignoring the result. |
| 72 if (!IsMemoryInfraTracingEnabled()) | 77 if (!IsMemoryInfraTracingEnabled()) |
| 73 return false; | 78 return false; |
| 79 // If the dump mode is too detailed don't add to trace to avoid accidentally |
| 80 // including PII. |
| 81 if (!IsDumpModeAllowed(req_args->level_of_detail)) |
| 82 return false; |
| 74 | 83 |
| 75 CHECK_NE(MemoryDumpType::SUMMARY_ONLY, req_args->dump_type); | 84 CHECK_NE(MemoryDumpType::SUMMARY_ONLY, req_args->dump_type); |
| 76 | 85 |
| 77 const uint64_t dump_guid = req_args->dump_guid; | 86 const uint64_t dump_guid = req_args->dump_guid; |
| 78 | 87 |
| 79 std::unique_ptr<TracedValue> traced_value(new TracedValue); | 88 std::unique_ptr<TracedValue> traced_value(new TracedValue); |
| 80 process_memory_dump->AsValueInto(traced_value.get()); | 89 process_memory_dump->AsValueInto(traced_value.get()); |
| 81 traced_value->SetString("level_of_detail", MemoryDumpLevelOfDetailToString( | 90 traced_value->SetString("level_of_detail", MemoryDumpLevelOfDetailToString( |
| 82 req_args->level_of_detail)); | 91 req_args->level_of_detail)); |
| 83 const char* const event_name = MemoryDumpTypeToString(req_args->dump_type); | 92 const char* const event_name = MemoryDumpTypeToString(req_args->dump_type); |
| 84 | 93 |
| 85 std::unique_ptr<ConvertableToTraceFormat> event_value( | 94 std::unique_ptr<ConvertableToTraceFormat> event_value( |
| 86 std::move(traced_value)); | 95 std::move(traced_value)); |
| 87 TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_PROCESS_ID( | 96 TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_PROCESS_ID( |
| 88 TRACE_EVENT_PHASE_MEMORY_DUMP, | 97 TRACE_EVENT_PHASE_MEMORY_DUMP, |
| 89 TraceLog::GetCategoryGroupEnabled(MemoryDumpManager::kTraceCategory), | 98 TraceLog::GetCategoryGroupEnabled(MemoryDumpManager::kTraceCategory), |
| 90 event_name, trace_event_internal::kGlobalScope, dump_guid, pid, | 99 event_name, trace_event_internal::kGlobalScope, dump_guid, pid, |
| 91 kTraceEventNumArgs, kTraceEventArgNames, kTraceEventArgTypes, | 100 kTraceEventNumArgs, kTraceEventArgNames, kTraceEventArgTypes, |
| 92 nullptr /* arg_values */, &event_value, TRACE_EVENT_FLAG_HAS_ID); | 101 nullptr /* arg_values */, &event_value, TRACE_EVENT_FLAG_HAS_ID); |
| 93 | 102 |
| 94 return true; | 103 return true; |
| 95 } | 104 } |
| 96 | 105 |
| 106 bool MemoryTracingObserver::IsDumpModeAllowed( |
| 107 MemoryDumpLevelOfDetail dump_mode) const { |
| 108 if (!memory_dump_config_) |
| 109 return false; |
| 110 return memory_dump_config_->allowed_dump_modes.count(dump_mode) != 0; |
| 111 } |
| 112 |
| 97 } // namespace trace_event | 113 } // namespace trace_event |
| 98 } // namespace base | 114 } // namespace base |
| OLD | NEW |