summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/strace/epoll.go
blob: 5d51a7792161f74172ae3c1f855be94a08f489d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package strace

import (
	"fmt"
	"strings"

	"gvisor.dev/gvisor/pkg/abi"
	"gvisor.dev/gvisor/pkg/abi/linux"
	"gvisor.dev/gvisor/pkg/sentry/kernel"
	"gvisor.dev/gvisor/pkg/usermem"
)

func epollEvent(t *kernel.Task, eventAddr usermem.Addr) string {
	var e linux.EpollEvent
	if _, err := t.CopyIn(eventAddr, &e); err != nil {
		return fmt.Sprintf("%#x {error reading event: %v}", eventAddr, err)
	}
	var sb strings.Builder
	fmt.Fprintf(&sb, "%#x ", eventAddr)
	writeEpollEvent(&sb, e)
	return sb.String()
}

func epollEvents(t *kernel.Task, eventsAddr usermem.Addr, numEvents, maxBytes uint64) string {
	var sb strings.Builder
	fmt.Fprintf(&sb, "%#x {", eventsAddr)
	addr := eventsAddr
	for i := uint64(0); i < numEvents; i++ {
		var e linux.EpollEvent
		if _, err := t.CopyIn(addr, &e); err != nil {
			fmt.Fprintf(&sb, "{error reading event at %#x: %v}", addr, err)
			continue
		}
		writeEpollEvent(&sb, e)
		if uint64(sb.Len()) >= maxBytes {
			sb.WriteString("...")
			break
		}
		// Allowing addr to overflow is consistent with Linux, and harmless; if
		// this isn't the last iteration of the loop, the next call to CopyIn
		// will just fail with EFAULT.
		addr, _ = addr.AddLength(uint64(linux.SizeOfEpollEvent))
	}
	sb.WriteString("}")
	return sb.String()
}

func writeEpollEvent(sb *strings.Builder, e linux.EpollEvent) {
	events := epollEventEvents.Parse(uint64(e.Events))
	fmt.Fprintf(sb, "{events=%s data=[%#x, %#x]}", events, e.Data[0], e.Data[1])
}

var epollCtlOps = abi.ValueSet{
	linux.EPOLL_CTL_ADD: "EPOLL_CTL_ADD",
	linux.EPOLL_CTL_DEL: "EPOLL_CTL_DEL",
	linux.EPOLL_CTL_MOD: "EPOLL_CTL_MOD",
}

var epollEventEvents = abi.FlagSet{
	{Flag: linux.EPOLLIN, Name: "EPOLLIN"},
	{Flag: linux.EPOLLPRI, Name: "EPOLLPRI"},
	{Flag: linux.EPOLLOUT, Name: "EPOLLOUT"},
	{Flag: linux.EPOLLERR, Name: "EPOLLERR"},
	{Flag: linux.EPOLLHUP, Name: "EPOLLHUP"},
	{Flag: linux.EPOLLRDNORM, Name: "EPOLLRDNORM"},
	{Flag: linux.EPOLLRDBAND, Name: "EPOLLRDBAND"},
	{Flag: linux.EPOLLWRNORM, Name: "EPOLLWRNORM"},
	{Flag: linux.EPOLLWRBAND, Name: "EPOLLWRBAND"},
	{Flag: linux.EPOLLMSG, Name: "EPOLLMSG"},
	{Flag: linux.EPOLLRDHUP, Name: "EPOLLRDHUP"},
	{Flag: linux.EPOLLEXCLUSIVE, Name: "EPOLLEXCLUSIVE"},
	{Flag: linux.EPOLLWAKEUP, Name: "EPOLLWAKEUP"},
	{Flag: linux.EPOLLONESHOT, Name: "EPOLLONESHOT"},
	{Flag: linux.EPOLLET, Name: "EPOLLET"},
}