FLEXSystemLogViewController.m 8.3 KB

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