1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
4 for INTEL64(R), AMD64(R)
6 Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
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.
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.
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
31 /* File Info -----------------------------------------------------------
35 ----------------------------------------------------------------------*/
37 #define DRD64_SRC_DEBUGD_MAIN
38 #include"drd64_debugd.h"
41 void Drd64_Debugd_SigTerm( siginfo_t *info, ucontext_t *uap)
43 drd64_debugd_info.i_flag_terminate = 0xff;
48 void Drd64_Debugd_SigHup( siginfo_t *info, ucontext_t *uap)
50 drd64_debugd_info.i_flag_terminate = 0xff;
56 Drd64_Debugd_Terminate(
59 DRD64_LOG_INFO("[INFO] Terminating drdebugd");
61 Drd64_Debugd_Debug_Detach( &drd64_debugd_info );
63 free( gp_sendbuffer );
65 if( drd64_debugd_info.i_socket_client
66 != drd64_debugd_info.i_socket_server ) {
67 close( drd64_debugd_info.i_socket_server );
70 close( drd64_debugd_info.i_socket_client );
71 close( drd64_debugd_info.i_socket_debugd );
73 DRD64_LOG_INFO("[INFO] Terminate drdebugd. Good-Bye.");
74 Drd64_Server_Log_TermLogSystem();
81 Drd64_Debugd_Main_PreInit(
82 Drd64_Debugd_MasterInformation *p_debugd )
84 Drd64_Server_Log_InitLogSystem(
85 DRD64_LOG_MODULE_DEBUGD,
86 DRD64_LOG_LEVEL_DEBUGD,
87 DRD64_LOG_FACILITY_DEBUGD );
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");
95 p_debugd->str_socketname_debugd[0] = '\0';
96 p_debugd->i_flag_terminate = 0x00;
103 Drd64_Debugd_Main_Init(
104 Drd64_Debugd_MasterInformation *p_debugd,
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 );
114 /* Init Recv.Status Struct */
115 i_err = Drd64_Server_RecvStatus_Init( DRD64_DEBUGD_RECVSTATUS_UNITS );
116 if( 0x00 != i_err ) { return i_err; }
118 /* Init Packet Read Buffer */
119 Drd64_Server_RecvSocket_InitReadBuffer();
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 ) {
129 /* Init Packet Data Buffer for Send */
130 gp_sendbuffer = (Byte *)malloc( DRD64_MAX_PACKET_LENGTH );
131 if( NULL == gp_sendbuffer ) { return 0x03; }
133 /* Attach or Debug-Start */
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; }
144 Drd64_Debugd_Main_Usage(
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.");
159 Drd64_Debugd_Main_AnalyzeOption(
160 Drd64_Debugd_MasterInformation *p_debugd,
177 while( -1 != ( i_opt = getopt( argc, argv, "n:c:r:m:a:d" )) ) {
178 if( 1 < i_flag_debug )
181 /* "n" : SocketName for Client */
183 strncpy( p_debugd->str_socketname_debugd,
184 optarg, DRD64_MAX_PATH );
187 /* "s" : SocketName for Server */
188 /*else if( 's' == i_opt ) {
189 strncpy( p_debugd->str_socketname_server,
190 optarg, DRD64_MAX_PATH );
192 /* "c" : coredump File Name */
193 else if( 'c' == i_opt ) {
194 strncpy( p_debugd->str_target_file_coredump,
195 optarg, DRD64_MAX_PATH );
197 /* "r" : Attach or Debug Request Source FD */
198 else if( 'r' == i_opt ) {
199 if( 0x00 == Drd64_Common_isDigits( optarg ) )
202 { *i_request_fd = atoi( optarg ); }
205 /* "m" : Local Marshald ConnectInfo ID */
206 else if( 'm' == i_opt ) {
207 if( 0x00 == Drd64_Common_isDigits( optarg ) )
210 { p_debugd->i_marshald_connectid = atoi( optarg ); }
213 /* "d" : Debug > Option Debugee FullPath */
214 else if( 'd' == i_opt ) {
215 if( 0 < i_flag_debug )
218 { i_flag_debug = 2; }
222 /* "a" : Attach > Option : Debugee Pid */
223 else if( 'a' == i_opt ) {
224 if( 0x00 == Drd64_Common_isDigits( optarg ) )
227 { p_debugd->i_target_pid = atoi( optarg ); }
238 if(( 'd' == i_opt ) && ( optind < argc )) {
239 strncpy( p_debugd->str_target_file_exec, argv[optind], DRD64_MAX_PATH );
242 if( optind < argc ) {
243 p_debugd->ppstr_argv = argv + optind;
244 p_debugd->i_arg_options = argc - optind;
251 if(( 0x00 != i_err ) || ( 4 > i_options )) {
252 Drd64_Debugd_Main_Usage();
266 /* Phase 1 : Check Booting by Command-Line? ---*/
268 Drd64_Debugd_Main_Usage();
272 /* Phase 2 : Deamonize drd64_debugd ---*/
273 Drd64_Server_Common_Deamonize();
274 /* XXX : Error Proc. */
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 ");
281 /* Phase 4 : Init. memory & Analyze Option ---*/
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;
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 ");
294 /* Phase 5 : Init. drd64_debugd ---*/
295 i_err = Drd64_Debugd_Main_Init( &drd64_debugd_info, i_request_fd );
296 if( 0x00 != i_err ) {
299 DRD64_LOG_VERBOSE(" [STAT] Completed drd64_debugd System Init. ");
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 ) {
306 DRD64_LOG_VERBOSE(" [STAT] Completed Attach. ");
309 DRD64_LOG_VERBOSE("[STAT] Completed Booting drd64_debugd. ");
311 Drd64_Debugd_Dispatch_Polling( &drd64_debugd_info );
313 Drd64_Debugd_Terminate();
320 /* EOF of drd64_.c ----------------------------------- */