• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "include/trace_marker.h"
6 
7 #include <fcntl.h>
8 #include <mntent.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 
14 #include "include/eintr_wrapper.h"
15 #include "include/logging.h"
16 
17 namespace gestures {
18 
CreateTraceMarker()19 void TraceMarker::CreateTraceMarker() {
20   if (!trace_marker_)
21     trace_marker_ = new TraceMarker();
22   trace_marker_count_++;
23 }
24 
DeleteTraceMarker()25 void TraceMarker::DeleteTraceMarker() {
26   if (trace_marker_count_ == 1) {
27     delete trace_marker_;
28     trace_marker_ = NULL;
29   }
30   trace_marker_count_--;
31   if (trace_marker_count_ < 0)
32     trace_marker_count_ = 0;
33 }
34 
StaticTraceWrite(const char * str)35 void TraceMarker::StaticTraceWrite(const char* str) {
36   if (TraceMarker::GetTraceMarker())
37     TraceMarker::GetTraceMarker()->TraceWrite(str);
38   else
39     Err("No TraceMarker Object");
40 }
41 
GetTraceMarker()42 TraceMarker* TraceMarker::GetTraceMarker() {
43   return trace_marker_;
44 }
45 
TraceWrite(const char * str)46 void TraceMarker::TraceWrite(const char* str) {
47   if (fd_ == -1) {
48     Err("Trace_marker does not open");
49     return;
50   }
51   ssize_t len = strlen(str);
52   ssize_t get = write(fd_, str, len);
53   if (get == -1)
54     Err("Write failed");
55   else if (get != len)
56     Err("Message too long!");
57 }
58 
TraceMarker()59 TraceMarker::TraceMarker() : fd_(-1) {
60   if (!OpenTraceMarker())
61     Log("Cannot open trace_marker");
62 }
63 
~TraceMarker()64 TraceMarker::~TraceMarker() {
65   if (fd_ != -1)
66     close(fd_);
67 }
68 
69 TraceMarker* TraceMarker::trace_marker_ = NULL;
70 int TraceMarker::trace_marker_count_ = 0;
71 
FindDebugfs(const char ** ret) const72 bool TraceMarker::FindDebugfs(const char** ret) const {
73   FILE* mounts = setmntent("/proc/mounts", "r");
74   if (mounts == NULL)
75     return false;
76 
77   while (true) {
78     struct mntent* entry = getmntent(mounts);
79     if (entry == NULL) {
80       fclose(mounts);
81       return false;
82     }
83     if (strcmp("debugfs", entry->mnt_type) == 0) {
84       *ret = entry->mnt_dir;
85       break;
86     }
87   }
88   fclose(mounts);
89   return true;
90 }
91 
FindTraceMarker(char ** ret) const92 bool TraceMarker::FindTraceMarker(char** ret) const {
93   const char* debugfs = NULL;
94   if (!FindDebugfs(&debugfs))
95     return false;
96   if (asprintf(ret, "%s/tracing/trace_marker", debugfs) == -1) {
97     *ret = NULL;
98     return false;
99   }
100   return true;
101 }
102 
OpenTraceMarker()103 bool TraceMarker::OpenTraceMarker() {
104   char* trace_marker_filename = NULL;
105   if (!FindTraceMarker(&trace_marker_filename))
106     return false;
107   fd_ = HANDLE_EINTR(open(trace_marker_filename, O_WRONLY));
108   free(trace_marker_filename);
109   if (fd_ == -1)
110     return false;
111   return true;
112 }
113 }  // namespace gestures
114