3

I am using this code to generate the control flow graph of a C program. It is working fine for all the function except built-in function like printf and scanf. What can I change in this code to output the built in function as it is?

open Cil
open Cil_types
let print_stmt out =
function
| Instr i -> !Ast_printer.d_instr out i
| Return _ -> Format.pp_print_string out "<return>"
| Goto _ -> Format.pp_print_string out "<goto>"
| Break _ -> Format.pp_print_string out "<break>"
| Continue _ -> Format.pp_print_string out "<continue>"
| If (e,_,_,_) -> Format.fprintf out "if %a" !Ast_printer.d_exp e
| Switch(e,_,_,_) -> Format.fprintf out "switch %a" !Ast_printer.d_exp e
| Loop _ -> Format.fprintf out "<loop>"
| Block _ -> Format.fprintf out "<enter block>"
| UnspecifiedSequence _ -> Format.fprintf out "<enter unspecified sequence>"
| TryFinally _ | TryExcept _ -> Format.fprintf out "<try>"

class print_cfg out =
object
inherit Visitor.frama_c_inplace

method vstmt_aux s =
Format.fprintf out "s%d@[[label=\"%a\"]@];@\n" s.sid print_stmt s.skind;
List.iter 
  (fun succ -> Format.fprintf out "s%d -> s%d;@\n" s.sid succ.sid)
  s.succs;
DoChildren
method vglob_aux g =
match g with
  | GFun(f,loc) ->
      Format.fprintf out "@[<hov 2>subgraph cluster_%a {@\n\
                         graph [label=\"%a\"];@\n" 
        Cil_datatype.Varinfo.pretty f.svar
        Cil_datatype.Varinfo.pretty f.svar;
      ChangeDoChildrenPost([g], fun g -> Format.fprintf out "}@\n@]"; g)
  | _ -> SkipChildren

 method vfile f =
 Format.fprintf out "@[<hov 2>digraph cfg {@\n";
 ChangeDoChildrenPost (f,fun f -> Format.fprintf out "}@."; f)
 end

 let run () =
 let chan = open_out "cfg.out" in
 let fmt = Format.formatter_of_out_channel chan in
 Visitor.visitFramacFileSameGlobals (new print_cfg fmt) (Ast.get())

 let () = Db.Main.extend run
choroba
  • 231,213
  • 25
  • 204
  • 289
kbiplav
  • 63
  • 8
  • What it is exactly the issue? I had a quick look at your code and I don't see anything that would cause trouble with variadic functions. Could you post a C example, with the difference between expected and actual output? – Virgile Feb 13 '15 at 17:19
  • if i use statement printf("HELLO %d"); then after loading the script while trying to covert it into png using dot -Tpng it gives error label=printf("HELLO %d"); – kbiplav Feb 13 '15 at 17:40

1 Answers1

4

The problem is not related to variadic functions per se, but instead to literal strings. You must quote them, as otherwise dot thinks the label has ended. Try replacing

Format.fprintf out "s%d@[[label=\"%a\"]@];@\n" s.sid print_stmt s.skind;

by

Format.fprintf out "s%d@[[label=%S]@];@\n" s.sid (Pretty_utils.to_string print_stmt s.skind);

(Notice the %S, that outputs a quoted string.)

byako
  • 3,372
  • 2
  • 21
  • 36
  • How can I learn this language to make control flow graph more understandable? @Boris Yakobowski – kbiplav Feb 17 '15 at 18:07
  • Which language do you mean ? Dot, OCaml, or OCaml's sublanguage for `fprintf` ? – byako Feb 19 '15 at 10:50
  • Ocaml , I want to generate the graph which is more user friendly , @Boris Yakobowski – kbiplav Feb 19 '15 at 17:21
  • Sorry to trouble you , but when i am using "frama-c -load-script cfg_print.ml test.c " to load this script ,it is working fine but when i am trying to execute this command using shell_exec(' frama-c -load-script cfg_print.ml'.escapeshellarg(test.c)); using php its not working. Please help @ Boris Yakobowsk – kbiplav Feb 23 '15 at 08:00