summaryrefslogtreecommitdiff
path: root/apps/soreduce/SO_Group.cpp
blob: b00cdeeb0199f5d84d671ab84580b0bb3ea5ea66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

#include <ace/OS.h>
#include <ace/Log_Msg.h>

#include <ace/Process.h>
#include <ace/Pipe.h>

#include "Library.h"
#include "SO_Group.h"

ACE_RCSID(src, SO_Group, "$Id$")


SO_Group::SO_Group ()
  : undef_wrapper_ ("nothing"),
    undefs_(undef_wrapper_.imports()),
    libs_ (0),
    max_libs_ (30),
    num_libs_(0)
{
  libs_ = new Library*[max_libs_];
}
           
SO_Group::~SO_Group ()
{
  for (int i = 0; i < num_libs_; delete libs_[i++]);    
  delete [] libs_;
}

void
SO_Group::add_executable (const char * path)
{
  ACE_Process proc;
  ACE_Process_Options opts;

  ACE_HANDLE pipe[2];
  ACE_Pipe io(pipe);

  opts.set_handles (ACE_STDIN,pipe[1]);

  ACE_ASSERT(opts.command_line ("ldd %s",path) == 0);

  proc.spawn (opts);
  if (ACE_OS::close(pipe[1]) == -1)
    ACE_DEBUG ((LM_DEBUG, "%p\n", "close"));
  opts.release_handles();

  const int max_line_length = 1000;
  char line[max_line_length];

  while (1) {
    ACE_OS::memset (line,0,max_line_length);
    int len = 0;
    int nread = 0;
    int bogus = 0;
    // skip initial whitespace
    while ((nread = ACE_OS::read(pipe[0],line,1)) == 1 &&
           (*line == ' ' || *line == '\t'));
    
    if (nread != 1) 
      break;

    // read the library name
    len = 1;
    while ((nread = ACE_OS::read(pipe[0],line + len,1)) == 1 &&
           (line[len] != ' '))      
      if (! bogus && ++len == max_line_length)
        {
          bogus = 1;
          break;
        }
    if (nread != 1 || bogus) 
      break;
    line[len] = 0;
    char * dot = ACE_OS::strchr (line,'.');
    if (dot)
      *dot = 0;
    char * libname = line + 3; // skip over "lib"

    // check to see if this is a new library
    int found = 0;
    for (int i = 0; !found && i < num_libs_; i++)
      found = (libs_[i]->name() == libname);

    if (!found) {
      Library *nlib = new Library(libname);
      ACE_OS::memset (line,0,max_line_length);

      // skip over '=> '
      if (ACE_OS::read(pipe[0],line,3) != 3)
        break;

      // get library path
      len = 0;
      while ((nread = ACE_OS::read(pipe[0],line + len,1)) == 1 &&
             (line[len] != ' '))      
        if (! bogus && ++len == max_line_length)
          {
            bogus = 1;
            break;
          }
      if (nread != 1 || bogus) 
        break;
      line[len] = 0;
      nlib->set_path (line);
      libs_[num_libs_++] = nlib;
      ACE_ASSERT (num_libs_ < max_libs_); // grow max libs?
    }
    // skip the rest of the line
    while ((nread = ACE_OS::read(pipe[0],line,1)) == 1 && *line != '\n');
    if (nread != 1) 
      break;
  }
  proc.wait ();
  close (pipe[0]);

  undef_wrapper_.add_source(path,1);
  // now do the ldd, iterate over the results to add new libs, etc.
}

void
SO_Group::analize ()
{
  for (int passcount = 0; undefs_.modified(); passcount++) {
    ACE_DEBUG ((LM_DEBUG,"pass %d, undef count = %d\n",
                passcount,undefs_.size()));
    for (int i = 0; i < num_libs_; libs_[i++]->resolve(undefs_));
  }
}

void
SO_Group::write_results()
{
  for (int i = 0; i < num_libs_; libs_[i++]->write_export_list(1));
}

void
SO_Group::load_modules()
{
  for (int i = 0; i < num_libs_; libs_[i++]->load_modules());
}

void
SO_Group::list_libs()
{
  ACE_DEBUG ((LM_DEBUG,"Libs subject to analysis:\n"));
  for (int i = 0; i < num_libs_; i++) {
    if (libs_[i]->has_modules())
      ACE_DEBUG ((LM_DEBUG,"  %s\n", libs_[i]->name().c_str()));
  }
}