1 /*
2    Copyright (C) 2003-2006 MySQL AB, 2010 Sun Microsystems, Inc.
3     All rights reserved. Use is subject to license terms.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License, version 2.0,
7    as published by the Free Software Foundation.
8 
9    This program is also distributed with certain software (including
10    but not limited to OpenSSL) that is licensed under separate terms,
11    as designated in a particular file or component or in included license
12    documentation.  The authors of MySQL hereby grant you an additional
13    permission to link the program and your derivative works with the
14    separately licensed software that they have included with MySQL.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License, version 2.0, for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 */
25 
26 #include <ndb_global.h>
27 #include <BaseString.hpp>
28 
29 #include "DebuggerNames.hpp"
30 
31 #include <BlockNumbers.h>
32 #include <GlobalSignalNumbers.h>
33 #include <signaldata/SignalDataPrint.hpp>
34 
35 static const char *            localSignalNames[MAX_GSN+1];
36 static SignalDataPrintFunction localPrintFunctions[MAX_GSN+1];
37 static const char *            localBlockNames[NO_OF_BLOCKS];
38 
39 static
40 int
initSignalNames(const char * dst[],const GsnName src[],unsigned short len)41 initSignalNames(const char * dst[], const GsnName src[], unsigned short len){
42   unsigned i;
43   for(i = 0; i<=MAX_GSN; i++)
44     dst[i] = 0;
45 
46   for(i = 0; i<len; i++){
47     unsigned short gsn = src[i].gsn;
48     const char * name  = src[i].name;
49 
50     if(dst[gsn] != 0 && name != 0){
51       if(strcmp(dst[gsn], name) != 0){
52 	fprintf(stderr,
53 		"Multiple definition of signal name for gsn: %d (%s, %s)\n",
54 		gsn, dst[gsn], name);
55 	exit(0);
56       }
57     }
58     dst[gsn] = name;
59   }
60   return 0;
61 }
62 
63 static
64 int
initSignalPrinters(SignalDataPrintFunction dst[],const NameFunctionPair src[])65 initSignalPrinters(SignalDataPrintFunction dst[],
66 		   const NameFunctionPair src[]){
67   unsigned i;
68   for(i = 0; i<=MAX_GSN; i++)
69     dst[i] = 0;
70 
71   unsigned short gsn;
72   for(i = 0; (gsn = src[i].gsn) > 0; i++){
73     SignalDataPrintFunction fun = src[i].function;
74 
75     if(dst[gsn] != 0 && fun != 0){
76       if(dst[gsn] != fun){
77 	fprintf(stderr,
78 		"Multiple definition of signal print function for gsn: %d\n",
79 		gsn);
80 	exit(0);
81       }
82     }
83     dst[gsn] = fun;
84   }
85   return 0;
86 }
87 
88 static
89 int
initBlockNames(const char * dst[],const BlockName src[],unsigned len)90 initBlockNames(const char * dst[],
91 	       const BlockName src[],
92 	       unsigned len){
93   unsigned i;
94   for(i = 0; i<NO_OF_BLOCKS; i++)
95     dst[i] = 0;
96 
97   for(i = 0; i<len; i++){
98     const int index = src[i].number - MIN_BLOCK_NO;
99     if((index < 0 && index >= NO_OF_BLOCKS) || dst[index] != 0){
100       fprintf(stderr,
101 	      "Invalid block name definition: %d %s\n",
102 	      src[i].number, src[i].name);
103       exit(0);
104     }
105     dst[index] = src[i].name;
106   }
107   return 0;
108 }
109 
110 /**
111  * Run static initializer
112  */
113 static const int
114 xxx_DUMMY_SIGNAL_NAMES_xxx = initSignalNames(localSignalNames,
115 					     SignalNames,
116 					     NO_OF_SIGNAL_NAMES);
117 static const int
118 xxx_DUMMY_PRINT_FUNCTIONS_xxx  = initSignalPrinters(localPrintFunctions,
119 						    SignalDataPrintFunctions);
120 
121 static const int
122 xxx_DUMMY_BLOCK_NAMES_xxx = initBlockNames(localBlockNames,
123 					   BlockNames,
124 					   NO_OF_BLOCK_NAMES);
125 
126 const char *
getSignalName(unsigned short gsn,const char * defVal)127 getSignalName(unsigned short gsn, const char * defVal){
128   if(gsn > 0 && gsn <= MAX_GSN)
129     return (localSignalNames[gsn] ? localSignalNames[gsn] : defVal);
130   return defVal;
131 }
132 
133 unsigned short
getGsn(const char * signalName)134 getGsn(const char * signalName){
135   return 0;
136 }
137 
138 const char *
getBlockName(unsigned short blockNo,const char * ret)139 getBlockName(unsigned short blockNo, const char * ret){
140   if(blockNo >= MIN_BLOCK_NO && blockNo <= MAX_BLOCK_NO)
141     return localBlockNames[blockNo-MIN_BLOCK_NO];
142   if (ret == 0) {
143     static char buf[20];
144     BaseString::snprintf(buf, sizeof(buf), "BLOCK#%d", (int)blockNo);
145     return buf;
146   }
147   return ret;
148 }
149 
150 unsigned short
getBlockNo(const char * blockName)151 getBlockNo(const char * blockName){
152   for(int i = 0; i<NO_OF_BLOCKS; i++)
153     if(localBlockNames[i] != 0 && strcmp(localBlockNames[i], blockName) == 0)
154       return i + MIN_BLOCK_NO;
155   return 0;
156 }
157 
158 SignalDataPrintFunction
findPrintFunction(unsigned short gsn)159 findPrintFunction(unsigned short gsn){
160   if(gsn > 0 && gsn <= MAX_GSN)
161     return localPrintFunctions[gsn];
162   return 0;
163 }
164