1 (* Memory info for virtual domains.
2 (C) Copyright 2008 Richard W.M. Jones, Red Hat Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 (* This program takes the kernel database (in kernels/ in toplevel
21 directory) and generates parsing code for the various structures
22 in the kernel that we are interested in.
24 The output programs -- *.ml, *.mli files of generated code -- go
25 into lib/ at the toplevel, eg. lib/kernel_task_struct.ml
27 The stuff at the top of this file determine what structures
28 and fields we try to parse.
33 "struct task_struct {", "};", true,
34 [ "state"; "prio"; "normal_prio"; "static_prio";
35 "tasks'prev"; "tasks'next"; "comm"]
39 "struct mm_struct {", "};", true,
44 "struct net_device {", "};", true,
45 [ "name"; "dev_addr" ]
59 let (//) = Filename.concat
62 let args = Array.to_list Sys.argv in
64 let kernelsdir, outputdir =
68 let arg0 = Filename.basename Sys.executable_name in
69 eprintf "%s - Turn kernels database into code modules.
72 %s <kernelsdir> <outputdir>
74 Example (from toplevel of virt-mem source tree):
79 (* Get the *.info files from the kernels database. *)
80 let infos = Sys.readdir kernelsdir in
81 let infos = Array.to_list infos in
82 let infos = List.filter (fun name -> String.ends_with name ".info") infos in
83 let infos = List.map ((//) kernelsdir) infos in
85 (* Regular expressions. We really really should use ocaml-mikmatch ... *)
86 let re_oldformat = Pcre.regexp "^RPM: \\d+: \\(build \\d+\\) ([-\\w]+) ([\\w.]+) ([\\w.]+) \\(.*?\\) (\\w+)" in
87 let re_keyvalue = Pcre.regexp "^(\\w+): (.*)" in
89 (* Parse in the *.info files. These have historically had a few different
90 * formats that we need to support.
92 let infos = List.map (
94 (* Get the basename (for getting the .data file later on). *)
95 let basename = Filename.chop_suffix filename ".info" in
97 let chan = open_in filename in
98 let line = input_line chan in
100 (* Kernel version string. *)
102 if Pcre.pmatch ~rex:re_oldformat line then (
103 (* If the file starts with "RPM: \d+: ..." then it's the
104 * original Fedora format. Everything in one line.
106 let subs = Pcre.exec ~rex:re_oldformat line in
107 (* let name = Pcre.get_substring subs 1 in *)
108 let version = Pcre.get_substring subs 2 in
109 let release = Pcre.get_substring subs 3 in
110 let arch = Pcre.get_substring subs 4 in
112 (* XXX Map name -> PAE, hugemem etc. *)
113 (* name, *) sprintf "%s-%s.%s" version release arch, arch
115 (* New-style "key: value" entries, up to end of file or the first
118 let (*name,*) version, release, arch =
119 (*ref "",*) ref "", ref "", ref "" in
122 let subs = Pcre.exec ~rex:re_keyvalue line in
123 let key = Pcre.get_substring subs 1 in
124 let value = Pcre.get_substring subs 2 in
125 (*if key = "Name" then name := value
126 else*) if key = "Version" then version := value
127 else if key = "Release" then release := value
128 else if key = "Architecture" then arch := value;
129 let line = input_line chan in
132 Not_found | End_of_file ->
136 let (*name,*) version, release, arch =
137 (*!name,*) !version, !release, !arch in
138 if (*name = "" ||*) version = "" || release = "" || arch = "" then
139 failwith (sprintf "%s: missing Name, Version, Release or Architecture key" filename);
140 (* XXX Map name -> PAE, hugemem etc. *)
141 (* name, *) sprintf "%s-%s.%s" version release arch, arch
144 (*printf "%s -> %s %s\n%!" basename version arch;*)
146 (basename, version, arch)
149 (* For quick access to the opener strings, build a hash. *)
150 let openers = Hashtbl.create 13 in
152 fun (name, (opener, closer, _, _)) ->
153 Hashtbl.add openers opener (closer, name)
156 (* Now read the data files and parse out the structures of interest. *)
157 let datas = List.map (
158 fun (basename, version, arch) ->
159 let file_exists name =
160 try Unix.access name [Unix.F_OK]; true
161 with Unix.Unix_error _ -> false
163 let close_process_in cmd chan =
164 match Unix.close_process_in chan with
165 | Unix.WEXITED 0 -> ()
167 eprintf "%s: command exited with code %d\n" cmd i; exit i
168 | Unix.WSIGNALED i ->
169 eprintf "%s: command exited with signal %d\n" cmd i; exit 1
171 eprintf "%s: command stopped by signal %d\n" cmd i; exit 1
174 (* Open the data file, uncompressing it on the fly if necessary. *)
176 if file_exists (basename ^ ".data") then
177 open_in (basename ^ ".data"), close_in
178 else if file_exists (basename ^ ".data.gz") then (
180 sprintf "gzip -cd %s" (Filename.quote (basename ^ ".data.gz")) in
181 Unix.open_process_in cmd, close_process_in cmd
183 else if file_exists (basename ^ ".data.bz2") then (
185 sprintf "bzip2 -cd %s" (Filename.quote (basename ^ ".data.bz2")) in
186 Unix.open_process_in cmd, close_process_in cmd
189 (sprintf "%s: cannot find corresponding data file" basename) in
191 (* Read the data file in, looking for structures of interest to us. *)
192 let bodies = Hashtbl.create 13 in
194 let line = input_line chan in
196 (* If the line is an opener for one of the structures we
197 * are looking for, then for now just save all the text until
198 * we get to the closer line.
201 let closer, name = Hashtbl.find openers line in
202 let rec loop2 lines =
203 let line = input_line chan in
204 let lines = line :: lines in
205 if String.starts_with line closer then List.rev lines
212 failwith (sprintf "%s: %s: %S not matched by closing %S" basename name line closer) in
214 Hashtbl.replace bodies name body
215 with Not_found -> ());
219 (try loop () with End_of_file -> ());
223 (* Make sure we got all the mandatory structures. *)
225 fun (name, (_, _, mandatory, _)) ->
226 if mandatory && not (Hashtbl.mem bodies name) then
227 failwith (sprintf "%s: structure %s not found in this kernel" basename name)
230 (basename, version, arch, bodies)
233 (* Now parse each structure body.
234 * XXX This would be better as a proper lex/yacc parser.
235 * XXX Even better would be to have a proper interface to libdwarves.
237 let re_offsetsize = Pcre.regexp "/\\*\\s+(\\d+)\\s+(\\d+)\\s+\\*/" in
238 let re_intfield = Pcre.regexp "int\\s+(\\w+);" in
239 let re_ptrfield = Pcre.regexp "struct\\s+(\\w+)\\s*\\*\\s*(\\w+);" in
240 let re_strfield = Pcre.regexp "char\\s+(\\w+)\\[(\\d+)\\];" in
241 let re_structopener = Pcre.regexp "(struct|union)\\s+.*{$" in
242 let re_structcloser = Pcre.regexp "}\\s*(\\w+)?(\\[\\d+\\])?;" in
244 (* 'basename' is the source file, and second parameter ('body') is
245 * the list of text lines which covers this structure (minus the
246 * opener line). Result is the list of parsed fields from this
249 let rec parse basename = function
251 | [_] -> [] (* Just the closer line, finished. *)
252 | line :: lines when Pcre.pmatch ~rex:re_structopener line ->
253 (* Recursively parse a sub-structure. First search for the
254 * corresponding closer line.
256 let rec loop depth acc = function
258 eprintf "%s: %S has no matching close structure line\n%!"
261 | line :: lines when Pcre.pmatch ~rex:re_structopener line ->
262 loop (depth+1) (line :: acc) lines
264 when depth = 0 && Pcre.pmatch ~rex:re_structcloser line ->
267 when depth > 0 && Pcre.pmatch ~rex:re_structcloser line ->
268 loop (depth-1) (line :: acc) lines
269 | line :: lines -> loop depth (line :: acc) lines
271 let nested_body, rest = loop 0 [] lines in
273 (* Then parse the sub-structure. *)
274 let struct_name, nested_body =
275 match nested_body with
278 let subs = Pcre.exec ~rex:re_structcloser closer in
280 try Some (Pcre.get_substring subs 1) with Not_found -> None in
281 struct_name, List.rev nested_body in
282 let nested_fields = parse basename nested_body in
284 (* Prefix the sub-fields with the name of the structure. *)
286 match struct_name with
287 | None -> nested_fields
290 fun (name, details) -> (prefix ^ "'" ^ name, details)
293 (* Parse the rest. *)
294 nested_fields @ parse basename rest
296 | line :: lines when Pcre.pmatch ~rex:re_intfield line ->
298 let subs = Pcre.exec ~rex:re_intfield line in
299 let name = Pcre.get_substring subs 1 in
301 let subs = Pcre.exec ~rex:re_offsetsize line in
302 let offset = int_of_string (Pcre.get_substring subs 1) in
303 let size = int_of_string (Pcre.get_substring subs 2) in
304 (name, (`Int, offset, size)) :: parse basename lines
306 Not_found -> parse basename lines
309 | line :: lines when Pcre.pmatch ~rex:re_ptrfield line ->
310 (* A pointer-to-struct field. *)
311 let subs = Pcre.exec ~rex:re_ptrfield line in
312 let struct_name = Pcre.get_substring subs 1 in
313 let name = Pcre.get_substring subs 2 in
315 let subs = Pcre.exec ~rex:re_offsetsize line in
316 let offset = int_of_string (Pcre.get_substring subs 1) in
317 let size = int_of_string (Pcre.get_substring subs 2) in
318 (name, (`Ptr struct_name, offset, size)) :: parse basename lines
320 Not_found -> parse basename lines
323 | line :: lines when Pcre.pmatch ~rex:re_strfield line ->
324 (* A string (char array) field. *)
325 let subs = Pcre.exec ~rex:re_strfield line in
326 let name = Pcre.get_substring subs 1 in
327 let width = int_of_string (Pcre.get_substring subs 2) in
329 let subs = Pcre.exec ~rex:re_offsetsize line in
330 let offset = int_of_string (Pcre.get_substring subs 1) in
331 let size = int_of_string (Pcre.get_substring subs 2) in
332 (name, (`Str width, offset, size)) :: parse basename lines
334 Not_found -> parse basename lines
338 (* Just ignore any other field we can't parse. *)
343 let datas = List.map (
344 fun (basename, version, arch, bodies) ->
345 let structures = List.filter_map (
346 fun (name, (_, _, _, wanted_fields)) ->
348 try Some (Hashtbl.find bodies name) with Not_found -> None in
352 let body = List.tl body in (* Don't care about opener line. *)
353 let fields = parse basename body in
355 (* That got us all the fields, but we only care about
358 let fields = List.filter (
359 fun (name, _) -> List.mem name wanted_fields
362 (* Also check we have all the wanted fields. *)
365 if not (List.mem_assoc wanted_field fields) then
366 failwith (sprintf "%s: structure %s is missing required field %s" basename name wanted_field)
372 (basename, version, arch, structures)
377 fun (basename, version, arch, structures) ->
378 printf "%s (version: %s, arch: %s):\n" basename version arch;
380 fun (struct_name, fields) ->
381 printf " struct %s {\n" struct_name;
383 fun (field_name, (typ, offset, size)) ->
386 printf " int %s; " field_name
387 | `Ptr struct_name ->
388 printf " struct %s *%s; " struct_name field_name
390 printf " char %s[%d]; " field_name width
392 printf " /* offset = %d, size = %d */\n" offset size
398 (* We'll generate a code file for each structure type (eg. task_struct
399 * across all kernel versions), so rearrange 'datas' for that purpose.
401 * XXX This loop is O(n^3), luckily n is small!
408 fun (basename, version, arch, structures) ->
409 try Some (basename, version, arch, List.assoc name structures)
410 with Not_found -> None
414 let datas = () in ignore datas; (* garbage collect *)
416 (* Get just the field types. It's plausible that a field with the
417 * same name has a different type between kernel versions, so we must
418 * check that didn't happen.
420 let files = List.map (
421 fun (struct_name, kernels) ->
425 | (_, _, _, fields) :: kernels ->
426 let field_types_of_fields fields =
429 fun (field_name, (typ, _, _)) -> field_name, typ
433 let field_types = field_types_of_fields fields in
435 fun (_, _, _, fields) ->
436 if field_types <> field_types_of_fields fields then
437 failwith (sprintf "%s: one of the structure fields changed type between kernel versions" struct_name)
440 (struct_name, kernels, field_types)
443 (* To minimize generated code size, we want to fold together all
444 * structures where the particulars (eg. offsets, sizes, endianness)
445 * of the fields we care about are the same -- eg. between kernel
446 * versions which are very similar.
448 let endian_of_architecture arch =
449 if String.starts_with arch "i386" ||
450 String.starts_with arch "i486" ||
451 String.starts_with arch "i586" ||
452 String.starts_with arch "i686" ||
453 String.starts_with arch "x86_64" ||
454 String.starts_with arch "x86-64" then
455 Bitstring.LittleEndian
456 else if String.starts_with arch "ia64" then
457 Bitstring.LittleEndian (* XXX usually? *)
458 else if String.starts_with arch "ppc" then
460 else if String.starts_with arch "sparc" then
463 failwith (sprintf "endian_of_architecture: cannot parse %S" arch)
468 fun (struct_name, kernels, field_types) ->
469 let hash = Hashtbl.create 13 in
474 fun (basename, version, arch, fields) ->
475 let key = endian_of_architecture arch, fields in
477 try Hashtbl.find hash key
480 xs := (!i, key) :: !xs; Hashtbl.add hash key !i;
482 (basename, version, arch, j)
484 struct_name, kernels, field_types, List.rev !xs
487 (* How much did we save by sharing? *)
490 fun (struct_name, kernels, _, parsers) ->
491 printf "struct %s:\n" struct_name;
492 printf " number of kernel versions: %d\n" (List.length kernels);
493 printf " number of parser functions needed after sharing: %d\n"
494 (List.length parsers)
497 (* Let's generate some code! *)
500 fun (struct_name, kernels, field_types, parsers) ->
501 (* Dummy location required - there are no real locations for
504 let _loc = Loc.ghost in
506 (* The structure type. *)
507 let struct_type, struct_sig =
508 let fields = List.map (
511 <:ctyp< $lid:name$ : int >>
512 | (name, `Ptr struct_name) ->
513 <:ctyp< $lid:name$ : (*`$str:struct_name$*) int64 >>
515 <:ctyp< $lid:name$ : string >>
517 let f, fs = match fields with
518 | [] -> failwith (sprintf "%s: structure has no fields" struct_name)
519 | f :: fs -> f, fs in
520 let fields = List.fold_left (
521 fun fs f -> <:ctyp< $fs$ ; $f$ >>
524 let struct_type = <:str_item< type t = { $fields$ } >> in
525 let struct_sig = <:sig_item< type t = { $fields$ } >> in
526 struct_type, struct_sig in
528 let code = <:str_item<
532 let interface = <:sig_item<
536 (struct_name, code, interface)
539 (* Finally generate the output files. *)
541 fun (struct_name, code, interface) ->
542 let output_file = outputdir // "kernel_" ^ struct_name ^ ".ml" in
543 Printers.OCaml.print_implem ~output_file code;
545 let output_file = outputdir // "kernel_" ^ struct_name ^ ".mli" in
546 Printers.OCaml.print_interf ~output_file interface