FLEXSystemLogViewController.m 9.2 KB

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