FLEXSystemLogViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #if !TARGET_OS_TV
  92. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  93. #endif
  94. self.title = @"Waiting for Logs...";
  95. // Toolbar buttons //
  96. UIBarButtonItem *scrollDown = [UIBarButtonItem
  97. flex_itemWithImage:FLEXResources.scrollToBottomIcon
  98. target:self
  99. action:@selector(scrollToLastRow)
  100. ];
  101. UIBarButtonItem *settings = [UIBarButtonItem
  102. flex_itemWithImage:FLEXResources.gearIcon
  103. target:self
  104. action:@selector(showLogSettings)
  105. ];
  106. [self addToolbarItems:@[scrollDown, settings]];
  107. }
  108. - (void)viewWillAppear:(BOOL)animated {
  109. [super viewWillAppear:animated];
  110. [self.logController startMonitoring];
  111. }
  112. - (NSArray<FLEXTableViewSection *> *)makeSections {
  113. __weak __typeof(self) weakSelf = self;
  114. _logMessages = [FLEXMutableListSection list:@[]
  115. cellConfiguration:^(FLEXSystemLogCell *cell, FLEXSystemLogMessage *message, NSInteger row) {
  116. __strong __typeof(self) strongSelf = weakSelf;
  117. if (strongSelf) {
  118. cell.logMessage = message;
  119. cell.highlightedText = strongSelf.filterText;
  120. if (row % 2 == 0) {
  121. cell.backgroundColor = FLEXColor.primaryBackgroundColor;
  122. } else {
  123. cell.backgroundColor = FLEXColor.secondaryBackgroundColor;
  124. }
  125. }
  126. } filterMatcher:^BOOL(NSString *filterText, FLEXSystemLogMessage *message) {
  127. NSString *displayedText = [FLEXSystemLogCell displayedTextForLogMessage:message];
  128. return [displayedText localizedCaseInsensitiveContainsString:filterText];
  129. }
  130. ];
  131. self.logMessages.cellRegistrationMapping = @{
  132. kFLEXSystemLogCellIdentifier : [FLEXSystemLogCell class]
  133. };
  134. return @[self.logMessages];
  135. }
  136. - (NSArray<FLEXTableViewSection *> *)nonemptySections {
  137. return @[self.logMessages];
  138. }
  139. #pragma mark - Private
  140. - (void)handleUpdateWithNewMessages:(NSArray<FLEXSystemLogMessage *> *)newMessages {
  141. self.title = [self.class globalsEntryTitle:FLEXGlobalsRowSystemLog];
  142. [self.logMessages mutate:^(NSMutableArray *list) {
  143. [list addObjectsFromArray:newMessages];
  144. }];
  145. // "Follow" the log as new messages stream in if we were previously near the bottom.
  146. BOOL wasNearBottom = self.tableView.contentOffset.y >= self.tableView.contentSize.height - self.tableView.frame.size.height - 100.0;
  147. [self reloadData];
  148. if (wasNearBottom) {
  149. [self scrollToLastRow];
  150. }
  151. }
  152. - (void)scrollToLastRow {
  153. NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0];
  154. if (numberOfRows > 0) {
  155. NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:numberOfRows - 1 inSection:0];
  156. [self.tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  157. }
  158. }
  159. - (void)showLogSettings {
  160. NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
  161. BOOL disableOSLog = defaults.flex_disableOSLog;
  162. BOOL persistent = defaults.flex_cacheOSLogMessages;
  163. NSString *aslToggle = disableOSLog ? @"Enable os_log (default)" : @"Disable os_log";
  164. NSString *persistence = persistent ? @"Disable persistent logging" : @"Enable persistent logging";
  165. NSString *title = @"System Log Settings";
  166. NSString *body = @"In iOS 10 and up, ASL has been replaced by os_log. "
  167. "The os_log API is much more limited. Below, you can opt-into the old behavior "
  168. "if you want cleaner, more reliable logs within FLEX, but this will break "
  169. "anything that expects os_log to be working, such as Console.app. "
  170. "This setting requires the app to restart to take effect. \n\n"
  171. "To get as close to the old behavior as possible with os_log enabled, logs must "
  172. "be collected manually at launch and stored. This setting has no effect "
  173. "on iOS 9 and below, or if os_log is disabled. "
  174. "You should only enable persistent logging when you need it.";
  175. FLEXOSLogController *logController = (FLEXOSLogController *)self.logController;
  176. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  177. make.title(title).message(body);
  178. make.button(aslToggle).destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  179. [defaults flex_toggleBoolForKey:kFLEXDefaultsDisableOSLogForceASLKey];
  180. });
  181. make.button(persistence).handler(^(NSArray<NSString *> *strings) {
  182. [defaults flex_toggleBoolForKey:kFLEXDefaultsiOSPersistentOSLogKey];
  183. logController.persistent = !persistent;
  184. [logController.messages addObjectsFromArray:self.logMessages.list];
  185. });
  186. make.button(@"Dismiss").cancelStyle();
  187. } showFrom:self];
  188. }
  189. #pragma mark - FLEXGlobalsEntry
  190. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row {
  191. return @"⚠️ System Log";
  192. }
  193. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row {
  194. return [self new];
  195. }
  196. #pragma mark - Table view data source
  197. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  198. FLEXSystemLogMessage *logMessage = self.logMessages.filteredList[indexPath.row];
  199. return [FLEXSystemLogCell preferredHeightForLogMessage:logMessage inWidth:self.tableView.bounds.size.width];
  200. }
  201. #pragma mark - Copy on long press
  202. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
  203. return YES;
  204. }
  205. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  206. return action == @selector(copy:);
  207. }
  208. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  209. if (action == @selector(copy:)) {
  210. // We usually only want to copy the log message itself, not any metadata associated with it.
  211. #if !TARGET_OS_TV
  212. UIPasteboard.generalPasteboard.string = self.logMessages.filteredList[indexPath.row].messageText;
  213. #endif
  214. }
  215. }
  216. #if FLEX_AT_LEAST_IOS13_SDK
  217. #if !TARGET_OS_TV
  218. - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView
  219. contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
  220. point:(CGPoint)point __IOS_AVAILABLE(13.0) {
  221. __weak __typeof__(self) weakSelf = self;
  222. return [UIContextMenuConfiguration configurationWithIdentifier:nil
  223. previewProvider:nil
  224. actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) {
  225. UIAction *copy = [UIAction actionWithTitle:@"Copy"
  226. image:nil
  227. identifier:@"Copy"
  228. handler:^(__kindof UIAction *action) {
  229. // We usually only want to copy the log message itself, not any metadata associated with it.
  230. UIPasteboard.generalPasteboard.string = weakSelf.logMessages.filteredList[indexPath.row].messageText ?: @"";
  231. }];
  232. return [UIMenu menuWithTitle:@"" image:nil identifier:nil options:UIMenuOptionsDisplayInline children:@[copy]];
  233. }];
  234. }
  235. #endif
  236. #endif
  237. @end