tttrace_objs.C 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. //%% (c) Copyright 1993, 1994 Hewlett-Packard Company
  24. //%% (c) Copyright 1993, 1994 International Business Machines Corp.
  25. //%% (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  26. //%% (c) Copyright 1993, 1994 Novell, Inc.
  27. //%% $XConsortium: tttrace_objs.C /main/3 1995/10/20 17:02:43 rswiston $
  28. /*
  29. * @(#)tttrace_objs.C 1.4 93/11/04
  30. *
  31. * Copyright (c) 1993 by Sun Microsystems, Inc.
  32. */
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <sys/uio.h>
  36. #include <unistd.h>
  37. #include <fcntl.h>
  38. #include <errno.h>
  39. #include "tttrace_objs.h"
  40. #include "util/tt_trace.h"
  41. _Tt_trace_optobj::_Tt_trace_optobj()
  42. {
  43. _msg_tracing = 1; // trace messages and calls by default
  44. _follow = 0; // follow off by default
  45. _api_calls = 1; // trace calls into API by default
  46. _all_attrs = 0; // short form by default
  47. _has_script_inline = 0;
  48. _has_script_filename = 0;
  49. _inline_or_filename = "version 1 ";
  50. _has_outfile = 0;
  51. _has_session = 2; // 1 -> -S option, 2 -> set by default
  52. _has_command = 0;
  53. _form = NO_FORM;
  54. for(int i = 0; i < MAXARGS; i++) {
  55. _cargv[i] = NULL;
  56. }
  57. }
  58. int
  59. _Tt_trace_optobj::getopts(int argc, char** argv)
  60. {
  61. int c; // command-line option char
  62. int i;
  63. extern char* optarg; // command-line option
  64. extern int optind; // option index into argv
  65. extern int opterr; // getopt error value
  66. _Tt_string tmpstr;
  67. _Tt_string tmppath;
  68. _Tt_string _tt_realpath(const _Tt_string&);
  69. _form = NO_FORM;
  70. // needed by the getopt call
  71. opterr = 0;
  72. while ((c = getopt(argc, argv, argstr)) != -1) {
  73. switch (c) {
  74. case '0':
  75. // Turn off msg tracing in <session> or run <command>
  76. // without message tracing
  77. if (_form == SCRIPT_FORM) return 1;
  78. _form = NOSCRIPT_FORM;
  79. _msg_tracing = 0;
  80. break;
  81. case 'F':
  82. // Follow children
  83. if (_form == SCRIPT_FORM) return 1;
  84. _form = NOSCRIPT_FORM;
  85. _follow = 1;
  86. break;
  87. case 'C':
  88. // Do not trace client calls into the TT API
  89. if (_form == SCRIPT_FORM) return 1;
  90. _form = NOSCRIPT_FORM;
  91. _api_calls = 0;
  92. break;
  93. case 'a':
  94. // verbose form
  95. if (_form == SCRIPT_FORM) return 1;
  96. _form = NOSCRIPT_FORM;
  97. _all_attrs = 1;
  98. break;
  99. case 'e':
  100. // command-line script
  101. if (_form == NOSCRIPT_FORM) return 1;
  102. _form = SCRIPT_FORM;
  103. if (!_has_script_filename) {
  104. _inline_or_filename = optarg;
  105. _has_script_inline = 1;
  106. }
  107. else {
  108. return 1;
  109. }
  110. break;
  111. case 'f':
  112. // script file
  113. if (_form == NOSCRIPT_FORM) return 1;
  114. _form = SCRIPT_FORM;
  115. if (!_has_script_inline) {
  116. struct stat sbuf;
  117. tmppath = optarg;
  118. tmpstr = _tt_realpath(tmppath);
  119. _inline_or_filename = tmpstr;
  120. if (stat(_inline_or_filename, &sbuf) != 0) {
  121. fprintf(stderr, "ttrace: %s: %s\n",
  122. (char *)_inline_or_filename,
  123. strerror(errno));
  124. return 2;
  125. }
  126. _has_script_filename = 1;
  127. }
  128. else {
  129. return 1;
  130. }
  131. break;
  132. case 'o':
  133. // outfile
  134. if (_form == SCRIPT_FORM) return 1;
  135. _form = NOSCRIPT_FORM;
  136. _outfile = optarg;
  137. _has_outfile = 1;
  138. break;
  139. case 'S':
  140. // XXX: Note that a Session_Trace message *always*
  141. // gets sent when this option is given, so we have
  142. // to do a tt_open somewhere, and it may as well be
  143. // here, so we can do a tt_default_session if
  144. // necessary.
  145. Tt_status tts;
  146. char* procid;
  147. _session = optarg;
  148. tts = tt_default_session_set(_session);
  149. if (tts != TT_OK) {
  150. fprintf(stderr, "tttrace: "
  151. "tt_default_session_set:\n%s\n",
  152. tt_status_message(tts));
  153. exit(1);
  154. }
  155. procid = tt_open();
  156. tts = tt_ptr_error(procid);
  157. if (tts != TT_OK) {
  158. fprintf(stderr, "tttrace: tt_open:\n%s\n",
  159. tt_status_message(tts));
  160. exit(1);
  161. }
  162. _has_session = 1;
  163. break;
  164. case '?':
  165. default:
  166. // error or help request.
  167. return 1;
  168. }
  169. }
  170. if (optind < argc) {
  171. // Command given after options
  172. if (_has_session == 2) {
  173. // Override the default
  174. _has_session = 0;
  175. }
  176. else if (_has_session == 1) {
  177. // Can't give session and command together
  178. return 1;
  179. }
  180. // command given after options
  181. _command = argv[optind++];
  182. _cargv[0] = (char *) _command;
  183. for (i = 1; optind < argc; ++i, ++optind) {
  184. _cargv[i] = argv[optind];
  185. }
  186. _cargv[i] = (char *) 0;
  187. _has_command = 1;
  188. } else if (_has_session == 2) {
  189. // default -- tttrace with no session or command args
  190. (void) tt_open();
  191. _session = tt_default_session();
  192. }
  193. if ((! _has_outfile) || (_outfile == "-")) {
  194. // We need to set up a FIFO to stuff the output
  195. // into, and give the FIFO name to the command/session
  196. _pipenm = tempnam(NULL, "trace");
  197. if (mkfifo(_pipenm, S_IWUSR|S_IRUSR) == -1) {
  198. fprintf(stderr, "tttrace: mkfifo(\"%s\"): %s\n",
  199. (char *)_pipenm, strerror(errno));
  200. exit(2);
  201. }
  202. }
  203. mkenvstr();
  204. if (_form == NO_FORM) {
  205. _has_script_inline = 1;
  206. _form = SCRIPT_FORM;
  207. }
  208. return 0;
  209. }
  210. int
  211. _Tt_trace_optobj::msg_tracing()
  212. {
  213. return _msg_tracing;
  214. }
  215. int
  216. _Tt_trace_optobj::follow()
  217. {
  218. return _follow;
  219. }
  220. int
  221. _Tt_trace_optobj::api_calls()
  222. {
  223. return _api_calls;
  224. }
  225. int
  226. _Tt_trace_optobj::all_attrs()
  227. {
  228. return _all_attrs;
  229. }
  230. int
  231. _Tt_trace_optobj::script(_Tt_string& script_string)
  232. {
  233. int ret = 0;
  234. if (!_msg_tracing) {
  235. // _msg_tracing is 1 by default, so if it's zero here, it
  236. // means the -0 option was given, so whatever else was
  237. // specified by the command line is overridden by this
  238. script_string = "states none";
  239. return 1;
  240. }
  241. if (_has_script_inline) {
  242. ret = 1;
  243. }
  244. else if (_has_script_filename) {
  245. ret = 2;
  246. }
  247. script_string = _inline_or_filename;
  248. return ret;
  249. }
  250. int
  251. _Tt_trace_optobj::outfile(_Tt_string& filename)
  252. {
  253. filename = _outfile;
  254. if (_has_outfile) {
  255. return 1;
  256. }
  257. else {
  258. return 0;
  259. }
  260. }
  261. int
  262. _Tt_trace_optobj::session(_Tt_string& session_string)
  263. {
  264. session_string = _session;
  265. if (_has_session) {
  266. return 1;
  267. }
  268. else {
  269. return 0;
  270. }
  271. }
  272. int
  273. _Tt_trace_optobj::command(_Tt_string& command_string)
  274. {
  275. command_string = _command;
  276. if (_has_command) {
  277. return 1;
  278. }
  279. else {
  280. return 0;
  281. }
  282. }
  283. char**
  284. _Tt_trace_optobj::cargv()
  285. {
  286. return _cargv;
  287. }
  288. int
  289. _Tt_trace_optobj::mkenvstr()
  290. {
  291. char *val = getenv(TRACE_SCRIPT);
  292. if ((val != 0) && (_form == NO_FORM)) {
  293. _envstr = val;
  294. if (_inline_or_filename[0] == '/' ||
  295. _inline_or_filename[0] == '.') {
  296. _form = NOSCRIPT_FORM; // filename
  297. }
  298. else {
  299. _form = SCRIPT_FORM; // inline script
  300. }
  301. return 1;
  302. }
  303. _envstr = TRACE_SCRIPT;
  304. _envstr = _envstr.cat("=");
  305. if (! _has_script_filename) {
  306. //
  307. // The script will be inline, instead of in a file
  308. //
  309. if ((! _has_outfile) || (_outfile == "-")) {
  310. //
  311. // No -o, so prepend "> pipe; " onto script
  312. //
  313. if (_pipenm.len() > 0) {
  314. _Tt_string output_cmd = "> ";
  315. output_cmd = output_cmd.cat(_pipenm).cat("; ");
  316. _inline_or_filename =
  317. output_cmd.cat( _inline_or_filename );
  318. }
  319. } else {
  320. //
  321. // -o option overrides ">" in script, so
  322. // append "; > outfile" to script
  323. //
  324. if (_outfile.len() > 0) {
  325. _Tt_string output_cmd = "> ";
  326. output_cmd = output_cmd.cat(_outfile);
  327. if (_inline_or_filename.len() > 0) {
  328. _inline_or_filename =
  329. _inline_or_filename.cat("; ");
  330. }
  331. _inline_or_filename =
  332. _inline_or_filename.cat( output_cmd );
  333. }
  334. }
  335. //
  336. } else {
  337. // XXX if both -o and -f, we need to read the file!
  338. }
  339. if (!_has_script_inline && !_has_script_filename) {
  340. if (! _msg_tracing) {
  341. _inline_or_filename =
  342. _inline_or_filename.cat("; states none");
  343. }
  344. if (_follow) {
  345. _inline_or_filename =
  346. _inline_or_filename.cat("; follow on");
  347. }
  348. if (!_api_calls) {
  349. _inline_or_filename =
  350. _inline_or_filename.cat("; functions none");
  351. }
  352. if (_all_attrs) {
  353. _inline_or_filename =
  354. _inline_or_filename.cat("; attributes all");
  355. }
  356. _has_script_inline = 1;
  357. }
  358. _envstr = _envstr.cat(_inline_or_filename);
  359. return 1;
  360. }
  361. int
  362. _Tt_trace_optobj::operation_mode()
  363. {
  364. if (_has_session)
  365. return SESSION_TRACE;
  366. else if (_has_command) // _has_command
  367. return FORK_COMMAND;
  368. else
  369. return -1; // error
  370. }
  371. _Tt_string
  372. _Tt_trace_optobj::envstr()
  373. {
  374. return _envstr;
  375. }
  376. _Tt_string&
  377. _Tt_trace_optobj::pipe_name()
  378. {
  379. return _pipenm;
  380. }