FLEXInstancesTableViewController.m 5.5 KB

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