1/* Copyright (C) 2021 Free Software Foundation, Inc.
2   Contributed by Oracle.
3
4   This file is part of GNU Binutils.
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 3, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, 51 Franklin Street - Fifth Floor, Boston,
19   MA 02110-1301, USA.  */
20
21#ifndef _COLLECT_H
22#define _COLLECT_H
23
24#include <Application.h>
25
26extern "C"
27{
28  typedef void (*SignalHandler)(int);
29}
30
31class Coll_Ctrl;
32class Elf;
33
34#define MAXLABELS       10      /* maximum number of -C arguments */
35#define STDEBUFSIZE     24000
36
37enum { MAX_LD_PRELOAD_TYPES = 3 };
38
39struct Process
40{
41  Process (pid_t _pid) : pid (_pid) { }
42  pid_t pid;
43};
44
45struct DtraceTool
46{
47  DtraceTool (char*);
48  ~DtraceTool ();
49
50  char *name;       // Tool name as specified by -D flag
51  char *params;     // Tool parameters
52  char *dfile;      // Extracted d-script
53  char *mfile;      // Extracted metadata file
54  char *ofile;      // Output file
55  pid_t pid;
56};
57
58// collect object
59class collect : Application
60{
61public:
62  collect (int argc, char *argv[], char **envp);
63  virtual ~collect ();
64  void start (int argc, char *argv[]);
65  void  writeStr (int f, const char *buf);
66
67  // the collector control class
68  Coll_Ctrl *cc;
69
70private:
71  enum Exec_status
72  {
73    EXEC_OK = 0, // found as runnable executable
74    EXEC_ELF_NOSHARE, // found, but built unshared
75    EXEC_IS_JAR, // found as a .jar file
76    EXEC_IS_CLASS, // found as a .class file but name missing .class
77    EXEC_IS_CLASSCLASS, // found as a .class file with explicit .class
78    EXEC_OPEN_FAIL, // could not be opened
79    EXEC_ELF_LIB, // internal error: bad elf library
80    EXEC_ELF_HEADER, // executable, with bad ELF header
81    EXEC_ELF_ARCH, // executable, but unrunnable architecture
82    EXEC_ISDIR, // a directory, not a file
83    EXEC_NOT_EXEC, // a file, but not executable
84    EXEC_NOT_FOUND // a directory, not a file
85  };
86
87  // override methods in base class
88  void usage ();
89  void short_usage ();
90  void show_hwc_usage ();
91  int check_args (int argc, char *argv[]);
92  void check_target (int, char **);
93  Exec_status check_executable (char *);
94  Exec_status check_executable_arch (Elf *);
95  char *status_str (Exec_status, char *);
96  int do_flag (const char *);
97  char *find_java (void);
98  char *java_path;
99  char *java_how;
100  int putenv_libcollector ();
101  int putenv_libcollector_ld_audits ();
102  int putenv_libcollector_ld_preloads ();
103  int putenv_libcollector_ld_misc ();
104  void add_ld_preload (const char *lib);
105  int putenv_ld_preloads ();
106  int putenv_memso ();
107  int env_strip (char *env, const char *str);
108  int putenv_purged_ld_preloads (const char *var);
109  int putenv_append (const char *var, const char *val);
110  void get_count_data ();
111  void prepare_dbx ();
112  int traceme (const char *file, char *const argv[]);
113  int checkflagterm (const char *);
114  void dupflagseen (char);
115  void dupflagseen (const char *);
116  void validate_config (int);
117  void validate_java (const char *, const char *, int);
118  int set_output ();
119  void reset_output ();
120
121  /* Logging warning messages */
122  char **collect_warnings;
123  int collect_warnings_idx;
124  void warn_open ();
125  void warn_close ();
126  void warn_write (const char *format, ...);
127  void warn_comment (const char *kind, int num, char *s = NULL, int len = 0);
128  char *warnfilename;
129  FILE *warn_file;
130
131  /* MPI experiment handling */
132  void setup_MPI_expt ();   /* the founder experiment */
133  void write_MPI_founder_log ();
134  void close_MPI_founder_log (int, int);
135  void spawn_MPI_job (); /* run the MPI job */
136  void copy_collect_args (char ***);   /* copy collect args for an MPI target */
137  int disabled;
138  int jseen_global;         /* if -j flag was seen */
139  int verbose;
140  bool mem_so_me;           /* if T, preload mem.so, not libcollector */
141  int origargc;
142  char **arglist;
143  char **origargv;
144  char **origenvp;
145  int targ_index;           // index of name of target in origargv
146  bool is_64;
147  int nargs;
148  int njargs;
149  char *jargs;
150  int nlabels;
151  char *label[MAXLABELS];
152  char *sp_preload_list[MAX_LD_PRELOAD_TYPES + 1];  // +1 for NULL termination
153  char *sp_libpath_list[MAX_LD_PRELOAD_TYPES + 1];  // +1 for NULL termination
154};
155
156#endif /* ! _COLLECT_H */
157