FLEXInstancesTableViewController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. if (malloc_size((__bridge const void *)(object)) > 0) {
  29. [instances addObject:object];
  30. }
  31. }
  32. }];
  33. FLEXInstancesTableViewController *instancesViewController = [[self alloc] init];
  34. instancesViewController.instances = instances;
  35. instancesViewController.title = [NSString stringWithFormat:@"%@ (%lu)", className, (unsigned long)[instances count]];
  36. return instancesViewController;
  37. }
  38. + (instancetype)instancesTableViewControllerForInstancesReferencingObject:(id)object
  39. {
  40. NSMutableArray *instances = [NSMutableArray array];
  41. NSMutableArray *fieldNames = [NSMutableArray array];
  42. [FLEXHeapEnumerator enumerateLiveObjectsUsingBlock:^(__unsafe_unretained id tryObject, __unsafe_unretained Class actualClass) {
  43. // Get all the ivars on the object. Start with the class and and travel up the inheritance chain.
  44. // 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.
  45. Class tryClass = actualClass;
  46. while (tryClass) {
  47. unsigned int ivarCount = 0;
  48. Ivar *ivars = class_copyIvarList(tryClass, &ivarCount);
  49. for (unsigned int ivarIndex = 0; ivarIndex < ivarCount; ivarIndex++) {
  50. Ivar ivar = ivars[ivarIndex];
  51. const char *typeEncoding = ivar_getTypeEncoding(ivar);
  52. if (typeEncoding[0] == @encode(id)[0] || typeEncoding[0] == @encode(Class)[0]) {
  53. ptrdiff_t offset = ivar_getOffset(ivar);
  54. uintptr_t *fieldPointer = (__bridge void *)tryObject + offset;
  55. if (*fieldPointer == (uintptr_t)(__bridge void *)object) {
  56. [instances addObject:tryObject];
  57. [fieldNames addObject:@(ivar_getName(ivar))];
  58. return;
  59. }
  60. }
  61. }
  62. tryClass = class_getSuperclass(tryClass);
  63. }
  64. }];
  65. FLEXInstancesTableViewController *instancesViewController = [[self alloc] init];
  66. instancesViewController.instances = instances;
  67. instancesViewController.fieldNames = fieldNames;
  68. instancesViewController.title = [NSString stringWithFormat:@"Referencing %@ %p", NSStringFromClass(object_getClass(object)), object];
  69. return instancesViewController;
  70. }
  71. #pragma mark - Table View Data Source
  72. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  73. {
  74. return 1;
  75. }
  76. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  77. {
  78. return [self.instances count];
  79. }
  80. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  81. {
  82. static NSString *CellIdentifier = @"Cell";
  83. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  84. if (!cell) {
  85. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  86. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  87. UIFont *cellFont = [FLEXUtility defaultTableViewCellLabelFont];
  88. cell.textLabel.font = cellFont;
  89. cell.detailTextLabel.font = cellFont;
  90. cell.detailTextLabel.textColor = [UIColor grayColor];
  91. }
  92. id instance = self.instances[indexPath.row];
  93. NSString *title = nil;
  94. if ((NSInteger)[self.fieldNames count] > indexPath.row) {
  95. title = [NSString stringWithFormat:@"%@ %@", NSStringFromClass(object_getClass(instance)), self.fieldNames[indexPath.row]];
  96. } else {
  97. title = [NSString stringWithFormat:@"%@ %p", NSStringFromClass(object_getClass(instance)), instance];
  98. }
  99. cell.textLabel.text = title;
  100. cell.detailTextLabel.text = [FLEXRuntimeUtility descriptionForIvarOrPropertyValue:instance];
  101. return cell;
  102. }
  103. #pragma mark - Table View Delegate
  104. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106. id instance = self.instances[indexPath.row];
  107. FLEXObjectExplorerViewController *drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:instance];
  108. [self.navigationController pushViewController:drillInViewController animated:YES];
  109. }
  110. @end