1 /* vim: ts=8 sw=4 expandtab:
2  * ************************************************************************
3  * This file is part of the Devel::NYTProf package.
4  * Copyright 2008 Adam J. Kaplan, The New York Times Company.
5  * Copyright 2008 Tim Bunce, Ireland.
6  * Released under the same terms as Perl 5.8
7  * See http://metacpan.org/release/Devel-NYTProf/
8  *
9  * Contributors:
10  * Adam Kaplan, akaplan at nytimes.com
11  * Tim Bunce, http://blog.timbunce.org
12  * Steve Peters, steve at fisharerojo.org
13  *
14  * ************************************************************************
15  */
16 
17 /* Arguably this header is naughty, as it's not self contained, because it
18    assumes that stdlib.h has already been included (via perl.h)  */
19 
20 #if defined(PERL_IMPLICIT_SYS) && !defined(NO_XSLOCKS)
21 /* on Win32 XSUB.h redirects stdio to PerlIO, interp context is then required */
22 #  define NYTP_IO_dTHX dTHX
23 #  define NYTP_IO_NEEDS_THX
24 #else
25 #  define NYTP_IO_dTHX dNOOP
26 #endif
27 
28 typedef struct NYTP_file_t *NYTP_file;
29 
30 void NYTP_start_deflate(NYTP_file file, int compression_level);
31 void NYTP_start_inflate(NYTP_file file);
32 
33 NYTP_file NYTP_open(const char *name, const char *mode);
34 char *NYTP_gets(NYTP_file ifile, char **buffer, size_t *len);
35 size_t NYTP_read_unchecked(NYTP_file ifile, void *buffer, size_t len);
36 size_t NYTP_read(NYTP_file ifile, void *buffer, size_t len, const char *what);
37 size_t NYTP_write(NYTP_file ofile, const void *buffer, size_t len);
38 int NYTP_scanf(NYTP_file ifile, const char *format, ...);
39 int NYTP_printf(NYTP_file ofile, const char *format, ...);
40 int NYTP_flush(NYTP_file file);
41 int NYTP_eof(NYTP_file ifile);
42 long NYTP_tell(NYTP_file file);
43 int NYTP_close(NYTP_file file, int discard);
44 
45 const char *NYTP_fstrerror(NYTP_file file);
46 #ifdef HAS_ZLIB
47 const char *NYTP_type_of_offset(NYTP_file file);
48 #else
49 #  define NYTP_type_of_offset(file) ""
50 #endif
51 
52 #define NYTP_TAG_NO_TAG          '\0'   /* Used as a flag to mean "no tag" */
53 #define NYTP_TAG_ATTRIBUTE       ':'    /* :name=value\n */
54 #define NYTP_TAG_OPTION          '!'    /* !name=value\n */
55 #define NYTP_TAG_COMMENT         '#'    /* till newline */
56 #define NYTP_TAG_TIME_BLOCK      '*'
57 #define NYTP_TAG_TIME_LINE       '+'
58 #define NYTP_TAG_DISCOUNT        '-'
59 #define NYTP_TAG_NEW_FID         '@'
60 #define NYTP_TAG_SRC_LINE        'S'    /* fid, line, str */
61 #define NYTP_TAG_SUB_INFO        's'
62 #define NYTP_TAG_SUB_CALLERS     'c'
63 #define NYTP_TAG_PID_START       'P'
64 #define NYTP_TAG_PID_END         'p'
65 #define NYTP_TAG_STRING          '\''
66 #define NYTP_TAG_STRING_UTF8     '"'
67 #define NYTP_TAG_START_DEFLATE   'z'
68 #define NYTP_TAG_SUB_ENTRY       '>'
69 #define NYTP_TAG_SUB_RETURN      '<'
70 /* also add new items to nytp_tax_index below */
71 
72 typedef enum {      /* XXX keep in sync with various *_callback structures */
73     nytp_no_tag,
74     nytp_version,   /* Not actually a tag, but needed by the perl callback */
75     nytp_attribute,
76     nytp_option,
77     nytp_comment,
78     nytp_time_block,
79     nytp_time_line,
80     nytp_discount,
81     nytp_new_fid,
82     nytp_src_line,
83     nytp_sub_info,
84     nytp_sub_callers,
85     nytp_pid_start,
86     nytp_pid_end,
87     nytp_string,
88     nytp_string_utf8,
89     nytp_start_deflate,
90     nytp_sub_entry,
91     nytp_sub_return,
92     nytp_tag_max /* keep last */
93 } nytp_tax_index;
94 
95 void NYTProf_croak_if_not_stdio(NYTP_file file, const char *function);
96 
97 size_t NYTP_write_header(NYTP_file ofile, U32 major, U32 minor);
98 size_t NYTP_write_comment(NYTP_file ofile, const char *format, ...);
99 size_t NYTP_write_attribute_string(NYTP_file ofile,
100                                    const char *key, size_t key_len,
101                                    const char *value, size_t value_len);
102 size_t NYTP_write_attribute_signed(NYTP_file ofile, const char *key,
103                                    size_t key_len, long value);
104 size_t NYTP_write_attribute_unsigned(NYTP_file ofile, const char *key,
105                                      size_t key_len, unsigned long value);
106 size_t NYTP_write_attribute_nv(NYTP_file ofile, const char *key,
107                                      size_t key_len, NV value);
108 size_t NYTP_write_option_pv(NYTP_file ofile, const char *key,
109                                     const char *value, size_t value_len);
110 size_t NYTP_write_option_iv(NYTP_file ofile, const char *key, IV value);
111 size_t NYTP_start_deflate_write_tag_comment(NYTP_file ofile, int compression_level);
112 size_t NYTP_write_process_start(NYTP_file ofile, U32 pid, U32 ppid, NV time_of_day);
113 size_t NYTP_write_process_end(NYTP_file ofile, U32 pid, NV time_of_day);
114 size_t NYTP_write_sawampersand(NYTP_file ofile, U32 fid, U32 line);
115 size_t NYTP_write_new_fid(NYTP_file ofile, U32 id, U32 eval_fid, U32 eval_line_num,
116                         U32 flags, U32 size, U32 mtime, const char *name, I32 len);
117 size_t NYTP_write_time_block(NYTP_file ofile, I32 elapsed, U32 overflow,
118                         U32 fid, U32 line, U32 last_block_line, U32 last_sub_line);
119 size_t NYTP_write_time_line(NYTP_file ofile, I32 elapsed, U32 overflow,
120                         U32 fid, U32 line);
121 size_t NYTP_write_sub_info(NYTP_file ofile, U32 fid, const char *name, I32 len,
122                         U32 first_line, U32 last_line);
123 size_t NYTP_write_sub_callers(NYTP_file ofile, U32 fid, U32 line,
124                         const char *caller_name, I32 caller_name_len,
125                         U32 count, NV incl_rtime, NV excl_rtime,
126                         NV reci_rtime, U32 depth,
127                         const char *called_name, I32 called_name_len);
128 size_t NYTP_write_src_line(NYTP_file ofile, U32 fid,
129                         U32 line, const char *text, I32 text_len);
130 size_t NYTP_write_discount(NYTP_file ofile);
131 size_t NYTP_write_call_entry(NYTP_file ofile, U32 caller_fid, U32 caller_line);
132 size_t NYTP_write_call_return(NYTP_file ofile, U32 prof_depth, const char *called_subname_pv,
133                         NV incl_subr_ticks, NV excl_subr_ticks);
134 
135 
136 /* XXX
137  * On the write-side the functions above encapsulate the data format.
138  * On the read-side we've not got that far yet (and there's less need).
139  */
140 U32 read_u32(NYTP_file ifile);
141 I32 read_i32(NYTP_file ifile);
142 NV  read_nv(NYTP_file ifile);
143 
144