OSDN Git Service

cf9923a18a416d3584d82d3549cc95612a9deef3
[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_Debug_Detach( &drd64_debugd_info );
44
45         free( gp_sendbuffer );
46
47         close( drd64_debugd_info.i_socket_debugd );
48         close( drd64_debugd_info.i_socket_client );
49
50         DRD64_LOG_INFO("[INFO] Terminate drdebugd. Good-Bye.");
51         Drd64_Server_Log_TermLogSystem();
52
53         exit( 0x00 );
54 }
55
56
57 void Drd64_Debugd_SigHup( siginfo_t *info, ucontext_t *uap)
58 {
59         exit( 0x00 );
60 }
61
62
63 int
64         Drd64_Debugd_Main_PreInit(
65                 Drd64_Debugd_MasterInformation *p_debugd )
66 {
67         Drd64_Server_Log_InitLogSystem(
68                 DRD64_LOG_MODULE_DEBUGD,
69                 DRD64_LOG_LEVEL_DEBUGD,
70                 DRD64_LOG_FACILITY_DEBUGD );
71
72         DRD64_LOG_INFO("**************************************************");
73         DRD64_LOG_INFO("drd64_debugd : Dr.Deamon64 Process Debugger Deamon");
74         DRD64_LOG_INFO("Copyright(C) 2007- Koine Yuusuke(koinec). All rights reserved.");
75         DRD64_LOG_INFO("[Arch]: INTEL64(amd64/x86-64) / FreeBSD");
76         DRD64_LOG_INFO("[INFO]: Booting drdebugd");
77
78         return 0x00;
79 }
80
81
82 int
83         Drd64_Debugd_Main_Init(
84                 Drd64_Debugd_MasterInformation *p_debugd,
85                 int             i_request_fd )
86 {
87         int             i_err;
88
89         /* Set Signal Functions */
90         Drd64_Server_Common_Signal( SIGINT, Drd64_Debugd_SigTerm );
91         Drd64_Server_Common_Signal( SIGTERM, Drd64_Debugd_SigTerm );
92         Drd64_Server_Common_Signal( SIGHUP, Drd64_Debugd_SigHup );
93
94         /* Init Recv.Status Struct */
95         i_err   = Drd64_Server_RecvStatus_Init( DRD64_DEBUGD_RECVSTATUS_UNITS );
96         if( 0x00 != i_err )             { return i_err; }
97
98         /* Init Packet Read Buffer */
99         Drd64_Server_RecvSocket_InitReadBuffer();
100
101         /* Create Socket from Local Client or Marshald */
102         /* GOMI : Drd64_Server_Socket_InitSocket_Local( p_debugd->str_socketname ); */
103         p_debugd->i_socket_debugd =
104                 Drd64_LibBrownie_Socket_InitSocketServer_Local(
105                         p_debugd->str_socketname );
106         if( 0 > p_debugd->i_socket_debugd )     {
107                 return 0x01;
108         }
109
110         /* Init Packet Data Buffer for Send */
111         gp_sendbuffer   = (Byte *)malloc( DRD64_MAX_PACKET_LENGTH );
112     if( NULL == gp_sendbuffer )     { return 0x03; }
113
114         /* Attach or Debug-Start */
115
116         /* Send Debugd-Boot-Info. Packet to Local-Marshald */
117         i_err = Drd64_Debugd_Send_Cmd_BootInfo( i_request_fd );
118         if( 0x00 != i_err )             { return i_err; }
119
120         return 0x00;
121 }
122
123
124 void
125         Drd64_Debugd_Main_Usage(
126                 void )
127 {
128         puts("drd64_debugd : Dr.Deamon64 Process Debugger Deamon");
129         puts("Copyright(C) 2007- Koine Yuusuke(koinec). All rights reserved.");
130         puts("[Arch]: INTEL64(amd64/x86-64) / FreeBSD");
131         puts(" ** CAUTION! ** ");
132         puts("  This Program have Booted by drd64_marshald.");
133         puts("  So, You can't boot drd64_debugd by Command-Line.");
134         
135         return;
136 }
137
138
139 int
140         Drd64_Debugd_Main_AnalyzeOption(
141                 Drd64_Debugd_MasterInformation  *p_debugd,
142                 int             argc,
143                 char    *argv[],
144                 int             *i_request_fd )
145 {
146         int                     i_err;
147         int                     i_flag_debug;
148         int                     i_opt;
149         int                     i_options;
150         extern char      *optarg;
151         extern int       optind;
152         extern int       optopt;
153
154         i_err   = 0x00;
155         i_flag_debug    = 0;
156         i_options       = 0;
157
158         while( -1 != ( i_opt = getopt( argc, argv, "n:c:r:m:a:d" )) )           {
159                 if( 1 < i_flag_debug )
160                                                 { break; }
161
162                 /* "n" : SocketName for Client */
163                 if( 'n' == i_opt )              {
164                         strncpy( p_debugd->str_socketname, optarg, DRD64_MAX_PATH );
165                         i_options++;
166                 }
167                 /* "c" : coredump File Name */
168                 else if( 'c' == i_opt ) {
169                         strncpy( p_debugd->str_target_file_coredump,
170                                                                         optarg, DRD64_MAX_PATH );
171                 }
172                 /* "r" : Attach or Debug Request Source FD */
173                 else if( 'r' == i_opt ) {
174                         if( 0x00 == Drd64_Common_isDigits( optarg ) )
175                                 { i_err = 0x01; }
176                         else
177                                 { *i_request_fd = atoi( optarg ); }
178                         i_options++;
179                 }
180                 /* "m" : Local Marshald ConnectInfo ID */
181                 else if( 'm' == i_opt ) {
182                         if( 0x00 == Drd64_Common_isDigits( optarg ) )
183                                 { i_err = 0x01; }
184                         else
185                                 { p_debugd->i_marshald_connectid        = atoi( optarg ); }
186                         i_options++;
187                 }
188                 /* "d" : Debug > Option Debugee FullPath */
189                 else if( 'd' == i_opt ) {
190                         if( 0 < i_flag_debug )
191                                 { i_err = 0x01; }
192                         else
193                                 { i_flag_debug  = 2; }
194                         i_options++;
195                         break;
196                 }
197                 /* "a" : Attach > Option : Debugee Pid */
198                 else if( 'a' == i_opt ) {
199                         if( 0x00 == Drd64_Common_isDigits( optarg ) )
200                                 { i_err = 0x01; }
201                         else
202                                 { p_debugd->i_target_pid        = atoi( optarg ); }
203                         i_flag_debug    = 1;
204                         i_options++;
205                         break;
206                 }
207                 else            {
208                         i_err   = 0x01;
209                         break;
210                 }
211         }
212
213         if(( 'd' == i_opt ) && ( optind < argc ))               {
214                 strncpy( p_debugd->str_target_file_exec, argv[optind], DRD64_MAX_PATH );
215
216                 optind++;
217                 if( optind < argc )     {
218                         p_debugd->ppstr_argv    = argv + optind;
219                         p_debugd->i_arg_options = argc - optind;
220                 }
221         }
222         else    {
223
224         }
225
226         if(( 0x00 != i_err ) || ( 4 > i_options ))              {
227                 Drd64_Debugd_Main_Usage();
228         }
229
230         return i_err;
231 }
232
233
234 int main(
235         int argc,
236         char *argv[] )
237 {
238         int             i_err;
239         int             i_request_fd;
240
241         /* Phase 1 : Check Booting by Command-Line? ---*/
242         if( 9 > argc )  {
243                 Drd64_Debugd_Main_Usage();
244                 return 0x00;
245         }
246
247         /* Phase 2 : Deamonize drd64_debugd ---*/
248         Drd64_Server_Common_Deamonize();
249         /* XXX : Error Proc. */
250
251         /* Phase 3 : PreInit. drd64_debugd ( Init. LogSystem ) ---*/
252         i_err = Drd64_Debugd_Main_PreInit( &drd64_debugd_info  );
253         if( 0x00 != i_err )             { return 0x01; }
254         DRD64_LOG_VERBOSE("  [STAT] Completed Deamonize ");
255
256         /* Phase 4 : Init. memory & Analyze Option ---*/
257         i_err   = 0x00;
258
259         memset( &drd64_debugd_info, 0x00,
260                                                 sizeof( Drd64_Debugd_MasterInformation ) );
261         drd64_debugd_info.i_socket_client       = -1;
262         drd64_debugd_info.i_target_pid  = -1;
263
264         i_err = Drd64_Debugd_Main_AnalyzeOption(
265                                 &drd64_debugd_info, argc, argv, &i_request_fd );
266         if( 0x00 != i_err )     { return 0x01; }
267         DRD64_LOG_VERBOSE("  [STAT] Completed Analyze Option ");
268
269         /* Phase 5 : Init. drd64_debugd ---*/
270         i_err = Drd64_Debugd_Main_Init( &drd64_debugd_info, i_request_fd );
271         if( 0x00 != i_err )             {
272                 return 0x02;
273         }
274         DRD64_LOG_VERBOSE("  [STAT] Completed drd64_debugd System Init. ");
275
276         /* Phase 6 : Attach or Debug-Start to Debugee Program ---*/
277         i_err = Drd64_Debugd_Debug_Attach( &drd64_debugd_info );
278         if( 0x00 != i_err )             {
279                 return 0x03;
280         }
281         DRD64_LOG_VERBOSE("  [STAT] Completed Attach. ");
282
283
284         DRD64_LOG_VERBOSE("[STAT] Completed Booting drd64_debugd. ");
285
286         Drd64_Debugd_Dispatch_Polling( &drd64_debugd_info );
287
288         return 0x00;
289 }
290
291
292
293 /* EOF of drd64_.c ----------------------------------- */