-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathFLEXSystemLogViewController.m
More file actions
311 lines (246 loc) · 10.8 KB
/
FLEXSystemLogViewController.m
File metadata and controls
311 lines (246 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//
// FLEXSystemLogViewController.m
// FLEX
//
// Created by Ryan Olson on 1/19/15.
// Copyright (c) 2020 FLEX Team. All rights reserved.
//
#import "FLEXSystemLogViewController.h"
#import "FLEXASLLogController.h"
#import "FLEXOSLogController.h"
#import "FLEXSystemLogCell.h"
#import "FLEXMutableListSection.h"
#import "FLEXUtility.h"
#import "FLEXColor.h"
#import "FLEXResources.h"
#import "UIBarButtonItem+FLEX.h"
#import "NSUserDefaults+FLEX.h"
#import "flex_fishhook.h"
#import <dlfcn.h>
@interface FLEXSystemLogViewController ()
@property (nonatomic, readonly) FLEXMutableListSection<FLEXSystemLogMessage *> *logMessages;
@property (nonatomic, readonly) id<FLEXLogController> logController;
@property (nonatomic) BOOL isPaused;
@property (nonatomic, strong) UIBarButtonItem *pauseButton;
@end
static void (*MSHookFunction)(void *symbol, void *replace, void **result);
static BOOL FLEXDidHookNSLog = NO;
static BOOL FLEXNSLogHookWorks = NO;
BOOL (*os_log_shim_enabled)(void *addr) = nil;
BOOL (*orig_os_log_shim_enabled)(void *addr) = nil;
static BOOL my_os_log_shim_enabled(void *addr) {
return NO;
}
@implementation FLEXSystemLogViewController
#pragma mark - Initialization
+ (void)load {
// User must opt-into disabling os_log
if (!NSUserDefaults.standardUserDefaults.flex_disableOSLog) {
return;
}
// Thanks to @Ram4096 on GitHub for telling me that
// os_log is conditionally enabled by the SDK version
void *addr = __builtin_return_address(0);
void *libsystem_trace = dlopen("/usr/lib/system/libsystem_trace.dylib", RTLD_LAZY);
os_log_shim_enabled = dlsym(libsystem_trace, "os_log_shim_enabled");
if (!os_log_shim_enabled) {
return;
}
FLEXDidHookNSLog = flex_rebind_symbols((struct rebinding[1]) {{
"os_log_shim_enabled",
(void *)my_os_log_shim_enabled,
(void **)&orig_os_log_shim_enabled
}}, 1) == 0;
if (FLEXDidHookNSLog && orig_os_log_shim_enabled != nil) {
// Check if our rebinding worked
FLEXNSLogHookWorks = my_os_log_shim_enabled(addr) == NO;
}
// So, just because we rebind the lazily loaded symbol for
// this function doesn't mean it's even going to be used.
// While it seems to be sufficient for the simulator, for
// whatever reason it is not sufficient on-device. We need
// to actually hook the function with something like Substrate.
// Check if we have substrate, and if so use that instead
void *handle = dlopen("/usr/lib/libsubstrate.dylib", RTLD_LAZY);
if (handle) {
MSHookFunction = dlsym(handle, "MSHookFunction");
if (MSHookFunction) {
// Set the hook and check if it worked
void *unused;
MSHookFunction(os_log_shim_enabled, my_os_log_shim_enabled, &unused);
FLEXNSLogHookWorks = os_log_shim_enabled(addr) == NO;
}
}
}
- (id)init {
return [super initWithStyle:UITableViewStylePlain];
}
#pragma mark - Overrides
- (void)viewDidLoad {
[super viewDidLoad];
self.showsSearchBar = YES;
self.pinSearchBar = YES;
weakify(self)
id logHandler = ^(NSArray<FLEXSystemLogMessage *> *newMessages) { strongify(self)
[self handleUpdateWithNewMessages:newMessages];
};
if (FLEXOSLogAvailable() && !FLEXNSLogHookWorks) {
_logController = [FLEXOSLogController withUpdateHandler:logHandler];
} else {
_logController = [FLEXASLLogController withUpdateHandler:logHandler];
}
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.title = @"Waiting for Logs...";
// Toolbar buttons //
UIBarButtonItem *scrollDown = [UIBarButtonItem
flex_itemWithImage:FLEXResources.scrollToBottomIcon
target:self
action:@selector(scrollToLastRow)
];
self.pauseButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage systemImageNamed:@"pause.fill"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(togglePause)
];
UIBarButtonItem *settings = [UIBarButtonItem
flex_itemWithImage:FLEXResources.gearIcon
target:self
action:@selector(showLogSettings)
];
[self addToolbarItems:@[scrollDown, self.pauseButton, settings]];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.logController startMonitoring];
}
- (NSArray<FLEXTableViewSection *> *)makeSections { weakify(self)
_logMessages = [FLEXMutableListSection list:@[]
cellConfiguration:^(FLEXSystemLogCell *cell, FLEXSystemLogMessage *message, NSInteger row) {
strongify(self)
cell.logMessage = message;
cell.highlightedText = self.filterText;
if (row % 2 == 0) {
cell.backgroundColor = FLEXColor.primaryBackgroundColor;
} else {
cell.backgroundColor = FLEXColor.secondaryBackgroundColor;
}
} filterMatcher:^BOOL(NSString *filterText, FLEXSystemLogMessage *message) {
NSString *displayedText = [FLEXSystemLogCell displayedTextForLogMessage:message];
return [displayedText localizedCaseInsensitiveContainsString:filterText];
}
];
self.logMessages.cellRegistrationMapping = @{
kFLEXSystemLogCellIdentifier : [FLEXSystemLogCell class]
};
return @[self.logMessages];
}
- (NSArray<FLEXTableViewSection *> *)nonemptySections {
return @[self.logMessages];
}
#pragma mark - Private
- (void)handleUpdateWithNewMessages:(NSArray<FLEXSystemLogMessage *> *)newMessages {
if (self.isPaused) {
return;
}
self.title = [self.class globalsEntryTitle:FLEXGlobalsRowSystemLog];
[self.logMessages mutate:^(NSMutableArray *list) {
[list addObjectsFromArray:newMessages];
}];
// Re-filter messages to filter against new messages
if (self.filterText.length) {
[self updateSearchResults:self.filterText];
}
// "Follow" the log as new messages stream in if we were previously near the bottom.
UITableView *tv = self.tableView;
BOOL wasNearBottom = tv.contentOffset.y >= tv.contentSize.height - tv.frame.size.height - 100.0;
[self reloadData];
if (wasNearBottom) {
[self scrollToLastRow];
}
}
- (void)scrollToLastRow {
NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0];
if (numberOfRows > 0) {
NSIndexPath *last = [NSIndexPath indexPathForRow:numberOfRows - 1 inSection:0];
[self.tableView scrollToRowAtIndexPath:last atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
- (void)togglePause {
self.isPaused = !self.isPaused;
UIImage *newIcon = self.isPaused ? [UIImage systemImageNamed:@"play.fill"] : [UIImage systemImageNamed:@"pause.fill"];
self.pauseButton.image = newIcon;
}
- (void)showLogSettings {
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
BOOL disableOSLog = defaults.flex_disableOSLog;
BOOL persistent = defaults.flex_cacheOSLogMessages;
NSString *aslToggle = disableOSLog ? @"Enable os_log (default)" : @"Disable os_log";
NSString *persistence = persistent ? @"Disable persistent logging" : @"Enable persistent logging";
NSString *title = @"System Log Settings";
NSString *body = @"In iOS 10 and up, ASL has been replaced by os_log. "
"The os_log API is much more limited. Below, you can opt-into the old behavior "
"if you want cleaner, more reliable logs within FLEX, but this will break "
"anything that expects os_log to be working, such as Console.app. "
"This setting requires the app to restart to take effect. \n\n"
"To get as close to the old behavior as possible with os_log enabled, logs must "
"be collected manually at launch and stored. This setting has no effect "
"on iOS 9 and below, or if os_log is disabled. "
"You should only enable persistent logging when you need it.";
FLEXOSLogController *logController = (FLEXOSLogController *)self.logController;
[FLEXAlert makeAlert:^(FLEXAlert *make) {
make.title(title).message(body);
make.button(aslToggle).destructiveStyle().handler(^(NSArray<NSString *> *strings) {
[defaults flex_toggleBoolForKey:kFLEXDefaultsDisableOSLogForceASLKey];
});
make.button(persistence).handler(^(NSArray<NSString *> *strings) {
[defaults flex_toggleBoolForKey:kFLEXDefaultsiOSPersistentOSLogKey];
logController.persistent = !persistent;
[logController.messages addObjectsFromArray:self.logMessages.list];
});
make.button(@"Dismiss").cancelStyle();
} showFrom:self];
}
#pragma mark - FLEXGlobalsEntry
+ (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row {
return @"⚠️ System Log";
}
+ (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row {
return [self new];
}
#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
FLEXSystemLogMessage *logMessage = self.logMessages.filteredList[indexPath.row];
return [FLEXSystemLogCell preferredHeightForLogMessage:logMessage inWidth:self.tableView.bounds.size.width];
}
#pragma mark - Copy on long press
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return action == @selector(copy:);
}
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
if (action == @selector(copy:)) {
// We usually only want to copy the log message itself, not any metadata associated with it.
UIPasteboard.generalPasteboard.string = self.logMessages.filteredList[indexPath.row].messageText ?: @"";
}
}
- (UIContextMenuConfiguration *)tableView:(UITableView *)tableView
contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
point:(CGPoint)point __IOS_AVAILABLE(13.0) {
weakify(self)
return [UIContextMenuConfiguration configurationWithIdentifier:nil previewProvider:nil
actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) {
UIAction *copy = [UIAction actionWithTitle:@"Copy"
image:nil
identifier:@"Copy"
handler:^(UIAction *action) { strongify(self)
// We usually only want to copy the log message itself, not any metadata associated with it.
UIPasteboard.generalPasteboard.string = self.logMessages.filteredList[indexPath.row].messageText ?: @"";
}];
return [UIMenu menuWithTitle:@"" image:nil identifier:nil options:UIMenuOptionsDisplayInline children:@[copy]];
}
];
}
@end