startstop.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon May 10 19:10:29 CEST 2004 startstop.cxx
3 
4  startstop.cxx - description
5  -------------------
6  begin : Mon May 10 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
45 #include "../rtt-config.h"
46 #include <os/startstop.h>
47 #include "os/MainThread.hpp"
48 #include "os/StartStopManager.hpp"
49 #include "../internal/GlobalEngine.hpp"
50 #include "../types/GlobalsRepository.hpp"
51 #include "../types/TypekitRepository.hpp"
52 
53 #ifdef OROPKG_OS_THREAD_SCOPE
54 # include <boost/scoped_ptr.hpp>
55 # include "../extras/dev/DigitalOutInterface.hpp"
56  using namespace RTT;
57 #elif defined(HAVE_LTTNG_UST) && defined(OROPKG_OS_GNULINUX)
58 #define TRACEPOINT_DEFINE
59 #define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
61 #endif
62 
63 #ifdef OS_HAVE_IOSTREAM
64 #include <iostream>
65 using namespace std;
66 #else
67 #include <cstdio>
68 #endif
69 
70 #include "../Logger.hpp"
71 #include "TimeService.hpp"
72 
73 using namespace RTT;
74 using namespace RTT::os;
75 
76 static int os_argc_arg;
77 static char** os_argv_arg;
78 
79 int __os_init(int argc, char** argv )
80 {
81 #ifdef OS_HAVE_MANUAL_CRT
82  DO_GLOBAL_CTORS();
83 #endif
84 
85  os_argc_arg = argc;
86  os_argv_arg = argv;
87 
89  Logger::log() << Logger::Debug << "MainThread started." << Logger::endl;
90 
91  Logger::log() << Logger::Debug << "Starting StartStopManager." << Logger::endl;
92  int ret = os::StartStopManager::Instance()->start() ? 0 : 1;
93 
94 #ifdef OROPKG_OS_THREAD_SCOPE
95  unsigned int bit = 0;
96 
97  boost::scoped_ptr<DigitalOutInterface> pp;
98  DigitalOutInterface* d = 0;
99  // this is the device users can use across all threads to control the
100  // scope's output.
101  if ( DigitalOutInterface::nameserver.getObject("ThreadScope") )
102  d = DigitalOutInterface::nameserver.getObject("ThreadScope");
103  else
104  Logger::log() << Logger::Error<< "Failed to find 'ThreadScope' object in DigitalOutInterface::nameserver." << Logger::endl;
105  if ( d ) {
106  Logger::log() << Logger::Info << "ThreadScope : main thread toggles bit "<< bit << Logger::endl;
107  d->switchOn( bit );
108  }
109 #endif
110  return ret;
111 }
112 
113 int __os_main_argc(void) {
114  return os_argc_arg;
115 }
116 char** __os_main_argv(void) {
117  return os_argv_arg;
118 }
119 
120 void __os_printFailure(const char* prog)
121 {
122 #ifdef OS_HAVE_IOSTREAM
123  cerr <<endl<< " Orocos has detected an uncaught C++ exception"<<endl;
124  cerr << " in the ORO_main() function."<<endl;
125  cerr << " You might have called a function which throws"<<endl;
126  cerr << " without a try {} catch {} block."<< endl << endl;
127  cerr << "To Debug this situation, issue the following command:"<<endl<<endl;
128  cerr << " valgrind -v --num-callers=16 "<< prog <<" [options...] --nocatch" << endl;
129  cerr << "Which will show where the exception occured."<<endl;
130  cerr << " ( Change num-callers for more/less detail."<<endl;
131  cerr << " Also, compiling orocos and your program with"<<endl;
132  cerr << " -g adds more usefull information. )"<<endl<<endl;
133 #else
134  printf("Orocos intercepted an uncaught C++ exception\n");
135 #endif
136 
137 }
138 
139 void __os_printException(const char* prog, std::exception& arg)
140 {
141 #ifdef OS_HAVE_IOSTREAM
142  cerr << endl<<" Caught std::exception." << endl << " what(): " << arg.what() <<endl;
143 #endif
144  __os_printFailure(prog);
145 }
146 
147 const char* oro_catchflag = "--nocatch";
148 const char* oro_catchflag2 = "--no-catch";
149 
150 int __os_checkException(int& argc, char** argv)
151 {
152  bool dotry = true;
153  // look for --nocatch/--no-catch flag :
154  for( int i=1; i < argc; ++i)
155  if ( strncmp(oro_catchflag, argv[i], strlen(oro_catchflag) ) == 0
156  || strncmp(oro_catchflag2, argv[i], strlen(oro_catchflag2) ) == 0 ) {
157  // if --no-catch was given last, remove it from the argc.
158  if ( i == argc-1)
159  --argc;
160  dotry = false;
161  }
162 
163  return dotry;
164 }
165 
166 void __os_exit(void)
167 {
168 #ifdef OROPKG_OS_THREAD_SCOPE
169  if (d)
170  d->switchOff(bit);
171 #endif
172 
174 
176 
178 
180 
181  Logger::log() << Logger::Debug << "Stopping StartStopManager." << Logger::endl;
184 
185  // This should be the (one but) last message to be logged :
186  Logger::log() << Logger::Debug << "Stopping MainThread." << Logger::endl;
187 
188  // Stop logging
189  Logger::Release();
190 
191  // Stop TimeService if present.
193 
194  // Stop Main Thread
196 
197 #ifdef OS_HAVE_MANUAL_CRT
198  DO_GLOBAL_DTORS();
199 #endif
200 }
static char ** os_argv_arg
Definition: startstop.cpp:77
static NameServer< DigitalOutInterface * > nameserver
static ThreadInterface * Instance()
Definition: MainThread.cpp:58
void __os_printException(const char *prog, std::exception &arg)
Definition: startstop.cpp:139
int __os_checkException(int &argc, char **argv)
Definition: startstop.cpp:150
int __os_init(int argc, char **argv)
Definition: startstop.cpp:79
Definition: mystd.hpp:163
bool start()
Call all registered start functions.
static bool Release()
Definition: TimeService.cpp:71
int __os_main_argc(void)
Definition: startstop.cpp:113
static StartStopManager * Instance()
virtual void switchOn(unsigned int n)=0
void __os_exit(void)
Definition: startstop.cpp:166
char ** __os_main_argv(void)
Definition: startstop.cpp:116
OS Abstractions such as Mutexes, Semaphores and Threads.
Definition: Atomic.hpp:45
static void Release()
Definition: MainThread.cpp:66
const char * oro_catchflag2
Definition: startstop.cpp:148
void __os_printFailure(const char *prog)
Definition: startstop.cpp:120
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
basic_ostreams & endl(basic_ostreams &s)
Definition: rtstreams.cpp:110
ValueType getObject(const NameType &s) const
Get the object registered for a name.
Definition: NameServer.hpp:136
static Logger & log()
Definition: Logger.cpp:117
static int os_argc_arg
Definition: startstop.cpp:76
static void Release()
Definition: Logger.cpp:82
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
void stop()
Call all registered stop functions.
static RTT_API void Release()
const char * oro_catchflag
Definition: startstop.cpp:147


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:44