1/* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16/*
17 Logging of MyISAM commands and records on logfile for debugging
18 The log can be examined with help of the myisamlog command.
19*/
20
21#include "myisamdef.h"
22#ifdef __WIN__
23#include <fcntl.h>
24#endif
25
26#undef GETPID /* For HPUX */
27#define GETPID() (log_type == 1 ? (long) myisam_pid : (long) my_thread_dbug_id())
28
29 /* Activate logging if flag is 1 and reset logging if flag is 0 */
30
31static int log_type=0;
32ulong myisam_pid=0;
33
34int mi_log(int activate_log)
35{
36 int error=0;
37 char buff[FN_REFLEN];
38 DBUG_ENTER("mi_log");
39
40 log_type=activate_log;
41 if (activate_log)
42 {
43 if (!myisam_pid)
44 myisam_pid=(ulong) getpid();
45 if (myisam_log_file < 0)
46 {
47 if ((myisam_log_file= mysql_file_create(mi_key_file_log,
48 fn_format(buff,
49 myisam_log_filename,
50 "", ".log", 4),
51 0,
52 (O_RDWR | O_BINARY | O_APPEND),
53 MYF(0))) < 0)
54 DBUG_RETURN(my_errno);
55 }
56 }
57 else if (myisam_log_file >= 0)
58 {
59 error= mysql_file_close(myisam_log_file, MYF(0)) ? my_errno : 0 ;
60 myisam_log_file= -1;
61 }
62 DBUG_RETURN(error);
63}
64
65
66 /* Logging of records and commands on logfile */
67 /* All logs starts with command(1) dfile(2) process(4) result(2) */
68
69void _myisam_log(enum myisam_log_commands command, MI_INFO *info,
70 const uchar *buffert, uint length)
71{
72 uchar buff[11];
73 int error,old_errno;
74 ulong pid=(ulong) GETPID();
75 old_errno=my_errno;
76 bzero(buff,sizeof(buff));
77 buff[0]=(char) command;
78 mi_int2store(buff+1,info->dfile);
79 mi_int4store(buff+3,pid);
80 mi_int2store(buff+9,length);
81
82 mysql_mutex_lock(&THR_LOCK_myisam);
83 error=my_lock(myisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
84 (void) mysql_file_write(myisam_log_file, buff, sizeof(buff), MYF(0));
85 (void) mysql_file_write(myisam_log_file, buffert, length, MYF(0));
86 if (!error)
87 error=my_lock(myisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
88 mysql_mutex_unlock(&THR_LOCK_myisam);
89 my_errno=old_errno;
90}
91
92
93void _myisam_log_command(enum myisam_log_commands command, MI_INFO *info,
94 const uchar *buffert, uint length, int result)
95{
96 uchar buff[9];
97 int error,old_errno;
98 ulong pid=(ulong) GETPID();
99
100 old_errno=my_errno;
101 buff[0]=(char) command;
102 mi_int2store(buff+1,info->dfile);
103 mi_int4store(buff+3,pid);
104 mi_int2store(buff+7,result);
105 mysql_mutex_lock(&THR_LOCK_myisam);
106 error=my_lock(myisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
107 (void) mysql_file_write(myisam_log_file, buff, sizeof(buff), MYF(0));
108 if (buffert)
109 (void) mysql_file_write(myisam_log_file, buffert, length, MYF(0));
110 if (!error)
111 error=my_lock(myisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
112 mysql_mutex_unlock(&THR_LOCK_myisam);
113 my_errno=old_errno;
114}
115
116
117void _myisam_log_record(enum myisam_log_commands command, MI_INFO *info,
118 const uchar *record, my_off_t filepos, int result)
119{
120 uchar buff[21],*pos;
121 int error,old_errno;
122 uint length;
123 ulong pid=(ulong) GETPID();
124
125 old_errno=my_errno;
126 if (!info->s->base.blobs)
127 length=info->s->base.reclength;
128 else
129 length=info->s->base.reclength+ _mi_calc_total_blob_length(info,record);
130 buff[0]=(uchar) command;
131 mi_int2store(buff+1,info->dfile);
132 mi_int4store(buff+3,pid);
133 mi_int2store(buff+7,result);
134 mi_sizestore(buff+9,filepos);
135 mi_int4store(buff+17,length);
136 mysql_mutex_lock(&THR_LOCK_myisam);
137 error=my_lock(myisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
138 (void) mysql_file_write(myisam_log_file, buff, sizeof(buff), MYF(0));
139 (void) mysql_file_write(myisam_log_file, record, info->s->base.reclength, MYF(0));
140 if (info->s->base.blobs)
141 {
142 MI_BLOB *blob,*end;
143
144 for (end=info->blobs+info->s->base.blobs, blob= info->blobs;
145 blob != end ;
146 blob++)
147 {
148 memcpy(&pos, record+blob->offset+blob->pack_length,
149 sizeof(char*));
150 (void) mysql_file_write(myisam_log_file, pos, blob->length, MYF(0));
151 }
152 }
153 if (!error)
154 error=my_lock(myisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
155 mysql_mutex_unlock(&THR_LOCK_myisam);
156 my_errno=old_errno;
157}
158