'Signal handler: dump a stack trace for each existing thread.'

def __dumpstacks(self, context=1, sighandler_deep=2):

    currentThreadId = threading.currentThread().ident
    def unique_count(l):
        d = collections.defaultdict((lambda : 0))
        for v in l:
            d[tuple(v)] += 1
        return list(((k, v) for (k, v) in d.items()))
    stack_displays = []
    for (threadId, stack) in sys._current_frames().items():
        stack_display = []
        for (filename, lineno, name, line) in traceback.extract_stack(stack):
            stack_display.append(('        File:    "%s",    line    %d,    in    %s' % (filename, lineno, name)))
            if line:
                stack_display.append(('                %s' % line.strip()))
        if (currentThreadId == threadId):
            stack_display = stack_display[:(- (sighandler_deep * 2))]
            stack_display.append('        =>    Stopped    to    handle    current    signal.    ')
        stack_displays.append(stack_display)
    stacks = unique_count(stack_displays)
    self.ui.debug('thread', '**    Thread    List:\n')
    for (stack, times) in stacks:
        if (times == 1):
            msg = '%s    Thread    is    at:\n%s\n'
        else:
            msg = '%s    Threads    are    at:\n%s\n'
        self.ui.debug('thread', (msg % (times, '\n'.join(stack[(- (context * 2)):]))))
    self.ui.debug('thread', ('Dumped    a    total    of    %d    Threads.' % len(sys._current_frames().keys())))
