HPCToolkit
InputFile.cpp
Go to the documentation of this file.
1 // * BeginRiceCopyright *****************************************************
2 //
3 // $HeadURL$
4 // $Id$
5 //
6 // --------------------------------------------------------------------------
7 // Part of HPCToolkit (hpctoolkit.org)
8 //
9 // Information about sources of support for research and development of
10 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
11 // --------------------------------------------------------------------------
12 //
13 // Copyright ((c)) 2002-2019, Rice University
14 // All rights reserved.
15 //
16 // Redistribution and use in source and binary forms, with or without
17 // modification, are permitted provided that the following conditions are
18 // met:
19 //
20 // * Redistributions of source code must retain the above copyright
21 // notice, this list of conditions and the following disclaimer.
22 //
23 // * Redistributions in binary form must reproduce the above copyright
24 // notice, this list of conditions and the following disclaimer in the
25 // documentation and/or other materials provided with the distribution.
26 //
27 // * Neither the name of Rice University (RICE) nor the names of its
28 // contributors may be used to endorse or promote products derived from
29 // this software without specific prior written permission.
30 //
31 // This software is provided by RICE and contributors "as is" and any
32 // express or implied warranties, including, but not limited to, the
33 // implied warranties of merchantability and fitness for a particular
34 // purpose are disclaimed. In no event shall RICE or contributors be
35 // liable for any direct, indirect, incidental, special, exemplary, or
36 // consequential damages (including, but not limited to, procurement of
37 // substitute goods or services; loss of use, data, or profits; or
38 // business interruption) however caused and on any theory of liability,
39 // whether in contract, strict liability, or tort (including negligence
40 // or otherwise) arising in any way out of the use of this software, even
41 // if advised of the possibility of such damage.
42 //
43 // ******************************************************* EndRiceCopyright *
44 
45 //***************************************************************************
46 //
47 // File:
48 //
49 // Purpose:
50 //
51 // Description:
52 //
53 //
54 //***************************************************************************
55 
56 
57 //******************************************************************************
58 // system includes
59 //******************************************************************************
60 
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 #include <err.h>
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <stdlib.h>
67 #include <unistd.h>
68 
69 
70 //******************************************************************************
71 // local includes
72 //******************************************************************************
73 
75 
76 #include "InputFile.hpp"
77 #include "ElfHelper.hpp"
78 #include "Fatbin.hpp"
79 
80 
81 //******************************************************************************
82 // private operations
83 //******************************************************************************
84 
85 static size_t
86 file_size(int fd)
87 {
88  struct stat sb;
89  int retval = fstat(fd, &sb);
90  if (retval == 0 && S_ISREG(sb.st_mode)) {
91  return sb.st_size;
92  }
93  return 0;
94 }
95 
96 
97 // Automatically restart short reads.
98 // This protects against EINTR.
99 //
100 static size_t
101 read_all(int fd, void *buf, size_t count)
102 {
103  ssize_t ret;
104  size_t len;
105 
106  len = 0;
107  while (len < count) {
108  ret = read(fd, ((char *) buf) + len, count - len);
109  if (ret == 0 || (ret < 0 && errno != EINTR)) {
110  break;
111  }
112  if (ret > 0) {
113  len += ret;
114  }
115  }
116 
117  return len;
118 }
119 
120 
121 //******************************************************************************
122 // interface oeprations
123 //******************************************************************************
124 
125 
126 bool
128 (
129  std::string &filename
130 )
131 {
132  this->filename = filename;
133 
134  int file_fd = open(filename.c_str(), O_RDONLY);
135 
136  if (file_fd < 0) {
137  DIAG_EMsg("unable to open input file: " << filename);
138  return false;
139  }
140 
141  size_t f_size = file_size(file_fd);
142 
143  if (f_size == 0) {
144  DIAG_EMsg("empty input file: " << filename);
145  return false;
146  }
147 
148  char *file_buffer = (char *) malloc(f_size);
149 
150  if (file_buffer == 0) {
151  DIAG_EMsg("unable to allocate file buffer of " << f_size << " bytes");
152  return false;
153  }
154 
155  size_t bytes = read_all(file_fd, file_buffer, f_size);
156 
157  if (f_size != bytes) {
158  DIAG_EMsg("read only " << bytes << " bytes of "
159  << f_size << " bytes from file " << filename);
160  return false;
161  }
162 
163  close(file_fd);
164 
165  ElfFile *elfFile = new ElfFile;
166  bool result = elfFile->open(file_buffer, f_size, filename);
167 
168  if (result) {
169  filevector = new ElfFileVector;
170  filevector->push_back(elfFile);
171  findCubins(elfFile, filevector);
172  } else {
173  DIAG_EMsg("not an ELF binary: " << filename);
174  delete elfFile;
175  }
176 
177  return result;
178 }
#define DIAG_EMsg(...)
Definition: diagnostics.h:251
Definition: fmt.c:108
bool open(char *_memPtr, size_t _memLen, std::string _fileName)
Definition: ElfHelper.cpp:96
bool findCubins(ElfFile *elfFile, ElfFileVector *elfFileVector)
Definition: Fatbin.cpp:246
ssize_t MONITOR_EXT_WRAP_NAME() read(int fd, void *buf, size_t count)
Definition: io-over.c:152
void *MONITOR_EXT_WRAP_NAME() malloc(size_t bytes)
bool openFile(std::string &filename)
Definition: InputFile.cpp:128
static size_t read_all(int fd, void *buf, size_t count)
Definition: InputFile.cpp:101
static size_t file_size(int fd)
Definition: InputFile.cpp:86