Skip to content

Instantly share code, notes, and snippets.

@jpauli
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpauli/7b10cef380c9d3cfcedb to your computer and use it in GitHub Desktop.
Save jpauli/7b10cef380c9d3cfcedb to your computer and use it in GitHub Desktop.
#! /usr/bin/env php
<?php
if ($argc != 2) {
exit(sprintf("Usage : %s <pid>\n", $argv[0]));
}
if (!file_exists("/proc/1/status")) {
exit("/proc/1/status does not exist, what OS are you running ?\n");
}
static $sigmap = [ 1 => 'SIGHUP', 2 => 'SIGINT', 3 => 'SIGQUIT', 4 => 'SIGILL', 5 => 'SIGTRAP',
6 => 'SIGABRT', 7 => 'SIGBUS', 8 => 'SIGFPE', 9 => 'SIGKILL', 10 => 'SIGUSR1',
11 => 'SIGSEGV', 12 => 'SIGUSR2', 13 => 'SIGPIPE', 14 => 'SIGALRM', 15 => 'SIGTERM',
16 => 'SIGSTKFLT', 17 => 'SIGCHLD', 18 => 'SIGCONT', 19 => 'SIGSTOP', 20 => 'SIGTSTP',
21 => 'SIGTTIN', 22 => 'SIGTTOU', 23 => 'SIGURG', 24 => 'SIGXCPU', 25 => 'SIGXFSZ',
26 => 'SIGVTALRM', 27 => 'SIGPROF', 28 => 'SIGWINCH', 29 => 'SIGIO', 30 => 'SIGPWR',
31 => 'SIGSYS', 32 => 'SIGCANCEL', 33 => 'SIGSETXID', 34 => 'SIGRTMIN', 35 => 'SIGRTMIN+1', /* 32 and 33 are glibc signals (https://sourceware.org/git/?p=glibc.git;a=blob;h=fa89cbf44a3e0cd23856d980baa9def8b1cc358d;hb=75f0d3040a2c2de8842bfa7a09e11da1a73e17d0#l307) */
36 => 'SIGRTMIN+2', 37 => 'SIGRTMIN+3',
38 => 'SIGRTMIN+4', 39 => 'SIGRTMIN+5', 40 => 'SIGRTMIN+6', 41 => 'SIGRTMIN+7', 42 => 'SIGRTMIN+8',
43 => 'SIGRTMIN+9', 44 => 'SIGRTMIN+10', 45 => 'SIGRTMIN+11', 46 => 'SIGRTMIN+12', 47 => 'SIGRTMIN+13',
48 => 'SIGRTMIN+14', 49 => 'SIGRTMIN+15', 50 => 'SIGRTMAX-14', 51 => 'SIGRTMAX-13', 52 => 'SIGRTMAX-12',
53 => 'SIGRTMAX-11', 54 => 'SIGRTMAX-10', 55 => 'SIGRTMAX-9', 56 => 'SIGRTMAX-8', 57 => 'SIGRTMAX-7',
58 => 'SIGRTMAX-6', 59 => 'SIGRTMAX-5', 60 => 'SIGRTMAX-4', 61 => 'SIGRTMAX-3', 62 => 'SIGRTMAX-2',
63 => 'SIGRTMAX-1', 64 => 'SIGRTMAX'];
$content = @file_get_contents($file = sprintf("/proc/%d/status", $argv[1]));
if (!$content) {
exit(sprintf("Pid %d does not exist\n", $argv[1]));
}
preg_match_all('/Sig([A-Z][a-z]{2}):\s(\w{16})/', $content, $pieces);
preg_match("/Name:\s*(\w+)/", $content, $progname);
unset($content);
printf("--Signal map for %s (pid %d)--\n\n", $progname[1], $argv[1]);
foreach($pieces[1] as $index => $sig) {
$sigtext = '';
$binary = strrev(sprintf("%064s", base_convert($pieces[2][$index], 16, 2)));
for ($i = 0; $i < 64; $i++) {
if ($binary[$i] == '1') {
$sigtext .= sprintf("%s - ", $sigmap[$i+1]);
}
}
if ($sigtext) {
echo "$sig: $sigtext\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment