interned_args_helper.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/trace_event/interned_args_helper.h"
  5. #include "third_party/perfetto/include/perfetto/tracing/track_event_interned_data_index.h"
  6. #include "third_party/perfetto/protos/perfetto/trace/interned_data/interned_data.pbzero.h"
  7. #include "third_party/perfetto/protos/perfetto/trace/profiling/profile_common.pbzero.h"
  8. #include "third_party/perfetto/protos/perfetto/trace/track_event/log_message.pbzero.h"
  9. #include "third_party/perfetto/protos/perfetto/trace/track_event/source_location.pbzero.h"
  10. #include "third_party/perfetto/protos/perfetto/trace/track_event/task_execution.pbzero.h"
  11. namespace base {
  12. namespace trace_event {
  13. // static
  14. void InternedSourceLocation::Add(
  15. perfetto::protos::pbzero::InternedData* interned_data,
  16. size_t iid,
  17. const TraceSourceLocation& location) {
  18. auto* msg = interned_data->add_source_locations();
  19. msg->set_iid(iid);
  20. if (location.file_name != nullptr)
  21. msg->set_file_name(location.file_name);
  22. if (location.function_name != nullptr)
  23. msg->set_function_name(location.function_name);
  24. // TODO(ssid): Add line number once it is allowed in internal proto.
  25. // TODO(ssid): Add program counter to the proto fields when
  26. // !BUILDFLAG(ENABLE_LOCATION_SOURCE).
  27. // TODO(http://crbug.com760702) remove file name and just pass the program
  28. // counter to the heap profiler macro.
  29. // TODO(ssid): Consider writing the program counter of the current task
  30. // (from the callback function pointer) instead of location that posted the
  31. // task.
  32. }
  33. // static
  34. void InternedLogMessage::Add(
  35. perfetto::protos::pbzero::InternedData* interned_data,
  36. size_t iid,
  37. const std::string& log_message) {
  38. auto* msg = interned_data->add_log_message_body();
  39. msg->set_iid(iid);
  40. msg->set_body(log_message);
  41. }
  42. // static
  43. void InternedBuildId::Add(perfetto::protos::pbzero::InternedData* interned_data,
  44. size_t iid,
  45. const std::string& build_id) {
  46. auto* msg = interned_data->add_build_ids();
  47. msg->set_iid(iid);
  48. msg->set_str(build_id);
  49. }
  50. // static
  51. void InternedMappingPath::Add(
  52. perfetto::protos::pbzero::InternedData* interned_data,
  53. size_t iid,
  54. const std::string& mapping_path) {
  55. auto* msg = interned_data->add_mapping_paths();
  56. msg->set_iid(iid);
  57. msg->set_str(mapping_path);
  58. }
  59. // static
  60. size_t InternedMapping::Get(perfetto::EventContext* ctx,
  61. const base::ModuleCache::Module* module) {
  62. auto* index_for_field = GetOrCreateIndexForField(ctx->GetIncrementalState());
  63. size_t iid;
  64. if (index_for_field->index_.LookUpOrInsert(&iid, module)) {
  65. return iid;
  66. }
  67. InternedMapping::Add(ctx, iid, module);
  68. return iid;
  69. }
  70. // static
  71. void InternedMapping::Add(perfetto::EventContext* ctx,
  72. size_t iid,
  73. const base::ModuleCache::Module* module) {
  74. const auto build_id = InternedBuildId::Get(
  75. ctx, base::TransformModuleIDToBreakpadFormat(module->GetId()));
  76. const auto path_id =
  77. InternedMappingPath::Get(ctx, module->GetDebugBasename().MaybeAsASCII());
  78. auto* msg =
  79. ctx->GetIncrementalState()->serialized_interned_data->add_mappings();
  80. msg->set_iid(iid);
  81. msg->set_build_id(build_id);
  82. msg->add_path_string_ids(path_id);
  83. }
  84. // static
  85. absl::optional<size_t> InternedUnsymbolizedSourceLocation::Get(
  86. perfetto::EventContext* ctx,
  87. uintptr_t address) {
  88. auto* index_for_field = GetOrCreateIndexForField(ctx->GetIncrementalState());
  89. const base::ModuleCache::Module* module =
  90. index_for_field->module_cache_.GetModuleForAddress(address);
  91. if (!module) {
  92. return absl::nullopt;
  93. }
  94. size_t iid;
  95. if (index_for_field->index_.LookUpOrInsert(&iid, address)) {
  96. return iid;
  97. }
  98. const auto mapping_id = InternedMapping::Get(ctx, module);
  99. const uintptr_t rel_pc = address - module->GetBaseAddress();
  100. InternedUnsymbolizedSourceLocation::Add(
  101. ctx->GetIncrementalState()->serialized_interned_data.get(), iid,
  102. base::trace_event::UnsymbolizedSourceLocation(mapping_id, rel_pc));
  103. return iid;
  104. }
  105. // static
  106. void InternedUnsymbolizedSourceLocation::Add(
  107. perfetto::protos::pbzero::InternedData* interned_data,
  108. size_t iid,
  109. const UnsymbolizedSourceLocation& location) {
  110. auto* msg = interned_data->add_unsymbolized_source_locations();
  111. msg->set_iid(iid);
  112. msg->set_mapping_id(location.mapping_id);
  113. msg->set_rel_pc(location.rel_pc);
  114. }
  115. } // namespace trace_event
  116. } // namespace base