~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/trace.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-10-12 00:56:49 UTC
  • mfrom: (5483.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20101012005649-ztnfgo1vqxbomj3v
(mbp) add -Dmem_dump (Martin Pool)

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
import os
60
60
import sys
61
61
import time
 
62
import tempfile
62
63
 
63
64
from bzrlib.lazy_import import lazy_import
64
65
lazy_import(globals(), """
469
470
                    note(line)
470
471
                    break
471
472
 
 
473
def _dump_memory_usage(err_file):
 
474
    try:
 
475
        try:
 
476
            fd, name = tempfile.mkstemp(prefix="bzr_memdump", suffix=".json")
 
477
            dump_file = os.fdopen(fd, 'w')
 
478
            from meliae import scanner
 
479
            scanner.dump_gc_objects(dump_file)
 
480
            err_file.write("Memory dumped to %s\n" % name)
 
481
        except ImportError:
 
482
            err_file.write("Dumping memory requires meliae module.\n")
 
483
            log_exception_quietly()
 
484
        except:
 
485
            err_file.write("Exception while dumping memory.\n")
 
486
            log_exception_quietly()
 
487
    finally:
 
488
        if dump_file is not None:
 
489
            dump_file.close()
 
490
        elif fd is not None:
 
491
            os.close(fd)
472
492
 
473
493
def report_exception(exc_info, err_file):
474
494
    """Report an exception to err_file (typically stderr) and to .bzr.log.
492
512
        return errors.EXIT_ERROR
493
513
    elif isinstance(exc_object, MemoryError):
494
514
        err_file.write("bzr: out of memory\n")
 
515
        if 'mem_dump' in debug.debug_flags:
 
516
            _dump_memory_usage(err_file)
 
517
        else:
 
518
            err_file.write("Use -Dmem_dump to dump memory to a file.\n")
495
519
        return errors.EXIT_ERROR
496
520
    elif isinstance(exc_object, ImportError) \
497
521
        and str(exc_object).startswith("No module named "):