summaryrefslogtreecommitdiffstats
path: root/main.cxx
blob: 4ab4e0bd90467a8e002c1daabafabeedb0443fb7 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// systemtap translator driver
// Copyright (C) 2005 Red Hat Inc.
//
// This file is part of systemtap, and is free software.  You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.

#include "config.h"
#include "staptree.h"
#include "parse.h"
#include "elaborate.h"
#include "translate.h"

#include <iostream>
#include <fstream>
#include <sstream>

extern "C" {
#include <glob.h>
#include <unistd.h>
}

using namespace std;


void usage ()
{
  cerr << "SystemTap translator "
       << "(version " << VERSION << " built " << DATE << ")" << endl;
  cerr << "Copyright (C) 2005 Red Hat, Inc." << endl;
  cerr << "This is free software; see the source for copying conditions." << endl;
  cerr << endl;
  cerr << "Usage: stap [options] FILE [ARGS ...]  \tRun script files." << endl;
  cerr << "   or: stap [options] -e PROGRAM [ARGS ...]\tRun script." << endl;
  cerr << endl;
  cerr << "Arguments:" << endl;
  cerr << "   --\tNo more options after this" << endl;
  cerr << "   -p NUM\tStop after pass NUM 1-3" << endl;
  cerr << "         \t(parse, elaborate, translate)" << endl;
  cerr << "   -I DIR\tLook in DIR for additional .stp script files." << endl;
  cerr << "   -o FILE\tSend translator output to file instead of stdout." << endl;
  // XXX: other options:
  // -s: safe mode
  // -d: dump safety-related external references 

  exit (0);
}


int
main (int argc, char * const argv [])
{
  int last_pass = 3; // -p NUM
  string cmdline_script; // -e PROGRAM
  string script_file; // FILE
  bool have_script = false;
  string output_file; // -o FILE
  vector<string> include_path; // -I DIR
  vector<string> args; // ARGS
  while (true)
    {
      int grc = getopt (argc, argv, "p:I:e:o:");
      if (grc < 0)
        break;
      switch (grc)
        {
        case 'p':
          last_pass = atoi (optarg);
          if (last_pass < 1 || last_pass > 3)
            {
              cerr << "Invalid pass number." << endl;
              usage ();
            }
          break;

        case 'I':
          include_path.push_back (string (optarg));
          break;

        case 'e':
	  if (have_script)
	    usage ();
          cmdline_script = string (optarg);
          have_script = true;
          break;

        case 'o':
	  if (output_file != "")
	    usage ();
          output_file = string (optarg);
          break;

        case '?':
        case 'h':
        default:
          usage ();
        }
    }

  for (int i = optind; i < argc; i++)
    {
      if (! have_script)
        {
          script_file = string (argv[i]);
          have_script = true;
        }
      else
        args.push_back (string (argv[i]));
    }

  // need a user file
  if (! have_script)
    usage();

  // arguments parsed; get down to business

  int rc = 0;
  systemtap_session s;
  if (output_file != "")
    s.op = new translator_output (output_file);
  else
    s.op = new translator_output (cout);


  // PASS 1a: PARSING USER SCRIPT
  // XXX: pass args vector, so parser (or lexer?) can substitute
  // $1..$NN with actual arguments
  if (script_file == "-")
    s.user_file = parser::parse (cin);
  else if (script_file != "")
    s.user_file = parser::parse (script_file);
  else
    {
      istringstream ii (cmdline_script);
      s.user_file = parser::parse (ii);
    }
  if (s.user_file == 0)
    // syntax errors already printed
    rc ++;

  // PASS 1b: PARSING LIBRARY SCRIPTS
  for (unsigned i=0; i<include_path.size(); i++)
    {
      glob_t globbuf;
      string dir = include_path[i] + "/*.stp";
      int r = glob(dir.c_str (), 0, NULL, & globbuf);
      if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
        rc ++;
      // GLOB_NOMATCH is acceptable

      for (unsigned j=0; j<globbuf.gl_pathc; j++)
        {
          stapfile* f = parser::parse (globbuf.gl_pathv[j]);
          if (f == 0)
            rc ++;
          else
            s.library_files.push_back (f);
        }

      globfree (& globbuf);
    }

  if (last_pass == 1 && rc == 0)
    {
      s.op->line() << "# parse tree dump";
      s.user_file->print (s.op->newline());
      for (unsigned i=0; i<s.library_files.size(); i++)
	s.library_files[i]->print (s.op->newline());
    }

  // PASS 2: ELABORATION
  if (rc == 0 && last_pass > 1)
    rc = semantic_pass (s);
  if (last_pass == 2 && rc == 0)
    {
      s.op->line() << "# globals";
      for (unsigned i=0; i<s.globals.size(); i++)
	{
	  vardecl* v = s.globals[i];
	  v->printsig (s.op->newline());
	}

      s.op->newline() << "# functions";
      for (unsigned i=0; i<s.functions.size(); i++)
	{
	  functiondecl* f = s.functions[i];
	  f->printsig (s.op->newline());
	}

      s.op->newline() << "# probes";
      for (unsigned i=0; i<s.probes.size(); i++)
	{
	  derived_probe* p = s.probes[i];
	  p->printsig (s.op->newline());
	}
    }

  // PASS 3: TRANSLATION
  if (rc == 0 && last_pass > 2)
    rc = translate_pass (s);

  delete s.op;

  return rc;
}