]> git.cworth.org Git - apitrace/blob - common/trace_local_writer.cpp
More D2D spec tweaks.
[apitrace] / common / trace_local_writer.cpp
1 /**************************************************************************
2  *
3  * Copyright 2007-2011 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <assert.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "os.hpp"
34 #include "trace_file.hpp"
35 #include "trace_writer.hpp"
36 #include "trace_format.hpp"
37
38
39 namespace Trace {
40
41
42 static void exceptionCallback(void)
43 {
44     localWriter.flush();
45 }
46
47
48 LocalWriter::LocalWriter() :
49     acquired(0)
50 {
51     // Install the signal handlers as early as possible, to prevent
52     // interfering with the application's signal handling.
53     OS::SetExceptionCallback(exceptionCallback);
54 }
55
56 LocalWriter::~LocalWriter()
57 {
58     OS::ResetExceptionCallback();
59 }
60
61 void
62 LocalWriter::open(void) {
63
64     static unsigned dwCounter = 0;
65
66     const char *szExtension = "trace";
67     char szFileName[PATH_MAX];
68     const char *lpFileName;
69
70     lpFileName = getenv("TRACE_FILE");
71     if (lpFileName) {
72         strncpy(szFileName, lpFileName, PATH_MAX);
73     }
74     else {
75         char szProcessName[PATH_MAX];
76         char szCurrentDir[PATH_MAX];
77         OS::GetProcessName(szProcessName, PATH_MAX);
78         OS::GetCurrentDir(szCurrentDir, PATH_MAX);
79
80         for (;;) {
81             FILE *file;
82
83             if (dwCounter)
84                 snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension);
85             else
86                 snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension);
87
88             file = fopen(szFileName, "rb");
89             if (file == NULL)
90                 break;
91
92             fclose(file);
93
94             ++dwCounter;
95         }
96     }
97
98     OS::DebugMessage("apitrace: tracing to %s\n", szFileName);
99
100     Writer::open(szFileName);
101
102 #if 0
103     // For debugging the exception handler
104     *((int *)0) = 0;
105 #endif
106 }
107
108 unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
109     OS::AcquireMutex();
110     ++acquired;
111
112     if (!m_file->isOpened()) {
113         open();
114     }
115
116     return Writer::beginEnter(sig);
117 }
118
119 void LocalWriter::endEnter(void) {
120     Writer::endEnter();
121     --acquired;
122     OS::ReleaseMutex();
123 }
124
125 void LocalWriter::beginLeave(unsigned call) {
126     OS::AcquireMutex();
127     ++acquired;
128     Writer::beginLeave(call);
129 }
130
131 void LocalWriter::endLeave(void) {
132     Writer::endLeave();
133     --acquired;
134     OS::ReleaseMutex();
135 }
136
137 void LocalWriter::flush(void) {
138     /*
139      * Do nothing if the mutex is already acquired (e.g., if a segfault happen
140      * while writing the file) to prevent dead-lock.
141      */
142
143     if (!acquired) {
144         OS::AcquireMutex();
145         if (m_file->isOpened()) {
146             OS::DebugMessage("apitrace: flushing trace due to an exception\n");
147             m_file->flush();
148         }
149         OS::ReleaseMutex();
150     }
151 }
152
153
154 LocalWriter localWriter;
155
156
157 } /* namespace Trace */
158