Skip to content

Commit 69579bb

Browse files
author
gaohaoyuan
committed
add code format
1 parent db8dfe3 commit 69579bb

18 files changed

+655
-572
lines changed

backtrace/include/Config.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
#include <stdint.h>
44
#include <cstddef>
55

6-
constexpr uint64_t BACKTRACE = 0x1; // 记录堆栈
7-
constexpr uint64_t TRACK_ALLOCS = 0x2; // 记录内存申请动作
8-
constexpr uint64_t BACKTRACE_SPECIFIC_SIZES = 0x4; // 记录特定大小的内存申请
9-
constexpr uint64_t RECORD_MEMORY_PEAK = 0x8; // 记录内存峰值
10-
constexpr uint64_t DUMP_ON_SINGAL = 0x80; // 记录内存峰值
6+
constexpr uint64_t BACKTRACE = 0x1; // 记录堆栈
7+
constexpr uint64_t TRACK_ALLOCS = 0x2; // 记录内存申请动作
8+
constexpr uint64_t BACKTRACE_SPECIFIC_SIZES = 0x4; // 记录特定大小的内存申请
9+
constexpr uint64_t RECORD_MEMORY_PEAK = 0x8; // 记录内存峰值
10+
constexpr uint64_t DUMP_ON_SINGAL = 0x80; // 记录内存峰值
1111

1212
class Config {
1313
public:
@@ -27,7 +27,6 @@ class Config {
2727
size_t backtrace_dump_peak_val() const { return backtrace_dump_peak_val_; }
2828

2929
private:
30-
3130
int backtrace_dump_signal_ = 0;
3231

3332
size_t backtrace_frames_ = 0;

backtrace/include/PointerData.h

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,58 @@
11
#pragma once
22

3-
#include <stdint.h>
43
#include <fcntl.h>
4+
#include <stdint.h>
55

66
#include <cstdint>
77
#include <cstdio>
88
#include <cstdlib>
99
#include <memory>
10-
#include <vector>
11-
#include <unordered_map>
1210
#include <mutex>
11+
#include <unordered_map>
12+
#include <vector>
1313

1414
#include <bionic/macros.h>
1515
#include <unwindstack/Unwinder.h>
1616

1717
#include "Config.h"
1818

19-
enum MemType{ HOST, MMAP, DMA };
19+
enum MemType { HOST, MMAP, DMA };
2020

2121
struct FrameKeyType {
22-
size_t num_frames;
23-
uintptr_t* frames;
24-
25-
bool operator==(const FrameKeyType& comp) const {
26-
if (num_frames != comp.num_frames) return false;
27-
for (size_t i = 0; i < num_frames; i++) {
28-
if (frames[i] != comp.frames[i]) {
29-
return false;
30-
}
22+
size_t num_frames;
23+
uintptr_t* frames;
24+
25+
bool operator==(const FrameKeyType& comp) const {
26+
if (num_frames != comp.num_frames)
27+
return false;
28+
for (size_t i = 0; i < num_frames; i++) {
29+
if (frames[i] != comp.frames[i]) {
30+
return false;
31+
}
32+
}
33+
return true;
3134
}
32-
return true;
33-
}
3435
};
3536

3637
// 新增 hash 算法
3738
namespace std {
38-
template <> struct hash<FrameKeyType> {
39-
std::size_t operator()(const FrameKeyType& key) const {
40-
std::size_t cur_hash = key.frames[0];
41-
// Limit the number of frames to speed up hashing.
42-
size_t max_frames = (key.num_frames > 5) ? 5 : key.num_frames;
43-
for (size_t i = 1; i < max_frames; i++) {
44-
cur_hash ^= key.frames[i];
39+
template <>
40+
struct hash<FrameKeyType> {
41+
std::size_t operator()(const FrameKeyType& key) const {
42+
std::size_t cur_hash = key.frames[0];
43+
// Limit the number of frames to speed up hashing.
44+
size_t max_frames = (key.num_frames > 5) ? 5 : key.num_frames;
45+
for (size_t i = 1; i < max_frames; i++) {
46+
cur_hash ^= key.frames[i];
47+
}
48+
return cur_hash;
4549
}
46-
return cur_hash;
47-
}
4850
};
4951
}; // namespace std
5052

5153
struct FrameInfoType {
52-
size_t references = 0;
53-
std::vector<uintptr_t> frames;
54+
size_t references = 0;
55+
std::vector<uintptr_t> frames;
5456
};
5557

5658
// 新增 timeval 比较函数
@@ -62,64 +64,67 @@ inline bool operator<(const timeval& lhs, const timeval& rhs) {
6264
}
6365

6466
struct PointerInfoType {
65-
size_t size;
66-
size_t hash_index;
67-
MemType mem_type;
68-
timeval alloc_time;
69-
size_t RealSize() const { return size & ~(1U << 31); }
70-
static size_t MaxSize() { return (1U << 31) - 1; }
67+
size_t size;
68+
size_t hash_index;
69+
MemType mem_type;
70+
timeval alloc_time;
71+
size_t RealSize() const { return size & ~(1U << 31); }
72+
static size_t MaxSize() { return (1U << 31) - 1; }
7173
};
7274

7375
struct ListInfoType {
74-
uintptr_t pointer;
75-
size_t num_allocations;
76-
size_t size;
77-
MemType mem_type;
78-
FrameInfoType* frame_info;
79-
std::shared_ptr<std::vector<unwindstack::FrameData>> backtrace_info;
80-
timeval alloc_time;
76+
uintptr_t pointer;
77+
size_t num_allocations;
78+
size_t size;
79+
MemType mem_type;
80+
FrameInfoType* frame_info;
81+
std::shared_ptr<std::vector<unwindstack::FrameData>> backtrace_info;
82+
timeval alloc_time;
8183
};
8284
using Pred = std::function<bool(const ListInfoType&, const ListInfoType&)>;
8385

8486
class PointerData {
8587
public:
86-
PointerData() = default;
87-
virtual ~PointerData() = default;
88+
PointerData() = default;
89+
virtual ~PointerData() = default;
8890

89-
bool Initialize(const Config& config);
91+
bool Initialize(const Config& config);
9092

91-
size_t AddBacktrace(size_t num_frames, size_t size_bytes);
92-
void RemoveBacktrace(size_t hash_index);
93+
size_t AddBacktrace(size_t num_frames, size_t size_bytes);
94+
void RemoveBacktrace(size_t hash_index);
9395

94-
void Add(uintptr_t pointer, size_t size, MemType type);
95-
void AddHost(const void* ptr, size_t pointer_size, MemType type = HOST);
96-
void AddDMA(const uint32_t ptr, size_t pointer_size, MemType type = DMA);
97-
void Remove(uintptr_t pointer, bool is_dma);
98-
void RemoveHost(const void* ptr);
99-
void RemoveDMA(const uint32_t ptr);
96+
void Add(uintptr_t pointer, size_t size, MemType type);
97+
void AddHost(const void* ptr, size_t pointer_size, MemType type = HOST);
98+
void AddDMA(const uint32_t ptr, size_t pointer_size, MemType type = DMA);
99+
void Remove(uintptr_t pointer, bool is_dma);
100+
void RemoveHost(const void* ptr);
101+
void RemoveDMA(const uint32_t ptr);
100102

101-
void DumpLiveToFile(int fd);
102-
void DumpPeakInfo();
103+
void DumpLiveToFile(int fd);
104+
void DumpPeakInfo();
103105

104106
private:
105-
inline uintptr_t ManglePointer(uintptr_t pointer) { return pointer ^ UINTPTR_MAX; }
106-
inline uintptr_t DemanglePointer(uintptr_t pointer) { return pointer ^ UINTPTR_MAX; }
107+
inline uintptr_t ManglePointer(uintptr_t pointer) { return pointer ^ UINTPTR_MAX; }
108+
inline uintptr_t DemanglePointer(uintptr_t pointer) {
109+
return pointer ^ UINTPTR_MAX;
110+
}
107111

108-
void GetList(std::vector<ListInfoType>* list, bool only_with_backtrace, Pred pred);
109-
void GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace);
112+
void GetList(std::vector<ListInfoType>* list, bool only_with_backtrace, Pred pred);
113+
void GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace);
110114

111-
std::mutex pointer_mutex_;
112-
std::unordered_map<uintptr_t, PointerInfoType> pointers_;
115+
std::mutex pointer_mutex_;
116+
std::unordered_map<uintptr_t, PointerInfoType> pointers_;
113117

114-
std::mutex frame_mutex_;
115-
std::unordered_map<FrameKeyType, size_t> key_to_index_;
116-
std::unordered_map<size_t, FrameInfoType> frames_;
117-
std::unordered_map<size_t, std::shared_ptr<std::vector<unwindstack::FrameData>>> backtraces_info_;
118-
size_t cur_hash_index_;
118+
std::mutex frame_mutex_;
119+
std::unordered_map<FrameKeyType, size_t> key_to_index_;
120+
std::unordered_map<size_t, FrameInfoType> frames_;
121+
std::unordered_map<size_t, std::shared_ptr<std::vector<unwindstack::FrameData>>>
122+
backtraces_info_;
123+
size_t cur_hash_index_;
119124

120-
size_t current_used, current_host, current_dma;
121-
size_t peak_tot, peak_host, peak_dma;
122-
std::vector<ListInfoType> peak_list;
125+
size_t current_used, current_host, current_dma;
126+
size_t peak_tot, peak_host, peak_dma;
127+
std::vector<ListInfoType> peak_list;
123128

124-
BIONIC_DISALLOW_COPY_AND_ASSIGN(PointerData);
129+
BIONIC_DISALLOW_COPY_AND_ASSIGN(PointerData);
125130
};

backtrace/include/UnwindBacktrace.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@
3333

3434
#include <unwindstack/Unwinder.h>
3535

36-
unwindstack::ErrorCode Unwind(std::vector<uintptr_t>* frames, std::vector<unwindstack::FrameData>* info, size_t max_frames);
36+
unwindstack::ErrorCode Unwind(
37+
std::vector<uintptr_t>* frames, std::vector<unwindstack::FrameData>* info,
38+
size_t max_frames);

backtrace/include/debug_disable.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ bool DebugCallsDisabled();
1515
void DebugDisableSet(bool disable);
1616

1717
class ScopedDisableDebugCalls {
18-
public:
19-
ScopedDisableDebugCalls() : disabled_(DebugCallsDisabled()) {
20-
if (!disabled_) {
21-
DebugDisableSet(true);
18+
public:
19+
ScopedDisableDebugCalls() : disabled_(DebugCallsDisabled()) {
20+
if (!disabled_) {
21+
DebugDisableSet(true);
22+
}
2223
}
23-
}
24-
~ScopedDisableDebugCalls() {
25-
if (!disabled_) {
26-
DebugDisableSet(false);
24+
~ScopedDisableDebugCalls() {
25+
if (!disabled_) {
26+
DebugDisableSet(false);
27+
}
2728
}
28-
}
2929

30-
private:
31-
bool disabled_;
30+
private:
31+
bool disabled_;
3232

33-
BIONIC_DISALLOW_COPY_AND_ASSIGN(ScopedDisableDebugCalls);
33+
BIONIC_DISALLOW_COPY_AND_ASSIGN(ScopedDisableDebugCalls);
3434
};

backtrace/include/memory_hook.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ extern void* (*m_sys_memalign)(size_t, size_t);
1010
extern int (*m_sys_posix_memalign)(void**, size_t, size_t);
1111
extern int (*m_sys_ioctl)(int fd, unsigned int request, ...);
1212
extern int (*m_sys_close)(int fd);
13-
extern void* (*m_sys_mmap)(void* addr, size_t size, int prot, int flags, int fd, off_t offset);
13+
extern void* (*m_sys_mmap)(
14+
void* addr, size_t size, int prot, int flags, int fd, off_t offset);
1415
extern int (*m_sys_munmap)(void* addr, size_t size);

backtrace/src/Config.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1+
#include <bionic/reserved_signals.h>
12
#include <cassert>
23
#include <cstdio>
34
#include <cstdlib>
45
#include <cstring>
5-
#include <bionic/reserved_signals.h>
66

77
#include "Config.h"
88

9-
109
static constexpr size_t DEFAULT_BACKTRACE_FRAMES = 128;
11-
static constexpr const char DEFAULT_BACKTRACE_DUMP_PREFIX[] = "/data/local/tmp/trace/backtrace_heap";
10+
static constexpr const char DEFAULT_BACKTRACE_DUMP_PREFIX[] =
11+
"/data/local/tmp/trace/backtrace_heap";
1212

1313
static bool ParseValue(const char* value, size_t* parsed_value) {
1414
*parsed_value = 0;
1515
if (value == nullptr) {
16-
return false;
16+
return false;
1717
}
1818
*parsed_value = static_cast<size_t>(atol(value)) * 1024 * 1024;
1919
return true;
@@ -44,7 +44,7 @@ bool Config::Init() {
4444

4545
// 通过信号插入 check point
4646
options_ |= DUMP_ON_SINGAL;
47-
backtrace_dump_signal_ = BIONIC_SIGNAL_BACKTRACE; // BIONIC_SIGNAL_BACKTRACE: 33
47+
backtrace_dump_signal_ = BIONIC_SIGNAL_BACKTRACE; // BIONIC_SIGNAL_BACKTRACE: 33
4848

4949
return true;
5050
}

backtrace/src/DebugData.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ bool DebugData::Initialize(void* storage) {
77

88
pointer.reset(new (storage) PointerData());
99
if (!pointer->Initialize(config_)) {
10-
return false;
10+
return false;
1111
}
1212

1313
return true;

0 commit comments

Comments
 (0)