OSDN Git Service

(LibGoblin)
[drdeamon64/drdeamon64.git] / deamon / drd64_debugd_main.c
1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
2
3                          D r . D e a m o n  6 4
4                         for INTEL64(R), AMD64(R)
5         
6    Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11  1. Redistributions of source code must retain the above copyright notice,
12     this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
30
31 /* File Info -----------------------------------------------------------
32 File: drd64_.c
33 Function: 
34 Comment: 
35 ----------------------------------------------------------------------*/
36
37 #define DRD64_SRC_DEBUGD_MAIN
38 #include"drd64_debugd.h"
39
40
41 void Drd64_Debugd_SigTerm( siginfo_t *info, ucontext_t *uap)
42 {
43         drd64_debugd_info.i_flag_terminate      = 0xff;
44         return;
45 }
46
47
48 void Drd64_Debugd_SigHup( siginfo_t *info, ucontext_t *uap)
49 {
50         drd64_debugd_info.i_flag_terminate      = 0xff;
51         return;
52 }
53
54
55 int
56         Drd64_Debugd_Terminate(
57                 void )
58 {
59         DRD64_LOG_INFO("[INFO] Terminating drdebugd");
60
61         Drd64_Debugd_Debug_Detach( &drd64_debugd_info );
62
63         free( gp_sendbuffer );
64
65         if( drd64_debugd_info.i_socket_client
66                         != drd64_debugd_info.i_socket_server )  {
67                 close( drd64_debugd_info.i_socket_server );
68         }
69
70         close( drd64_debugd_info.i_socket_client );
71         close( drd64_debugd_info.i_socket_debugd );
72
73         DRD64_LOG_INFO("[INFO] Terminate drdebugd. Good-Bye.");
74         Drd64_Server_Log_TermLogSystem();
75
76         return 0x00;
77 }
78
79
80 int
81         Drd64_Debugd_Main_PreInit(
82                 Drd64_Debugd_MasterInformation *p_debugd )
83 {
84         Drd64_Server_Log_InitLogSystem(
85                 DRD64_LOG_MODULE_DEBUGD,
86                 DRD64_LOG_LEVEL_DEBUGD,
87                 DRD64_LOG_FACILITY_DEBUGD );
88
89         DRD64_LOG_INFO("**************************************************");
90         DRD64_LOG_INFO("drd64_debugd : Dr.Deamon64 Process Debugger Deamon");
91         DRD64_LOG_INFO("Copyright(C) 2007- Koine Yuusuke(koinec). All rights reserved.");
92         DRD64_LOG_INFO("[Arch]: INTEL64(amd64/x86-64) / FreeBSD");
93         DRD64_LOG_INFO("[INFO]: Booting drdebugd");
94
95         p_debugd->str_socketname_debugd[0] = '\0';
96         p_debugd->i_flag_terminate                      = 0x00;
97
98         return 0x00;
99 }
100
101
102 int
103         Drd64_Debugd_Main_Init(
104                 Drd64_Debugd_MasterInformation *p_debugd,
105                 int             i_request_fd )
106 {
107         int             i_err;
108
109         /* Set Signal Functions */
110         Drd64_Server_Common_Signal( SIGINT, Drd64_Debugd_SigTerm );
111         Drd64_Server_Common_Signal( SIGTERM, Drd64_Debugd_SigTerm );
112         Drd64_Server_Common_Signal( SIGHUP, Drd64_Debugd_SigHup );
113
114         /* Init Recv.Status Struct */
115         i_err   = Drd64_Server_RecvStatus_Init( DRD64_DEBUGD_RECVSTATUS_UNITS );
116         if( 0x00 != i_err )             { return i_err; }
117
118         /* Init Packet Read Buffer */
119         Drd64_Server_RecvSocket_InitReadBuffer();
120
121         /* Create Socket from Local Client or Marshald */
122         p_debugd->i_socket_debugd =
123                 Drd64_LibBrownie_Socket_InitSocketServer_Local(
124                         p_debugd->str_socketname_debugd );
125         if( 0 > p_debugd->i_socket_debugd )     {
126                 return 0x01;
127         }
128
129         /* Init Packet Data Buffer for Send */
130         gp_sendbuffer   = (Byte *)malloc( DRD64_MAX_PACKET_LENGTH );
131     if( NULL == gp_sendbuffer )     { return 0x03; }
132
133         /* Attach or Debug-Start */
134
135         /* Send Debugd-Boot-Info. Packet to Local-Marshald */
136         i_err = Drd64_Debugd_Send_Cmd_BootInfo( i_request_fd );
137         if( 0x00 != i_err )             { return i_err; }
138
139         return 0x00;
140 }
141
142
143 void
144         Drd64_Debugd_Main_Usage(
145                 void )
146 {
147         puts("drd64_debugd : Dr.Deamon64 Process Debugger Deamon");
148         puts("Copyright(C) 2007- Koine Yuusuke(koinec). All rights reserved.");
149         puts("[Arch]: INTEL64(amd64/x86-64) / FreeBSD");
150         puts(" ** CAUTION! ** ");
151         puts("  This Program have Booted by drd64_marshald.");
152         puts("  So, You can't boot drd64_debugd by Command-Line.");
153         
154         return;
155 }
156
157
158 int
159         Drd64_Debugd_Main_AnalyzeOption(
160                 Drd64_Debugd_MasterInformation  *p_debugd,
161                 int             argc,
162                 char    *argv[],
163                 int             *i_request_fd )
164 {
165         int                     i_err;
166         int                     i_flag_debug;
167         int                     i_opt;
168         int                     i_options;
169         extern char      *optarg;
170         extern int       optind;
171         extern int       optopt;
172
173         i_err   = 0x00;
174         i_flag_debug    = 0;
175         i_options       = 0;
176
177         while( -1 != ( i_opt = getopt( argc, argv, "n:c:r:m:a:d" )) )           {
178                 if( 1 < i_flag_debug )
179                                                 { break; }
180
181                 /* "n" : SocketName for Client */
182                 if( 'n' == i_opt )              {
183                         strncpy( p_debugd->str_socketname_debugd,
184                                                                 optarg, DRD64_MAX_PATH );
185                         i_options++;
186                 }
187                 /* "s" : SocketName for Server */
188                 /*else if( 's' == i_opt )               {
189                         strncpy( p_debugd->str_socketname_server,
190                                                                 optarg, DRD64_MAX_PATH );
191                 }*/
192                 /* "c" : coredump File Name */
193                 else if( 'c' == i_opt ) {
194                         strncpy( p_debugd->str_target_file_coredump,
195                                                                         optarg, DRD64_MAX_PATH );
196                 }
197                 /* "r" : Attach or Debug Request Source FD */
198                 else if( 'r' == i_opt ) {
199                         if( 0x00 == Drd64_Common_isDigits( optarg ) )
200                                 { i_err = 0x01; }
201                         else
202                                 { *i_request_fd = atoi( optarg ); }
203                         i_options++;
204                 }
205                 /* "m" : Local Marshald ConnectInfo ID */
206                 else if( 'm' == i_opt ) {
207                         if( 0x00 == Drd64_Common_isDigits( optarg ) )
208                                 { i_err = 0x01; }
209                         else
210                                 { p_debugd->i_marshald_connectid        = atoi( optarg ); }
211                         i_options++;
212                 }
213                 /* "d" : Debug > Option Debugee FullPath */
214                 else if( 'd' == i_opt ) {
215                         if( 0 < i_flag_debug )
216                                 { i_err = 0x01; }
217                         else
218                                 { i_flag_debug  = 2; }
219                         i_options++;
220                         break;
221                 }
222                 /* "a" : Attach > Option : Debugee Pid */
223                 else if( 'a' == i_opt ) {
224                         if( 0x00 == Drd64_Common_isDigits( optarg ) )
225                                 { i_err = 0x01; }
226                         else
227                                 { p_debugd->i_target_pid        = atoi( optarg ); }
228                         i_flag_debug    = 1;
229                         i_options++;
230                         break;
231                 }
232                 else            {
233                         i_err   = 0x01;
234                         break;
235                 }
236         }
237
238         if(( 'd' == i_opt ) && ( optind < argc ))               {
239                 strncpy( p_debugd->str_target_file_exec, argv[optind], DRD64_MAX_PATH );
240
241                 optind++;
242                 if( optind < argc )     {
243                         p_debugd->ppstr_argv    = argv + optind;
244                         p_debugd->i_arg_options = argc - optind;
245                 }
246         }
247         else    {
248
249         }
250
251         if(( 0x00 != i_err ) || ( 4 > i_options ))              {
252                 Drd64_Debugd_Main_Usage();
253         }
254
255         return i_err;
256 }
257
258
259 int main(
260         int argc,
261         char *argv[] )
262 {
263         int             i_err;
264         int             i_request_fd;
265
266         /* Phase 1 : Check Booting by Command-Line? ---*/
267         if( 9 > argc )  {
268                 Drd64_Debugd_Main_Usage();
269                 return 0x00;
270         }
271
272         /* Phase 2 : Deamonize drd64_debugd ---*/
273         Drd64_Server_Common_Deamonize();
274         /* XXX : Error Proc. */
275
276         /* Phase 3 : PreInit. drd64_debugd ( Init. LogSystem ) ---*/
277         i_err = Drd64_Debugd_Main_PreInit( &drd64_debugd_info  );
278         if( 0x00 != i_err )             { return 0x01; }
279         DRD64_LOG_VERBOSE("  [STAT] Completed Deamonize ");
280
281         /* Phase 4 : Init. memory & Analyze Option ---*/
282         i_err   = 0x00;
283
284         memset( &drd64_debugd_info, 0x00,
285                                                 sizeof( Drd64_Debugd_MasterInformation ) );
286         drd64_debugd_info.i_socket_client       = -1;
287         drd64_debugd_info.i_target_pid  = -1;
288
289         i_err = Drd64_Debugd_Main_AnalyzeOption(
290                                 &drd64_debugd_info, argc, argv, &i_request_fd );
291         if( 0x00 != i_err )     { return 0x01; }
292         DRD64_LOG_VERBOSE("  [STAT] Completed Analyze Option ");
293
294         /* Phase 5 : Init. drd64_debugd ---*/
295         i_err = Drd64_Debugd_Main_Init( &drd64_debugd_info, i_request_fd );
296         if( 0x00 != i_err )             {
297                 return 0x02;
298         }
299         DRD64_LOG_VERBOSE("  [STAT] Completed drd64_debugd System Init. ");
300
301         /* Phase 6 : Attach or Debug-Start to Debugee Program ---*/
302         i_err = Drd64_Debugd_Debug_Attach( &drd64_debugd_info );
303         if( 0x00 != i_err )             {
304                 return 0x03;
305         }
306         DRD64_LOG_VERBOSE("  [STAT] Completed Attach. ");
307
308
309         DRD64_LOG_VERBOSE("[STAT] Completed Booting drd64_debugd. ");
310
311         Drd64_Debugd_Dispatch_Polling( &drd64_debugd_info );
312
313         Drd64_Debugd_Terminate();
314
315         return 0x00;
316 }
317
318
319
320 /* EOF of drd64_.c ----------------------------------- */