FLEXInstancesTableViewController.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // FLEXInstancesTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/28/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXInstancesTableViewController.h"
  9. #import "FLEXObjectExplorerFactory.h"
  10. #import "FLEXObjectExplorerViewController.h"
  11. #import "FLEXRuntimeUtility.h"
  12. #import "FLEXUtility.h"
  13. #import "FLEXHeapEnumerator.h"
  14. @interface FLEXInstancesTableViewController ()
  15. @property (nonatomic, strong) NSArray *instances;
  16. @property (nonatomic, strong) NSArray *fieldNames;
  17. @end
  18. @implementation FLEXInstancesTableViewController
  19. + (instancetype)instancesTableViewControllerForClassName:(NSString *)className
  20. {
  21. const char *classNameCString = [className UTF8String];
  22. NSMutableArray *instances = [NSMutableArray array];
  23. [FLEXHeapEnumerator enumerateLiveObjectsUsingBlock:^(__unsafe_unretained id object, __unsafe_unretained Class actualClass) {
  24. if (strcmp(classNameCString, class_getName(actualClass)) == 0) {
  25. // Note: objects of certain classes crash when retain is called. It is up to the user to avoid tapping into instance lists for these classes.
  26. // Ex. OS_dispatch_queue_specific_queue
  27. // In the future, we could provide some kind of warning for classes that are known to be problematic.
  28. [instances addObject:object];
  29. }
  30. }];
  31. FLEXInstancesTableViewController *instancesViewController = [[self alloc] init];
  32. instancesViewController.instances = instances;
  33. instancesViewController.title = [NSString stringWithFormat:@"%@ (%lu)", className, (unsigned long)[instances count]];
  34. return instancesViewController;
  35. }
  36. + (instancetype)instancesTableViewControllerForInstancesReferencingObject:(id)object
  37. {
  38. NSMutableArray *instances = [NSMutableArray array];
  39. NSMutableArray *fieldNames = [NSMutableArray array];
  40. [FLEXHeapEnumerator enumerateLiveObjectsUsingBlock:^(__unsafe_unretained id tryObject, __unsafe_unretained Class actualClass) {
  41. // Get all the ivars on the object. Start with the class and and travel up the inheritance chain.
  42. // Once we find a match, record it and move on to the next object. There's no reason to find multiple matches within the same object.
  43. Class tryClass = actualClass;
  44. while (tryClass) {
  45. unsigned int ivarCount = 0;
  46. Ivar *ivars = class_copyIvarList(tryClass, &ivarCount);
  47. for (unsigned int ivarIndex = 0; ivarIndex < ivarCount; ivarIndex++) {
  48. Ivar ivar = ivars[ivarIndex];
  49. const char *typeEncoding = ivar_getTypeEncoding(ivar);
  50. if (typeEncoding[0] == @encode(id)[0] || typeEncoding[0] == @encode(Class)[0]) {
  51. ptrdiff_t offset = ivar_getOffset(ivar);
  52. uintptr_t *fieldPointer = (__bridge void *)tryObject + offset;
  53. if (*fieldPointer == (uintptr_t)(__bridge void *)object) {
  54. [instances addObject:tryObject];
  55. [fieldNames addObject:@(ivar_getName(ivar))];
  56. return;
  57. }
  58. }
  59. }
  60. tryClass = class_getSuperclass(tryClass);
  61. }
  62. }];
  63. FLEXInstancesTableViewController *instancesViewController = [[self alloc] init];
  64. instancesViewController.instances = instances;
  65. instancesViewController.fieldNames = fieldNames;
  66. instancesViewController.title = [NSString stringWithFormat:@"Referencing %@ %p", NSStringFromClass(object_getClass(object)), object];
  67. return instancesViewController;
  68. }
  69. #pragma mark - Table View Data Source
  70. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  71. {
  72. return 1;
  73. }
  74. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  75. {
  76. return [self.instances count];
  77. }
  78. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  79. {
  80. static NSString *CellIdentifier = @"Cell";
  81. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  82. if (!cell) {
  83. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  84. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  85. UIFont *cellFont = [FLEXUtility defaultTableViewCellLabelFont];
  86. cell.textLabel.font = cellFont;
  87. cell.detailTextLabel.font = cellFont;
  88. cell.detailTextLabel.textColor = [UIColor grayColor];
  89. }
  90. id instance = self.instances[indexPath.row];
  91. NSString *title = nil;
  92. if ((NSInteger)[self.fieldNames count] > indexPath.row) {
  93. title = [NSString stringWithFormat:@"%@ %@", NSStringFromClass(object_getClass(instance)), self.fieldNames[indexPath.row]];
  94. } else {
  95. title = [NSString stringWithFormat:@"%@ %p", NSStringFromClass(object_getClass(instance)), instance];
  96. }
  97. cell.textLabel.text = title;
  98. cell.detailTextLabel.text = [FLEXRuntimeUtility descriptionForIvarOrPropertyValue:instance];
  99. return cell;
  100. }
  101. #pragma mark - Table View Delegate
  102. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  103. {
  104. id instance = self.instances[indexPath.row];
  105. FLEXObjectExplorerViewController *drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:instance];
  106. [self.navigationController pushViewController:drillInViewController animated:YES];
  107. }
  108. @end