FLEXSystemLogViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // FLEXSystemLogViewController.m
  3. // FLEX
  4. //
  5. // Created by Ryan Olson on 1/19/15.
  6. // Copyright (c) 2020 FLEX Team. 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. static BOOL my_os_log_shim_enabled(void *addr) {
  30. return NO;
  31. }
  32. @implementation FLEXSystemLogViewController
  33. #pragma mark - Initialization
  34. + (void)load {
  35. // User must opt-into disabling os_log
  36. if (!NSUserDefaults.standardUserDefaults.flex_disableOSLog) {
  37. return;
  38. }
  39. // Thanks to @Ram4096 on GitHub for telling me that
  40. // os_log is conditionally enabled by the SDK version
  41. void *addr = __builtin_return_address(0);
  42. void *libsystem_trace = dlopen("/usr/lib/system/libsystem_trace.dylib", RTLD_LAZY);
  43. os_log_shim_enabled = dlsym(libsystem_trace, "os_log_shim_enabled");
  44. if (!os_log_shim_enabled) {
  45. return;
  46. }
  47. FLEXDidHookNSLog = flex_rebind_symbols((struct rebinding[1]) {{
  48. "os_log_shim_enabled",
  49. (void *)my_os_log_shim_enabled,
  50. (void **)&orig_os_log_shim_enabled
  51. }}, 1) == 0;
  52. if (FLEXDidHookNSLog && orig_os_log_shim_enabled != nil) {
  53. // Check if our rebinding worked
  54. FLEXNSLogHookWorks = my_os_log_shim_enabled(addr) == NO;
  55. }
  56. // So, just because we rebind the lazily loaded symbol for
  57. // this function doesn't mean it's even going to be used.
  58. // While it seems to be sufficient for the simulator, for
  59. // whatever reason it is not sufficient on-device. We need
  60. // to actually hook the function with something like Substrate.
  61. // Check if we have substrate, and if so use that instead
  62. void *handle = dlopen("/usr/lib/libsubstrate.dylib", RTLD_LAZY);
  63. if (handle) {
  64. MSHookFunction = dlsym(handle, "MSHookFunction");
  65. if (MSHookFunction) {
  66. // Set the hook and check if it worked
  67. void *unused;
  68. MSHookFunction(os_log_shim_enabled, my_os_log_shim_enabled, &unused);
  69. FLEXNSLogHookWorks = os_log_shim_enabled(addr) == NO;
  70. }
  71. }
  72. }
  73. - (id)init {
  74. return [super initWithStyle:UITableViewStylePlain];
  75. }
  76. #pragma mark - Overrides
  77. - (void)viewDidLoad {
  78. [super viewDidLoad];
  79. self.showsSearchBar = YES;
  80. self.showSearchBarInitially = NO;
  81. __weak __typeof(self) weakSelf = self;
  82. id logHandler = ^(NSArray<FLEXSystemLogMessage *> *newMessages) {
  83. __strong __typeof(weakSelf) strongSelf = weakSelf;
  84. [strongSelf handleUpdateWithNewMessages:newMessages];
  85. };
  86. if (FLEXOSLogAvailable() && !FLEXNSLogHookWorks) {
  87. _logController = [FLEXOSLogController withUpdateHandler:logHandler];
  88. } else {
  89. _logController = [FLEXASLLogController withUpdateHandler:logHandler];
  90. }
  91. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  92. self.title = @"Waiting for Logs...";
  93. // Toolbar buttons //
  94. UIBarButtonItem *scrollDown = [UIBarButtonItem
  95. flex_itemWithImage:FLEXResources.scrollToBottomIcon
  96. target:self
  97. action:@selector(scrollToLastRow)
  98. ];
  99. UIBarButtonItem *settings = [UIBarButtonItem
  100. flex_itemWithImage:FLEXResources.gearIcon
  101. target:self
  102. action:@selector(showLogSettings)
  103. ];
  104. [self addToolbarItems:@[scrollDown, settings]];
  105. }
  106. - (void)viewWillAppear:(BOOL)animated {
  107. [super viewWillAppear:animated];
  108. [self.logController startMonitoring];
  109. }
  110. - (NSArray<FLEXTableViewSection *> *)makeSections {
  111. __weak __typeof(self) weakSelf = self;
  112. _logMessages = [FLEXMutableListSection list:@[]
  113. cellConfiguration:^(FLEXSystemLogCell *cell, FLEXSystemLogMessage *message, NSInteger row) {
  114. __strong __typeof(self) strongSelf = weakSelf;
  115. if (strongSelf) {
  116. cell.logMessage = message;
  117. cell.highlightedText = strongSelf.filterText;
  118. if (row % 2 == 0) {
  119. cell.backgroundColor = FLEXColor.primaryBackgroundColor;
  120. } else {
  121. cell.backgroundColor = FLEXColor.secondaryBackgroundColor;
  122. }
  123. }
  124. } filterMatcher:^BOOL(NSString *filterText, FLEXSystemLogMessage *message) {
  125. NSString *displayedText = [FLEXSystemLogCell displayedTextForLogMessage:message];
  126. return [displayedText localizedCaseInsensitiveContainsString:filterText];
  127. }
  128. ];
  129. self.logMessages.cellRegistrationMapping = @{
  130. kFLEXSystemLogCellIdentifier : [FLEXSystemLogCell class]
  131. };
  132. return @[self.logMessages];
  133. }
  134. - (NSArray<FLEXTableViewSection *> *)nonemptySections {
  135. return @[self.logMessages];
  136. }
  137. #pragma mark - Private
  138. - (void)handleUpdateWithNewMessages:(NSArray<FLEXSystemLogMessage *> *)newMessages {
  139. self.title = [self.class globalsEntryTitle:FLEXGlobalsRowSystemLog];
  140. [self.logMessages mutate:^(NSMutableArray *list) {
  141. [list addObjectsFromArray:newMessages];
  142. }];
  143. // "Follow" the log as new messages stream in if we were previously near the bottom.
  144. BOOL wasNearBottom = self.tableView.contentOffset.y >= self.tableView.contentSize.height - self.tableView.frame.size.height - 100.0;
  145. [self reloadData];
  146. if (wasNearBottom) {
  147. [self scrollToLastRow];
  148. }
  149. }
  150. - (void)scrollToLastRow {
  151. NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0];
  152. if (numberOfRows > 0) {
  153. NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:numberOfRows - 1 inSection:0];
  154. [self.tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  155. }
  156. }
  157. - (void)showLogSettings {
  158. NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
  159. BOOL disableOSLog = defaults.flex_disableOSLog;
  160. BOOL persistent = defaults.flex_cacheOSLogMessages;
  161. NSString *aslToggle = disableOSLog ? @"Enable os_log (default)" : @"Disable os_log";
  162. NSString *persistence = persistent ? @"Disable persistent logging" : @"Enable persistent logging";
  163. NSString *title = @"System Log Settings";
  164. NSString *body = @"In iOS 10 and up, ASL has been replaced by os_log. "
  165. "The os_log API is much more limited. Below, you can opt-into the old behavior "
  166. "if you want cleaner, more reliable logs within FLEX, but this will break "
  167. "anything that expects os_log to be working, such as Console.app. "
  168. "This setting requires the app to restart to take effect. \n\n"
  169. "To get as close to the old behavior as possible with os_log enabled, logs must "
  170. "be collected manually at launch and stored. This setting has no effect "
  171. "on iOS 9 and below, or if os_log is disabled. "
  172. "You should only enable persistent logging when you need it.";
  173. FLEXOSLogController *logController = (FLEXOSLogController *)self.logController;
  174. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  175. make.title(title).message(body);
  176. make.button(aslToggle).destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  177. [defaults flex_toggleBoolForKey:kFLEXDefaultsDisableOSLogForceASLKey];
  178. });
  179. make.button(persistence).handler(^(NSArray<NSString *> *strings) {
  180. [defaults flex_toggleBoolForKey:kFLEXDefaultsiOSPersistentOSLogKey];
  181. logController.persistent = !persistent;
  182. [logController.messages addObjectsFromArray:self.logMessages.list];
  183. });
  184. make.button(@"Dismiss").cancelStyle();
  185. } showFrom:self];
  186. }
  187. #pragma mark - FLEXGlobalsEntry
  188. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row {
  189. return @"⚠️ System Log";
  190. }
  191. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row {
  192. return [self new];
  193. }
  194. #pragma mark - Table view data source
  195. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  196. FLEXSystemLogMessage *logMessage = self.logMessages.filteredList[indexPath.row];
  197. return [FLEXSystemLogCell preferredHeightForLogMessage:logMessage inWidth:self.tableView.bounds.size.width];
  198. }
  199. #pragma mark - Copy on long press
  200. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
  201. return YES;
  202. }
  203. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  204. return action == @selector(copy:);
  205. }
  206. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  207. if (action == @selector(copy:)) {
  208. // We usually only want to copy the log message itself, not any metadata associated with it.
  209. UIPasteboard.generalPasteboard.string = self.logMessages.filteredList[indexPath.row].messageText ?: @"";
  210. }
  211. }
  212. #if FLEX_AT_LEAST_IOS13_SDK
  213. - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView
  214. contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
  215. point:(CGPoint)point __IOS_AVAILABLE(13.0) {
  216. __weak __typeof__(self) weakSelf = self;
  217. return [UIContextMenuConfiguration configurationWithIdentifier:nil
  218. previewProvider:nil
  219. actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) {
  220. UIAction *copy = [UIAction actionWithTitle:@"Copy"
  221. image:nil
  222. identifier:@"Copy"
  223. handler:^(__kindof UIAction *action) {
  224. // We usually only want to copy the log message itself, not any metadata associated with it.
  225. UIPasteboard.generalPasteboard.string = weakSelf.logMessages.filteredList[indexPath.row].messageText ?: @"";
  226. }];
  227. return [UIMenu menuWithTitle:@"" image:nil identifier:nil options:UIMenuOptionsDisplayInline children:@[copy]];
  228. }];
  229. }
  230. #endif
  231. @end