fix: context.h add missing documentation for packet_seek parameter
[babeltrace.git] / lib / context.c
CommitLineData
b469d2dd
JD
1/*
2 * context.c
3 *
4 * Babeltrace Library
5 *
6 * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Julien Desfossez <julien.desfossez@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 */
21
22#include <babeltrace/babeltrace.h>
23#include <babeltrace/context.h>
08c22d05 24#include <babeltrace/context-internal.h>
6cba487f
MD
25#include <babeltrace/trace-handle.h>
26#include <babeltrace/trace-handle-internal.h>
27#include <babeltrace/trace-collection.h>
28#include <babeltrace/format.h>
e1d01c39 29#include <babeltrace/babeltrace-internal.h>
b469d2dd 30#include <stdlib.h>
e1d01c39
MD
31#include <string.h>
32#include <assert.h>
b469d2dd 33
6cba487f
MD
34#include <fts.h>
35#include <fcntl.h> /* For O_RDONLY */
36
6cba487f
MD
37#include <glib.h>
38
39struct bt_context *bt_context_create(void)
b469d2dd
JD
40{
41 struct bt_context *ctx;
42
6cba487f 43 ctx = g_new0(struct bt_context, 1);
b469d2dd 44 ctx->refcount = 1;
6cba487f 45 /* Negative handle id are errors. */
842c2b97 46 ctx->last_trace_handle_id = 0;
b469d2dd 47
6cba487f
MD
48 /* Instanciate the trace handle container */
49 ctx->trace_handles = g_hash_table_new_full(g_direct_hash,
50 g_direct_equal, NULL,
51 (GDestroyNotify) bt_trace_handle_destroy);
52
53 ctx->tc = g_new0(struct trace_collection, 1);
54 init_trace_collection(ctx->tc);
55
b469d2dd 56 return ctx;
6cba487f 57}
b469d2dd 58
6cba487f 59int bt_context_add_trace(struct bt_context *ctx, const char *path,
613f532b 60 const char *format_name,
20d0dcf9
MD
61 void (*packet_seek)(struct stream_pos *pos, size_t index,
62 int whence))
6cba487f
MD
63{
64 struct trace_descriptor *td;
65 struct format *fmt;
66 struct bt_trace_handle *handle;
1059a2bf 67 int ret;
6cba487f 68
282e1952
MD
69 fmt = bt_lookup_format(g_quark_from_string(format_name));
70 if (!fmt) {
71 fprintf(stderr, "[error] [Context] Format \"%s\" unknown.\n\n",
72 format_name);
73 ret = -1;
74 goto end;
75 }
613f532b 76 td = fmt->open_trace(path, O_RDONLY, packet_seek, NULL);
6cba487f 77 if (!td) {
02dc4610 78 fprintf(stderr, "[error] [Context] Cannot open_trace of the format %s .\n\n",
6cba487f 79 path);
1059a2bf
JD
80 ret = -1;
81 goto end;
6cba487f
MD
82 }
83
84 /* Create an handle for the trace */
85 handle = bt_trace_handle_create(ctx);
86 if (handle < 0) {
02dc4610 87 fprintf(stderr, "[error] [Context] Creating trace handle %s .\n\n",
6cba487f 88 path);
1059a2bf
JD
89 ret = -1;
90 goto end;
6cba487f
MD
91 }
92 handle->format = fmt;
93 handle->td = td;
94 strncpy(handle->path, path, PATH_MAX);
95 handle->path[PATH_MAX - 1] = '\0';
96
97 /* Add new handle to container */
98 g_hash_table_insert(ctx->trace_handles,
99 (gpointer) (unsigned long) handle->id,
100 handle);
1059a2bf
JD
101 ret = trace_collection_add(ctx->tc, td);
102 if (ret == 0)
103 return handle->id;
104end:
105 return ret;
b469d2dd
JD
106}
107
6cba487f 108void bt_context_remove_trace(struct bt_context *ctx, int handle_id)
b469d2dd 109{
6cba487f
MD
110 struct bt_trace_handle *handle;
111
112 handle = g_hash_table_lookup(ctx->trace_handles,
113 (gpointer) (unsigned long) handle_id);
114 assert(handle != NULL);
115
116 /* Remove from containers */
117 trace_collection_remove(ctx->tc, handle->td);
118 g_hash_table_remove(ctx->trace_handles,
119 (gpointer) (unsigned long) handle_id);
120
121 /* Close the trace */
122 handle->format->close_trace(handle->td);
123
124 /* Destory the handle */
125 bt_trace_handle_destroy(handle);
126}
127
128static
129void bt_context_destroy(struct bt_context *ctx)
130{
131 finalize_trace_collection(ctx->tc);
132
133 /* Remote all traces. The g_hash_table_destroy will call
134 * bt_trace_handle_destroy on each elements.
135 */
136 g_hash_table_destroy(ctx->trace_handles);
137
138 /* ctx->tc should always be valid */
139 assert(ctx->tc != NULL);
140 g_free(ctx->tc);
141 g_free(ctx);
b469d2dd
JD
142}
143
6cba487f 144void bt_context_get(struct bt_context *ctx)
b469d2dd 145{
6cba487f
MD
146 ctx->refcount++;
147}
b469d2dd 148
6cba487f
MD
149void bt_context_put(struct bt_context *ctx)
150{
b469d2dd
JD
151 ctx->refcount--;
152 if (ctx->refcount == 0)
6cba487f 153 bt_context_destroy(ctx);
b469d2dd 154}
This page took 0.029131 seconds and 4 git commands to generate.