Wednesday, October 12, 2011

GDB: Quickly attach to the only instance of a program

Wouldn't it be nice if, instead running shell pidof foo and pasting the resulting PID into an attach command, you could just run one command to attach to your inferior? Sort of like this:
(gdb) qattach crawl
[Thread debugging using libthread_db enabled]
0xb7d1588e in __open_nocancel () at ../sysdeps/unix/syscall-template.S:82
82      ../sysdeps/unix/syscall-template.S: No such file or directory.
        in ../sysdeps/unix/syscall-template.S
(gdb)
Well, now you can! Just arrange for GDB to run the following Python code first, which (gdb) Python Commands and (gdb) Startup will show you how to do:

# Copyright (c) 2011 Samuel Bronson
#
# This software is made available under the same license as the
# "expat" XML library for C, or the "do whatever you want" license, at
# your option.

# Note: The subprocess.check_output() function is new in Python 2.7.

class QAttach(gdb.Command):
    """Quickly attach to the only instance of a program."""

    def __init__(self):
        super(QAttach, self).__init__("qattach", gdb.COMMAND_RUNNING, gdb.COMPLETE_FILENAME)

    def invoke(self, arg, from_tty):
        import subprocess

        if not arg:
            print "qattach: Missing target name!"
            return

        pids = subprocess.check_output(['pidof', arg]).split()

        if len(pids) != 1:
            print subprocess.check_output(['ps'] + pids)
        else:
            try:
                gdb.execute("detach")
            except:
                pass
            gdb.execute("attach "+pids[0])

QAttach()

Saturday, May 28, 2011

CRC: A Paper On CRCs

Check out this CRC paper, it really makes it easier to understand CRC implementations.

This CRC web was created because of the popularity of a paper on CRC algorithms written by Ross Williams and posted to some Internet newsgroups on 19 August 1993. This paper is available here:

Here is the title and abstract of the paper:

A Painless Guide to CRC Error Detection Algorithms

This document explains CRCs (Cyclic Redundancy Codes) and their table-driven implementations in full, precise detail. Much of the literature on CRCs, and in particular on their table-driven implementations, is a little obscure. This document is an attempt to provide a clear and simple no-nonsense explanation of CRCs and to absolutely nail down every detail of the operation of their high-speed implementations. In addition to this, this document presents a parameterized model CRC algorithm called the "Rocksoft™ Model CRC Algorithm". The model algorithm can be parameterized to behave like most of the CRC implementations around, and so acts as a good reference for describing particular algorithms. A low-speed implementation of the model CRC algorithm is provided in the C programming language. Lastly there is a section giving two forms of high-speed table driven implementations, and providing a program that generates CRC lookup tables.