FLEXSystemLogViewController.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // FLEXSystemLogViewController.m
  3. // FLEX
  4. //
  5. // Created by Ryan Olson on 1/19/15.
  6. // Copyright (c) 2015 f. All rights reserved.
  7. //
  8. #import "FLEXSystemLogViewController.h"
  9. #import "FLEXASLLogController.h"
  10. #import "FLEXOSLogController.h"
  11. #import "FLEXSystemLogCell.h"
  12. #import "FLEXUtility.h"
  13. #import "FLEXColor.h"
  14. #import "FLEXResources.h"
  15. #import "UIBarButtonItem+FLEX.h"
  16. #import "fishhook.h"
  17. #import <dlfcn.h>
  18. @interface FLEXSystemLogViewController ()
  19. @property (nonatomic, readonly) id<FLEXLogController> logController;
  20. @property (nonatomic, readonly) NSMutableArray<FLEXSystemLogMessage *> *logMessages;
  21. @property (nonatomic, copy) NSArray<FLEXSystemLogMessage *> *filteredLogMessages;
  22. @end
  23. static void (*MSHookFunction)(void *symbol, void *replace, void **result);
  24. static BOOL FLEXDidHookNSLog = NO;
  25. static BOOL FLEXNSLogHookWorks = NO;
  26. BOOL (*os_log_shim_enabled)(void *addr) = nil;
  27. BOOL (*orig_os_log_shim_enabled)() = nil;
  28. BOOL my_os_log_shim_enabled() {
  29. return NO;
  30. }
  31. @implementation FLEXSystemLogViewController
  32. + (void)load {
  33. // Thanks to @Ram4096 on GitHub for telling me that
  34. // os_log is conditionally enabled by the SDK version
  35. void *addr = __builtin_return_address(0);
  36. void *libsystem_trace = dlopen("/usr/lib/system/libsystem_trace.dylib", RTLD_LAZY);
  37. os_log_shim_enabled = dlsym(libsystem_trace, "os_log_shim_enabled");
  38. if (!os_log_shim_enabled) {
  39. return;
  40. }
  41. FLEXDidHookNSLog = rebind_symbols((struct rebinding[1]) {
  42. "os_log_shim_enabled",
  43. (void *)my_os_log_shim_enabled,
  44. (void **)&orig_os_log_shim_enabled
  45. }, 1) == 0;
  46. if (FLEXDidHookNSLog && orig_os_log_shim_enabled != nil) {
  47. // Check if our rebinding worked
  48. FLEXNSLogHookWorks = os_log_shim_enabled(addr) == NO;
  49. }
  50. // So, just because we rebind the lazily loaded symbol for
  51. // this function doesn't mean it's even going to be used.
  52. // While it seems to be sufficient for the simulator, for
  53. // whatever reason it is not sufficient on-device. We need
  54. // to actually hook the function with something like Substrate.
  55. // Check if we have substrate, and if so use that instead
  56. void *handle = dlopen("/usr/lib/libsubstrate.dylib", RTLD_LAZY);
  57. if (handle) {
  58. MSHookFunction = dlsym(handle, "MSHookFunction");
  59. if (MSHookFunction) {
  60. // Set the hook and check if it worked
  61. void *unused;
  62. MSHookFunction(os_log_shim_enabled, my_os_log_shim_enabled, &unused);
  63. FLEXNSLogHookWorks = os_log_shim_enabled(addr) == NO;
  64. }
  65. }
  66. }
  67. - (id)init {
  68. return [super initWithStyle:UITableViewStylePlain];
  69. }
  70. - (void)viewDidLoad {
  71. [super viewDidLoad];
  72. self.showsSearchBar = YES;
  73. __weak __typeof(self) weakSelf = self;
  74. id logHandler = ^(NSArray<FLEXSystemLogMessage *> *newMessages) {
  75. __strong __typeof(weakSelf) self = weakSelf;
  76. [self handleUpdateWithNewMessages:newMessages];
  77. };
  78. _logMessages = [NSMutableArray array];
  79. if (FLEXOSLogAvailable() && !FLEXNSLogHookWorks) {
  80. _logController = [FLEXOSLogController withUpdateHandler:logHandler];
  81. } else {
  82. _logController = [FLEXASLLogController withUpdateHandler:logHandler];
  83. }
  84. [self.tableView registerClass:[FLEXSystemLogCell class] forCellReuseIdentifier:kFLEXSystemLogCellIdentifier];
  85. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  86. self.title = @"Loading...";
  87. // Toolbar buttons //
  88. UIBarButtonItem *scrollDown = [UIBarButtonItem
  89. itemWithImage:FLEXResources.scrollToBottomIcon
  90. target:self
  91. action:@selector(scrollToLastRow)
  92. ];
  93. UIBarButtonItem *settings = [UIBarButtonItem
  94. itemWithImage:FLEXResources.gearIcon
  95. target:self
  96. action:@selector(showLogSettings)
  97. ];
  98. if (FLEXOSLogAvailable() && !FLEXNSLogHookWorks) {
  99. [self addToolbarItems:@[scrollDown, settings]];
  100. } else {
  101. [self addToolbarItems:@[scrollDown]];
  102. }
  103. }
  104. - (void)handleUpdateWithNewMessages:(NSArray<FLEXSystemLogMessage *> *)newMessages {
  105. self.title = @"System Log";
  106. [self.logMessages addObjectsFromArray:newMessages];
  107. // "Follow" the log as new messages stream in if we were previously near the bottom.
  108. BOOL wasNearBottom = self.tableView.contentOffset.y >= self.tableView.contentSize.height - self.tableView.frame.size.height - 100.0;
  109. [self.tableView reloadData];
  110. if (wasNearBottom) {
  111. [self scrollToLastRow];
  112. }
  113. }
  114. - (void)viewWillAppear:(BOOL)animated {
  115. [super viewWillAppear:animated];
  116. [self.logController startMonitoring];
  117. }
  118. - (void)scrollToLastRow {
  119. NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0];
  120. if (numberOfRows > 0) {
  121. NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:numberOfRows - 1 inSection:0];
  122. [self.tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  123. }
  124. }
  125. - (void)showLogSettings {
  126. FLEXOSLogController *logController = (FLEXOSLogController *)self.logController;
  127. BOOL persistent = [[NSUserDefaults standardUserDefaults] boolForKey:kFLEXiOSPersistentOSLogKey];
  128. NSString *toggle = persistent ? @"Disable" : @"Enable";
  129. NSString *title = [@"Persistent logging: " stringByAppendingString:persistent ? @"ON" : @"OFF"];
  130. NSString *body = @"In iOS 10 and up, ASL is gone. The OS Log API is much more limited. "
  131. "To get as close to the old behavior as possible, logs must be collected manually at launch and stored.\n\n"
  132. "Turn this feature on only when you need it.";
  133. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  134. make.title(title).message(body).button(toggle).handler(^(NSArray<NSString *> *strings) {
  135. [[NSUserDefaults standardUserDefaults] setBool:!persistent forKey:kFLEXiOSPersistentOSLogKey];
  136. logController.persistent = !persistent;
  137. [logController.messages addObjectsFromArray:self.logMessages];
  138. });
  139. make.button(@"Dismiss").cancelStyle();
  140. } showFrom:self];
  141. }
  142. #pragma mark - FLEXGlobalsEntry
  143. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row {
  144. return @"⚠️ System Log";
  145. }
  146. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row {
  147. return [self new];
  148. }
  149. #pragma mark - Table view data source
  150. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  151. return self.searchController.isActive ? self.filteredLogMessages.count : self.logMessages.count;
  152. }
  153. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  154. FLEXSystemLogCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXSystemLogCellIdentifier forIndexPath:indexPath];
  155. cell.logMessage = [self logMessageAtIndexPath:indexPath];
  156. cell.highlightedText = self.searchText;
  157. if (indexPath.row % 2 == 0) {
  158. cell.backgroundColor = [FLEXColor primaryBackgroundColor];
  159. } else {
  160. cell.backgroundColor = [FLEXColor secondaryBackgroundColor];
  161. }
  162. return cell;
  163. }
  164. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  165. FLEXSystemLogMessage *logMessage = [self logMessageAtIndexPath:indexPath];
  166. return [FLEXSystemLogCell preferredHeightForLogMessage:logMessage inWidth:self.tableView.bounds.size.width];
  167. }
  168. #pragma mark - Copy on long press
  169. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
  170. return YES;
  171. }
  172. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  173. return action == @selector(copy:);
  174. }
  175. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  176. if (action == @selector(copy:)) {
  177. // We usually only want to copy the log message itself, not any metadata associated with it.
  178. UIPasteboard.generalPasteboard.string = [self logMessageAtIndexPath:indexPath].messageText;
  179. }
  180. }
  181. - (FLEXSystemLogMessage *)logMessageAtIndexPath:(NSIndexPath *)indexPath {
  182. return self.searchController.isActive ? self.filteredLogMessages[indexPath.row] : self.logMessages[indexPath.row];
  183. }
  184. #pragma mark - Search bar
  185. - (void)updateSearchResults:(NSString *)searchString {
  186. [self onBackgroundQueue:^NSArray *{
  187. return [self.logMessages filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(FLEXSystemLogMessage *logMessage, NSDictionary<NSString *, id> *bindings) {
  188. NSString *displayedText = [FLEXSystemLogCell displayedTextForLogMessage:logMessage];
  189. return [displayedText rangeOfString:searchString options:NSCaseInsensitiveSearch].length > 0;
  190. }]];
  191. } thenOnMainQueue:^(NSArray *filteredLogMessages) {
  192. if ([self.searchText isEqual:searchString]) {
  193. self.filteredLogMessages = filteredLogMessages;
  194. [self.tableView reloadData];
  195. }
  196. }];
  197. }
  198. @end