0

Is there any indicator I can look for to know whether a function performs I/O operations? The main target platform is Linux (Ubuntu, or some variant of it).

The most obvious way is to have a catalog of such functions/libraries, and check if the function is present in it. However, I want to know how could I build such a catalog in the first place.

For instance, printf eventually writes to the standard output, so it would count. A function that reads from a USB port would also count. But is there anything they have in common, even if I have to dig to the lowest level?


This may not matter to the question, but, ideally, there would be some pattern to these functions that I could exploit, so that they can be identified automatically with a tool. I am just not sure what low-level instructions to look for.

afsantos
  • 5,178
  • 4
  • 30
  • 54
  • 1
    Well, I think You should analyze asm for some sort of syscalls, but what exactly is hard question. It depends on Your OS obviously. Ideally You should create a call tree for every function and find which branches contain the syscall You are looking for – bartop Jul 27 '18 at 14:23
  • I think that any answer is going to be highly dependent on your target platform. Can you clarify what sort of environment you're building for? Windows, Unix, smartphones, embedded devices...? – Tim Randall Jul 27 '18 at 14:25
  • @Ron ideally, any IO, but I would prioritize USB interfaces in this case. – afsantos Jul 27 '18 at 14:25
  • 1
    Sounds like your question is not related to C++ per se but some OS specific interface / function. – Ron Jul 27 '18 at 14:26
  • @TimRandall Preferably Unix. I clarified that in the question. – afsantos Jul 27 '18 at 14:29
  • @Ron You are probably right. At a higher level, I would like to identify C++ functions with I/O side effects, but how to do it might have nothing to do with C++ itself. – afsantos Jul 27 '18 at 14:30
  • Does writing to a string (e.g. `sprintf()`, `std::ostringstream`) count as doing output? The reason I ask is that, internally, such functions or objects use the same sort of logic (e.g. to format data for output, writing to a buffer) as their counterparts that actually write to a file or device - the only difference is that their output goes to a different destination. – Peter Jul 27 '18 at 15:18
  • @Peter I would not count writing to strings, personally, but I would not mind a solution that includes those as well, if it makes the problem easier in any way. – afsantos Jul 27 '18 at 15:42

1 Answers1

2

Standard C++ of course has no functions to read from USB, but let's ignore that detail for a moment.

The question which functions perform "real" I/O can be approximated in Standard C++ by asking which library functions have observable side effects. That points directly to the set of Input/Output headers, and of course nowadays there's also <filesystem>.

(Copying from C++reference:)

<iosfwd>    forward declarations of all classes in the input/output library
<ios>       std::ios_base class, std::basic_ios class template and several typedefs
<istream>   std::basic_istream class template and several typedefs
<ostream>   std::basic_ostream, std::basic_iostream class templates and several typedefs
<iostream>  several standard stream objects
<fstream>   std::basic_fstream, std::basic_ifstream, std::basic_ofstream class templates and several typedefs
<sstream>   std::basic_stringstream, std::basic_istringstream, std::basic_ostringstream class templates and several typedefs
<syncstream> (since C++20)  std::basic_osyncstream, std::basic_syncbuf, and typedefs
<strstream> (deprecated)    std::strstream, std::istrstream, std::ostrstream
<iomanip>   Helper functions to control the format of input and output
<streambuf> std::basic_streambuf class template
<cstdio>    C-style input-output functions
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Yes, I know that it's unlikely that `ios_base::ios_base` does any actual I/O, or ` std::istringstream::istringstream`. And of course swapping to disk can be triggered by any use of memory, including stack memory. So this list is only official and not necessarily meaningful. – MSalters Jul 27 '18 at 15:04
  • I realize that my question might not have anything to do with C++, at the bottom of it, so your answer is probably the closest thing I can get. Thank you for the pointers, those might help direct future search. – afsantos Jul 27 '18 at 16:40