Class: Dtrace::Dof::File

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/dtrace/dof/file.rb,
ext/dof/dof_api.c

Overview

Ruby-Dtrace © 2008 Chris Andrews <[email protected]>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFile

Returns a new instance of File.



10
11
12
# File 'lib/dtrace/dof/file.rb', line 10

def initialize
  @sections = []
end

Instance Attribute Details

#sectionsObject

Returns the value of attribute sections.



8
9
10
# File 'lib/dtrace/dof/file.rb', line 8

def sections
  @sections
end

Instance Method Details

#<<(data) ⇒ Object

Appends the given string to the DOF file.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'ext/dof/file.c', line 56

VALUE dof_file_append(VALUE self, VALUE data)
{
  dof_file_t *file;
  Data_Get_Struct(self, dof_file_t, file);

  if ((file->offset + RSTRING(data)->len) > file->len) {
    rb_raise(eDtraceDofException, "DOF allocation insufficient: %d > %d",
	     (file->offset + RSTRING(data)->len), file->len);
    return Qnil;
  }
  
  memcpy((file->dof + file->offset), RSTRING(data)->ptr, RSTRING(data)->len);
  file->offset += RSTRING(data)->len;
}

#addrObject

Returns the memory address of the DOF file.



72
73
74
75
76
77
78
79
80
81
# File 'ext/dof/file.c', line 72

VALUE dof_file_addr(VALUE self)
{
  dof_file_t *file;
  Data_Get_Struct(self, dof_file_t, file);
  if (file->dof == NULL) {
    rb_raise(eDtraceDofException, "must allocate DOF buffer before calling addr");
    return Qnil;
  }
  return INT2FIX(file->dof);
}

#allocate(size) ⇒ Object

:nodoc:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'ext/dof/file.c', line 40

VALUE dof_file_allocate_dof(VALUE self, VALUE size)
{
  dof_file_t *file;
  Data_Get_Struct(self, dof_file_t, file);

  file->len = FIX2INT(size);
  file->dof = (char *)ALLOC_N(char, file->len);
  if (file->dof == NULL) {
    rb_raise(eDtraceDofException, "failed to allocate %d bytes for DOF", file->len);
    return Qnil;
  }

  return Qnil;
}

#dataObject

Returns the DOF itself.



84
85
86
87
88
89
# File 'ext/dof/file.c', line 84

VALUE dof_file_data(VALUE self)
{
  dof_file_t *file;
  Data_Get_Struct(self, dof_file_t, file);
  return rb_str_new(file->dof, file->offset);
}

#generateObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dtrace/dof/file.rb', line 14

def generate
  hdr = Dtrace::Dof::Header.new
  hdr.secnum = @sections.length
  filesz = hdr.hdrlen
  loadsz = filesz
  dof_version = 1

  @sections.each do |s|
    # Presence of is_enabled probes forces DOF version 2.
    if s.section_type == DOF_SECT_PRENOFFS
      dof_version = 2
    end
    
    length = s.generate
    s.offset = filesz

    pad = 0
    if s.align > 1
      i = s.offset.to_f % s.align
      if i > 0
        pad = (s.align - i).to_i
        s.offset = pad + s.offset
        s.pad = "\000" * pad
      end
    end

    s.size = length

    loadsz += (s.size + pad) if (s.flags & 1) == 1 # DOF_SECF_LOAD
    filesz += (s.size + pad)

  end
  
  hdr.loadsz = loadsz
  hdr.filesz = filesz
  hdr.dof_version = dof_version

  self << hdr.generate
  
  @sections.each do |s|
    self << s.generate_header
  end
    
  @sections.each do |s|
    self << s.pad if s.pad
    self << s.dof
  end

end