Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: chrome/common/mac/objc_zombie.mm

Issue 7826016: [Mac] Enable CrZombie for all processes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "chrome/browser/ui/cocoa/objc_zombie.h" 5 #import "chrome/common/mac/objc_zombie.h"
6 6
7 #include <dlfcn.h> 7 #include <dlfcn.h>
8 #include <execinfo.h> 8 #include <execinfo.h>
9 #include <mach-o/dyld.h> 9 #include <mach-o/dyld.h>
10 #include <mach-o/nlist.h> 10 #include <mach-o/nlist.h>
11 11
12 #import <objc/objc-class.h> 12 #import <objc/objc-class.h>
13 13
14 #include <algorithm> 14 #include <algorithm>
15 #include <iostream> 15 #include <iostream>
16 16
17 #include "base/debug/stack_trace.h" 17 #include "base/debug/stack_trace.h"
18 #include "base/logging.h" 18 #include "base/logging.h"
19 #include "base/mac/mac_util.h" 19 #include "base/mac/mac_util.h"
20 #include "base/metrics/histogram.h" 20 #include "base/metrics/histogram.h"
21 #include "base/synchronization/lock.h" 21 #include "base/synchronization/lock.h"
22 #import "chrome/app/breakpad_mac.h" 22 #import "chrome/app/breakpad_mac.h"
23 #import "chrome/browser/ui/cocoa/objc_method_swizzle.h" 23 #import "chrome/common/mac/objc_method_swizzle.h"
24 24
25 // Deallocated objects are re-classed as |CrZombie|. No superclass 25 // Deallocated objects are re-classed as |CrZombie|. No superclass
26 // because then the class would have to override many/most of the 26 // because then the class would have to override many/most of the
27 // inherited methods (|NSObject| is like a category magnet!). 27 // inherited methods (|NSObject| is like a category magnet!).
28 @interface CrZombie { 28 @interface CrZombie {
29 Class isa; 29 Class isa;
30 } 30 }
31 @end 31 @end
32 32
33 // Objects with enough space are made into "fat" zombies, which 33 // Objects with enough space are made into "fat" zombies, which
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 @implementation NSObject (CrZombie) 439 @implementation NSObject (CrZombie)
440 440
441 - (BOOL)shouldBecomeCrZombie { 441 - (BOOL)shouldBecomeCrZombie {
442 return NO; 442 return NO;
443 } 443 }
444 444
445 @end 445 @end
446 446
447 namespace ObjcEvilDoers { 447 namespace ObjcEvilDoers {
448 448
449 BOOL ZombieEnable(BOOL zombieAllObjects, 449 bool ZombieEnable(bool zombieAllObjects,
450 size_t zombieCount) { 450 size_t zombieCount) {
451 // Only allow enable/disable on the main thread, just to keep things 451 // Only allow enable/disable on the main thread, just to keep things
452 // simple. 452 // simple.
453 CHECK([NSThread isMainThread]); 453 CHECK([NSThread isMainThread]);
454 DLOG(INFO)
455 << "ZombieEnable(" << (zombieAllObjects ? "true" : "false") << ", "
456 << zombieCount << ")";
454 457
455 if (!ZombieInit()) 458 if (!ZombieInit())
456 return NO; 459 return false;
457 460
458 g_zombieAllObjects = zombieAllObjects; 461 g_zombieAllObjects = zombieAllObjects;
459 462
460 // Replace the implementation of -[NSObject dealloc]. 463 // Replace the implementation of -[NSObject dealloc].
461 Method m = class_getInstanceMethod([NSObject class], @selector(dealloc)); 464 Method m = class_getInstanceMethod([NSObject class], @selector(dealloc));
462 if (!m) 465 if (!m)
463 return NO; 466 return false;
464 467
465 const IMP prevDeallocIMP = method_setImplementation(m, (IMP)ZombieDealloc); 468 const IMP prevDeallocIMP = method_setImplementation(m, (IMP)ZombieDealloc);
466 DCHECK(prevDeallocIMP == g_originalDeallocIMP || 469 DCHECK(prevDeallocIMP == g_originalDeallocIMP ||
467 prevDeallocIMP == (IMP)ZombieDealloc); 470 prevDeallocIMP == (IMP)ZombieDealloc);
468 471
469 // Grab the current set of zombies. This is thread-safe because 472 // Grab the current set of zombies. This is thread-safe because
470 // only the main thread can change these. 473 // only the main thread can change these.
471 const size_t oldCount = g_zombieCount; 474 const size_t oldCount = g_zombieCount;
472 ZombieRecord* oldZombies = g_zombies; 475 ZombieRecord* oldZombies = g_zombies;
473 476
(...skipping 10 matching lines...) Expand all
484 g_zombies = NULL; 487 g_zombies = NULL;
485 if (g_zombieCount) { 488 if (g_zombieCount) {
486 g_zombies = 489 g_zombies =
487 static_cast<ZombieRecord*>(calloc(g_zombieCount, sizeof(*g_zombies))); 490 static_cast<ZombieRecord*>(calloc(g_zombieCount, sizeof(*g_zombies)));
488 if (!g_zombies) { 491 if (!g_zombies) {
489 NOTREACHED(); 492 NOTREACHED();
490 g_zombies = oldZombies; 493 g_zombies = oldZombies;
491 g_zombieCount = oldCount; 494 g_zombieCount = oldCount;
492 g_zombieIndex = oldIndex; 495 g_zombieIndex = oldIndex;
493 ZombieDisable(); 496 ZombieDisable();
494 return NO; 497 return false;
495 } 498 }
496 } 499 }
497 500
498 // If the count is changing, allow some of the zombies to continue 501 // If the count is changing, allow some of the zombies to continue
499 // shambling forward. 502 // shambling forward.
500 const size_t sharedCount = std::min(oldCount, zombieCount); 503 const size_t sharedCount = std::min(oldCount, zombieCount);
501 if (sharedCount) { 504 if (sharedCount) {
502 // Get index of the first shared zombie. 505 // Get index of the first shared zombie.
503 oldIndex = (oldIndex + oldCount - sharedCount) % oldCount; 506 oldIndex = (oldIndex + oldCount - sharedCount) % oldCount;
504 507
505 for (; g_zombieIndex < sharedCount; ++ g_zombieIndex) { 508 for (; g_zombieIndex < sharedCount; ++ g_zombieIndex) {
506 DCHECK_LT(g_zombieIndex, g_zombieCount); 509 DCHECK_LT(g_zombieIndex, g_zombieCount);
507 DCHECK_LT(oldIndex, oldCount); 510 DCHECK_LT(oldIndex, oldCount);
508 std::swap(g_zombies[g_zombieIndex], oldZombies[oldIndex]); 511 std::swap(g_zombies[g_zombieIndex], oldZombies[oldIndex]);
509 oldIndex = (oldIndex + 1) % oldCount; 512 oldIndex = (oldIndex + 1) % oldCount;
510 } 513 }
511 g_zombieIndex %= g_zombieCount; 514 g_zombieIndex %= g_zombieCount;
512 } 515 }
513 } 516 }
514 517
515 // Free the old treadmill and any remaining zombies. 518 // Free the old treadmill and any remaining zombies.
516 if (oldZombies) { 519 if (oldZombies) {
517 for (size_t i = 0; i < oldCount; ++i) { 520 for (size_t i = 0; i < oldCount; ++i) {
518 if (oldZombies[i].object) 521 if (oldZombies[i].object)
519 object_dispose(oldZombies[i].object); 522 object_dispose(oldZombies[i].object);
520 } 523 }
521 free(oldZombies); 524 free(oldZombies);
522 } 525 }
523 526
524 return YES; 527 return true;
525 } 528 }
526 529
527 void ZombieDisable() { 530 void ZombieDisable() {
528 // Only allow enable/disable on the main thread, just to keep things 531 // Only allow enable/disable on the main thread, just to keep things
529 // simple. 532 // simple.
530 CHECK([NSThread isMainThread]); 533 CHECK([NSThread isMainThread]);
531 534
532 // |ZombieInit()| was never called. 535 // |ZombieInit()| was never called.
533 if (!g_originalDeallocIMP) 536 if (!g_originalDeallocIMP)
534 return; 537 return;
(...skipping 17 matching lines...) Expand all
552 if (oldZombies) { 555 if (oldZombies) {
553 for (size_t i = 0; i < oldCount; ++i) { 556 for (size_t i = 0; i < oldCount; ++i) {
554 if (oldZombies[i].object) 557 if (oldZombies[i].object)
555 object_dispose(oldZombies[i].object); 558 object_dispose(oldZombies[i].object);
556 } 559 }
557 free(oldZombies); 560 free(oldZombies);
558 } 561 }
559 } 562 }
560 563
561 } // namespace ObjcEvilDoers 564 } // namespace ObjcEvilDoers
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698