Module: StraceArgs

Defined in:
lib/straceargs.rb

Overview

Parse the arguments of an Strace call These calls all follow the same pattern. They are given a string as input and parse as much as they can off the front of it, modifying it as they go and returning the parsed version.

Constant Summary collapse

STRUCT =

gobble up the largest matching braces

/\A(?<b>{([^{}]|\g<b>)*}),?\s*/

Class Method Summary collapse

Class Method Details

.afds!(args) ⇒ Object



25
26
27
28
29
30
# File 'lib/straceargs.rb', line 25

def self.afds! args
    raise "parse error: #{args}" unless args.slice!(/^(\[.*?\]|NULL),?\s*/)
    afd = $~[1]
    return [] if afd.start_with? 'NULL'
    afd.slice(1..-2).split(/\s*,\s*/).map(&:to_i)
end

.define!(args) ⇒ Object



37
38
39
40
# File 'lib/straceargs.rb', line 37

def self.define! args
    raise "parse error: #{args}" unless args.slice! /^(\w+),?\s*/
    return $~[1].to_sym
end

.epollevents!(args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/straceargs.rb', line 61

def self.epollevents! args
    raise "parse error: #{args}" unless args.slice! STRUCT
    evs = $~[1].slice(1..-2)
    rex = /{(.*?),\s{u32=(\d+),\su64=(\d+)}},?\s*/
    eva = []
    while ev = evs.slice!(rex)
        eva.push $~[3].to_i
    end
    raise "parse leftover #{evs}" unless evs.empty?
    return eva
end

.fd!(args) ⇒ Object



20
21
22
23
# File 'lib/straceargs.rb', line 20

def self.fd! args
    raise "parse error: #{args}" unless args.slice! /^(\d+),?\s*/
    $~[1].to_i
end

.fds(args) ⇒ Object



11
12
13
# File 'lib/straceargs.rb', line 11

def self.fds args
    args.slice(1..-2).split(/},{/).map{|m| /^fd=(\d+)/ =~ m; $1.to_i}
end

.flags!(args) ⇒ Object



73
74
75
76
77
# File 'lib/straceargs.rb', line 73

def self.flags! args
    raise "parse error: #{args}" unless args.slice! /^\w+=([^,]+),\s*/
    flags = $~[1].split('|').map{|f| f.to_sym}
    return flags
end

.name!(args) ⇒ Object



32
33
34
35
# File 'lib/straceargs.rb', line 32

def self.name! args
    raise "parse error: #{args}" unless args.slice! /^"(.*?)",?\s*/
    return $~[1]
end

.nextcomma!(args) ⇒ Object



15
16
17
18
# File 'lib/straceargs.rb', line 15

def self.nextcomma! args
    raise "parse error: #{args}" unless args.slice! /^([^,]+),?\s*/
    $~[1]
end

.sockaddr!(args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/straceargs.rb', line 42

def self.sockaddr! args
    raise "parse error: #{args}" unless args.slice! STRUCT
    soa = $~[1].slice(1..-2)
    if /^
        sa_family=AF_INET,\s*
        sin_port=htons\((?<port>\d+)\),\s*
        sin_addr=inet_addr\("(?<host>.*)"\)
        /x =~ soa
        return "#{host}:#{port}"
    elsif /^
        sa_family=AF_FILE,\s*
        path="(?<path>.*)"
        /x =~ soa
        return "file:#{path}"
    else
        raise "Invalid sockaddr format: #{soa}"
    end
end