Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 34dd83f4 authored by Cheng Li's avatar Cheng Li
Browse files

Add trace points for HCI packets

Bug: 365778358
Bug: 369428038
Test: m <target>
Test: Build and run locally with perfetto tracing enabled while using bluetooth

Change-Id: Ic44db021cb0f7667fa156324485ca59b982d5484
parent 144eb1ec
Loading
Loading
Loading
Loading
+4 −1
Original line number Original line Diff line number Diff line
@@ -24,7 +24,10 @@ source_set("BluetoothHalSources") {
  ]
  ]


  configs += [ "//bt/system/gd:gd_defaults" ]
  configs += [ "//bt/system/gd:gd_defaults" ]
  deps = [ "//bt/system/gd:gd_default_deps" ]
  deps = [
    "//bt/flags:bluetooth_flags_c_lib",
    "//bt/system/gd:gd_default_deps"
  ]
}
}


source_set("BluetoothHalSources_hci_host") {
source_set("BluetoothHalSources_hci_host") {
+64 −0
Original line number Original line Diff line number Diff line
@@ -14,10 +14,16 @@
 * limitations under the License.
 * limitations under the License.
 */
 */


#define ATRACE_TAG ATRACE_TAG_APP

#include "hal/snoop_logger.h"
#include "hal/snoop_logger.h"


#include <arpa/inet.h>
#include <arpa/inet.h>
#include <bluetooth/log.h>
#include <bluetooth/log.h>
#include <com_android_bluetooth_flags.h>
#ifdef __ANDROID__
#include <cutils/trace.h>
#endif // __ANDROID__
#include <sys/stat.h>
#include <sys/stat.h>


#include <algorithm>
#include <algorithm>
@@ -28,6 +34,7 @@
#include "common/circular_buffer.h"
#include "common/circular_buffer.h"
#include "common/strings.h"
#include "common/strings.h"
#include "hal/snoop_logger_common.h"
#include "hal/snoop_logger_common.h"
#include "hci/hci_packets.h"
#include "module_dumper_flatbuffer.h"
#include "module_dumper_flatbuffer.h"
#include "os/files.h"
#include "os/files.h"
#include "os/parameter_provider.h"
#include "os/parameter_provider.h"
@@ -1123,6 +1130,13 @@ void SnoopLogger::Capture(const HciPacket& immutable_packet, Direction direction
  HciPacket mutable_packet(immutable_packet);
  HciPacket mutable_packet(immutable_packet);
  HciPacket& packet = mutable_packet;
  HciPacket& packet = mutable_packet;
  //////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////

  #ifdef __ANDROID__
  if (com::android::bluetooth::flags::snoop_logger_tracing()) {
    LogTracePoint(packet, direction, type);
  }
  #endif // __ANDROID__

  uint64_t timestamp_us = std::chrono::duration_cast<std::chrono::microseconds>(
  uint64_t timestamp_us = std::chrono::duration_cast<std::chrono::microseconds>(
                                  std::chrono::system_clock::now().time_since_epoch())
                                  std::chrono::system_clock::now().time_since_epoch())
                                  .count();
                                  .count();
@@ -1401,5 +1415,55 @@ const ModuleFactory SnoopLogger::Factory = ModuleFactory([]() {
                         IsBtSnoopLogPersisted());
                         IsBtSnoopLogPersisted());
});
});


#ifdef __ANDROID__
void SnoopLogger::LogTracePoint(const HciPacket& packet, Direction direction, PacketType type) {
  switch (type) {
    case PacketType::EVT: {
      uint8_t evt_code = packet[0];

      if (evt_code == static_cast<uint8_t>(hci::EventCode::LE_META_EVENT) ||
          evt_code == static_cast<uint8_t>(hci::EventCode::VENDOR_SPECIFIC)) {
        uint8_t subevt_code = packet[2];
        std::string message =
                fmt::format("BTSL:{}/{}/{}/{:02x}/{:02x}", static_cast<uint8_t>(type),
                            static_cast<uint8_t>(direction), packet.size(), evt_code, subevt_code);

        ATRACE_INSTANT_FOR_TRACK(LOG_TAG, message.c_str());
      } else {
        std::string message = fmt::format("BTSL:{}/{}/{}/{:02x}", static_cast<uint8_t>(type),
                                          static_cast<uint8_t>(direction), packet.size(), evt_code);

        ATRACE_INSTANT_FOR_TRACK(LOG_TAG, message.c_str());
      }
    } break;
    case PacketType::CMD: {
      uint16_t op_code = packet[0] | (packet[1] << 8);

      std::string message = fmt::format("BTSL:{}/{}/{}/{:04x}", static_cast<uint8_t>(type),
                                        static_cast<uint8_t>(direction), packet.size(), op_code);

      ATRACE_INSTANT_FOR_TRACK(LOG_TAG, message.c_str());
    } break;
    case PacketType::ACL: {
      uint16_t handle = (packet[0] | (packet[1] << 8)) & 0x0fff;
      uint8_t pb_flag = (packet[1] & 0x30) >> 4;

      std::string message = fmt::format("BTSL:{}/{}/{}/{:03x}/{}", static_cast<uint8_t>(type),
                                        static_cast<uint8_t>(direction), packet.size(), handle,
                                        pb_flag);

      ATRACE_INSTANT_FOR_TRACK(LOG_TAG, message.c_str());
    } break;
    case PacketType::ISO:
    case PacketType::SCO: {
      std::string message = fmt::format("BTSL:{}/{}/{}", static_cast<uint8_t>(type),
                                        static_cast<uint8_t>(direction), packet.size());

      ATRACE_INSTANT_FOR_TRACK(LOG_TAG, message.c_str());
    } break;
  }
}
#endif // __ANDROID__

}  // namespace hal
}  // namespace hal
}  // namespace bluetooth
}  // namespace bluetooth
+4 −0
Original line number Original line Diff line number Diff line
@@ -319,6 +319,10 @@ protected:


  std::unique_ptr<SnoopLoggerSocketThread> snoop_logger_socket_thread_;
  std::unique_ptr<SnoopLoggerSocketThread> snoop_logger_socket_thread_;


  #ifdef __ANDROID__
  void LogTracePoint(const HciPacket& packet, Direction direction, PacketType type);
  #endif // __ANDROID__

private:
private:
  static std::string btsnoop_mode_;
  static std::string btsnoop_mode_;
  std::string snoop_log_path_;
  std::string snoop_log_path_;
+2 −0
Original line number Original line Diff line number Diff line
@@ -1773,6 +1773,7 @@ cc_test {
    shared_libs: [
    shared_libs: [
        "libaconfig_storage_read_api_cc",
        "libaconfig_storage_read_api_cc",
        "libcrypto",
        "libcrypto",
        "libcutils",
        "server_configurable_flags",
        "server_configurable_flags",
    ],
    ],
    sanitize: {
    sanitize: {
@@ -2355,6 +2356,7 @@ cc_test {
    shared_libs: [
    shared_libs: [
        "libaconfig_storage_read_api_cc",
        "libaconfig_storage_read_api_cc",
        "libcrypto",
        "libcrypto",
        "libcutils",
        "server_configurable_flags",
        "server_configurable_flags",
    ],
    ],
    sanitize: {
    sanitize: {