debug.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. #include "config.h"
  2. #include "debug.h"
  3. #include <algorithm>
  4. #include <array>
  5. #include <atomic>
  6. #include <cstring>
  7. #include <deque>
  8. #include <mutex>
  9. #include <optional>
  10. #include <stdexcept>
  11. #include <string>
  12. #include <string_view>
  13. #include <unordered_map>
  14. #include <utility>
  15. #include "AL/al.h"
  16. #include "AL/alc.h"
  17. #include "AL/alext.h"
  18. #include "alc/context.h"
  19. #include "alc/device.h"
  20. #include "alc/inprogext.h"
  21. #include "alnumeric.h"
  22. #include "alspan.h"
  23. #include "alstring.h"
  24. #include "auxeffectslot.h"
  25. #include "buffer.h"
  26. #include "core/logging.h"
  27. #include "core/voice.h"
  28. #include "direct_defs.h"
  29. #include "effect.h"
  30. #include "error.h"
  31. #include "filter.h"
  32. #include "intrusive_ptr.h"
  33. #include "opthelpers.h"
  34. #include "source.h"
  35. /* Declared here to prevent compilers from thinking it should be inlined, which
  36. * GCC warns about increasing code size.
  37. */
  38. DebugGroup::~DebugGroup() = default;
  39. namespace {
  40. static_assert(DebugSeverityBase+DebugSeverityCount <= 32, "Too many debug bits");
  41. template<typename T, T ...Vals>
  42. constexpr auto make_array_sequence(std::integer_sequence<T, Vals...>)
  43. { return std::array<T,sizeof...(Vals)>{Vals...}; }
  44. template<typename T, size_t N>
  45. constexpr auto make_array_sequence()
  46. { return make_array_sequence(std::make_integer_sequence<T,N>{}); }
  47. constexpr auto GetDebugSource(ALenum source) noexcept -> std::optional<DebugSource>
  48. {
  49. switch(source)
  50. {
  51. case AL_DEBUG_SOURCE_API_EXT: return DebugSource::API;
  52. case AL_DEBUG_SOURCE_AUDIO_SYSTEM_EXT: return DebugSource::System;
  53. case AL_DEBUG_SOURCE_THIRD_PARTY_EXT: return DebugSource::ThirdParty;
  54. case AL_DEBUG_SOURCE_APPLICATION_EXT: return DebugSource::Application;
  55. case AL_DEBUG_SOURCE_OTHER_EXT: return DebugSource::Other;
  56. }
  57. return std::nullopt;
  58. }
  59. constexpr auto GetDebugType(ALenum type) noexcept -> std::optional<DebugType>
  60. {
  61. switch(type)
  62. {
  63. case AL_DEBUG_TYPE_ERROR_EXT: return DebugType::Error;
  64. case AL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_EXT: return DebugType::DeprecatedBehavior;
  65. case AL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_EXT: return DebugType::UndefinedBehavior;
  66. case AL_DEBUG_TYPE_PORTABILITY_EXT: return DebugType::Portability;
  67. case AL_DEBUG_TYPE_PERFORMANCE_EXT: return DebugType::Performance;
  68. case AL_DEBUG_TYPE_MARKER_EXT: return DebugType::Marker;
  69. case AL_DEBUG_TYPE_PUSH_GROUP_EXT: return DebugType::PushGroup;
  70. case AL_DEBUG_TYPE_POP_GROUP_EXT: return DebugType::PopGroup;
  71. case AL_DEBUG_TYPE_OTHER_EXT: return DebugType::Other;
  72. }
  73. return std::nullopt;
  74. }
  75. constexpr auto GetDebugSeverity(ALenum severity) noexcept -> std::optional<DebugSeverity>
  76. {
  77. switch(severity)
  78. {
  79. case AL_DEBUG_SEVERITY_HIGH_EXT: return DebugSeverity::High;
  80. case AL_DEBUG_SEVERITY_MEDIUM_EXT: return DebugSeverity::Medium;
  81. case AL_DEBUG_SEVERITY_LOW_EXT: return DebugSeverity::Low;
  82. case AL_DEBUG_SEVERITY_NOTIFICATION_EXT: return DebugSeverity::Notification;
  83. }
  84. return std::nullopt;
  85. }
  86. constexpr auto GetDebugSourceEnum(DebugSource source) -> ALenum
  87. {
  88. switch(source)
  89. {
  90. case DebugSource::API: return AL_DEBUG_SOURCE_API_EXT;
  91. case DebugSource::System: return AL_DEBUG_SOURCE_AUDIO_SYSTEM_EXT;
  92. case DebugSource::ThirdParty: return AL_DEBUG_SOURCE_THIRD_PARTY_EXT;
  93. case DebugSource::Application: return AL_DEBUG_SOURCE_APPLICATION_EXT;
  94. case DebugSource::Other: return AL_DEBUG_SOURCE_OTHER_EXT;
  95. }
  96. throw std::runtime_error{"Unexpected debug source value "+std::to_string(al::to_underlying(source))};
  97. }
  98. constexpr auto GetDebugTypeEnum(DebugType type) -> ALenum
  99. {
  100. switch(type)
  101. {
  102. case DebugType::Error: return AL_DEBUG_TYPE_ERROR_EXT;
  103. case DebugType::DeprecatedBehavior: return AL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_EXT;
  104. case DebugType::UndefinedBehavior: return AL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_EXT;
  105. case DebugType::Portability: return AL_DEBUG_TYPE_PORTABILITY_EXT;
  106. case DebugType::Performance: return AL_DEBUG_TYPE_PERFORMANCE_EXT;
  107. case DebugType::Marker: return AL_DEBUG_TYPE_MARKER_EXT;
  108. case DebugType::PushGroup: return AL_DEBUG_TYPE_PUSH_GROUP_EXT;
  109. case DebugType::PopGroup: return AL_DEBUG_TYPE_POP_GROUP_EXT;
  110. case DebugType::Other: return AL_DEBUG_TYPE_OTHER_EXT;
  111. }
  112. throw std::runtime_error{"Unexpected debug type value "+std::to_string(al::to_underlying(type))};
  113. }
  114. constexpr auto GetDebugSeverityEnum(DebugSeverity severity) -> ALenum
  115. {
  116. switch(severity)
  117. {
  118. case DebugSeverity::High: return AL_DEBUG_SEVERITY_HIGH_EXT;
  119. case DebugSeverity::Medium: return AL_DEBUG_SEVERITY_MEDIUM_EXT;
  120. case DebugSeverity::Low: return AL_DEBUG_SEVERITY_LOW_EXT;
  121. case DebugSeverity::Notification: return AL_DEBUG_SEVERITY_NOTIFICATION_EXT;
  122. }
  123. throw std::runtime_error{"Unexpected debug severity value "+std::to_string(al::to_underlying(severity))};
  124. }
  125. constexpr auto GetDebugSourceName(DebugSource source) noexcept -> const char*
  126. {
  127. switch(source)
  128. {
  129. case DebugSource::API: return "API";
  130. case DebugSource::System: return "Audio System";
  131. case DebugSource::ThirdParty: return "Third Party";
  132. case DebugSource::Application: return "Application";
  133. case DebugSource::Other: return "Other";
  134. }
  135. return "<invalid source>";
  136. }
  137. constexpr auto GetDebugTypeName(DebugType type) noexcept -> const char*
  138. {
  139. switch(type)
  140. {
  141. case DebugType::Error: return "Error";
  142. case DebugType::DeprecatedBehavior: return "Deprecated Behavior";
  143. case DebugType::UndefinedBehavior: return "Undefined Behavior";
  144. case DebugType::Portability: return "Portability";
  145. case DebugType::Performance: return "Performance";
  146. case DebugType::Marker: return "Marker";
  147. case DebugType::PushGroup: return "Push Group";
  148. case DebugType::PopGroup: return "Pop Group";
  149. case DebugType::Other: return "Other";
  150. }
  151. return "<invalid type>";
  152. }
  153. constexpr auto GetDebugSeverityName(DebugSeverity severity) noexcept -> const char*
  154. {
  155. switch(severity)
  156. {
  157. case DebugSeverity::High: return "High";
  158. case DebugSeverity::Medium: return "Medium";
  159. case DebugSeverity::Low: return "Low";
  160. case DebugSeverity::Notification: return "Notification";
  161. }
  162. return "<invalid severity>";
  163. }
  164. } // namespace
  165. void ALCcontext::sendDebugMessage(std::unique_lock<std::mutex> &debuglock, DebugSource source,
  166. DebugType type, ALuint id, DebugSeverity severity, std::string_view message)
  167. {
  168. if(!mDebugEnabled.load(std::memory_order_relaxed)) UNLIKELY
  169. return;
  170. if(message.length() >= MaxDebugMessageLength) UNLIKELY
  171. {
  172. ERR("Debug message too long (%zu >= %d):\n-> %.*s\n", message.length(),
  173. MaxDebugMessageLength, al::sizei(message), message.data());
  174. return;
  175. }
  176. DebugGroup &debug = mDebugGroups.back();
  177. const uint64_t idfilter{(1_u64 << (DebugSourceBase+al::to_underlying(source)))
  178. | (1_u64 << (DebugTypeBase+al::to_underlying(type)))
  179. | (uint64_t{id} << 32)};
  180. auto iditer = std::lower_bound(debug.mIdFilters.cbegin(), debug.mIdFilters.cend(), idfilter);
  181. if(iditer != debug.mIdFilters.cend() && *iditer == idfilter)
  182. return;
  183. const uint filter{(1u << (DebugSourceBase+al::to_underlying(source)))
  184. | (1u << (DebugTypeBase+al::to_underlying(type)))
  185. | (1u << (DebugSeverityBase+al::to_underlying(severity)))};
  186. auto iter = std::lower_bound(debug.mFilters.cbegin(), debug.mFilters.cend(), filter);
  187. if(iter != debug.mFilters.cend() && *iter == filter)
  188. return;
  189. if(mDebugCb)
  190. {
  191. auto callback = mDebugCb;
  192. auto param = mDebugParam;
  193. debuglock.unlock();
  194. callback(GetDebugSourceEnum(source), GetDebugTypeEnum(type), id,
  195. GetDebugSeverityEnum(severity), static_cast<ALsizei>(message.length()), message.data(),
  196. param);
  197. }
  198. else
  199. {
  200. if(mDebugLog.size() < MaxDebugLoggedMessages)
  201. mDebugLog.emplace_back(source, type, id, severity, message);
  202. else UNLIKELY
  203. ERR("Debug message log overflow. Lost message:\n"
  204. " Source: %s\n"
  205. " Type: %s\n"
  206. " ID: %u\n"
  207. " Severity: %s\n"
  208. " Message: \"%.*s\"\n",
  209. GetDebugSourceName(source), GetDebugTypeName(type), id,
  210. GetDebugSeverityName(severity), al::sizei(message), message.data());
  211. }
  212. }
  213. FORCE_ALIGN DECL_FUNCEXT2(void, alDebugMessageCallback,EXT, ALDEBUGPROCEXT,callback, void*,userParam)
  214. FORCE_ALIGN void AL_APIENTRY alDebugMessageCallbackDirectEXT(ALCcontext *context,
  215. ALDEBUGPROCEXT callback, void *userParam) noexcept
  216. {
  217. std::lock_guard<std::mutex> debuglock{context->mDebugCbLock};
  218. context->mDebugCb = callback;
  219. context->mDebugParam = userParam;
  220. }
  221. FORCE_ALIGN DECL_FUNCEXT6(void, alDebugMessageInsert,EXT, ALenum,source, ALenum,type, ALuint,id, ALenum,severity, ALsizei,length, const ALchar*,message)
  222. FORCE_ALIGN void AL_APIENTRY alDebugMessageInsertDirectEXT(ALCcontext *context, ALenum source,
  223. ALenum type, ALuint id, ALenum severity, ALsizei length, const ALchar *message) noexcept
  224. try {
  225. if(!context->mContextFlags.test(ContextFlags::DebugBit))
  226. return;
  227. if(!message)
  228. throw al::context_error{AL_INVALID_VALUE, "Null message pointer"};
  229. auto msgview = (length < 0) ? std::string_view{message}
  230. : std::string_view{message, static_cast<uint>(length)};
  231. if(msgview.size() >= MaxDebugMessageLength)
  232. throw al::context_error{AL_INVALID_VALUE, "Debug message too long (%zu >= %d)",
  233. msgview.size(), MaxDebugMessageLength};
  234. auto dsource = GetDebugSource(source);
  235. if(!dsource)
  236. throw al::context_error{AL_INVALID_ENUM, "Invalid debug source 0x%04x", source};
  237. if(*dsource != DebugSource::ThirdParty && *dsource != DebugSource::Application)
  238. throw al::context_error{AL_INVALID_ENUM, "Debug source 0x%04x not allowed", source};
  239. auto dtype = GetDebugType(type);
  240. if(!dtype)
  241. throw al::context_error{AL_INVALID_ENUM, "Invalid debug type 0x%04x", type};
  242. auto dseverity = GetDebugSeverity(severity);
  243. if(!dseverity)
  244. throw al::context_error{AL_INVALID_ENUM, "Invalid debug severity 0x%04x", severity};
  245. context->debugMessage(*dsource, *dtype, id, *dseverity, msgview);
  246. }
  247. catch(al::context_error& e) {
  248. context->setError(e.errorCode(), "%s", e.what());
  249. }
  250. FORCE_ALIGN DECL_FUNCEXT6(void, alDebugMessageControl,EXT, ALenum,source, ALenum,type, ALenum,severity, ALsizei,count, const ALuint*,ids, ALboolean,enable)
  251. FORCE_ALIGN void AL_APIENTRY alDebugMessageControlDirectEXT(ALCcontext *context, ALenum source,
  252. ALenum type, ALenum severity, ALsizei count, const ALuint *ids, ALboolean enable) noexcept
  253. try {
  254. if(count > 0)
  255. {
  256. if(!ids)
  257. throw al::context_error{AL_INVALID_VALUE, "IDs is null with non-0 count"};
  258. if(source == AL_DONT_CARE_EXT)
  259. throw al::context_error{AL_INVALID_OPERATION,
  260. "Debug source cannot be AL_DONT_CARE_EXT with IDs"};
  261. if(type == AL_DONT_CARE_EXT)
  262. throw al::context_error{AL_INVALID_OPERATION,
  263. "Debug type cannot be AL_DONT_CARE_EXT with IDs"};
  264. if(severity != AL_DONT_CARE_EXT)
  265. throw al::context_error{AL_INVALID_OPERATION,
  266. "Debug severity must be AL_DONT_CARE_EXT with IDs"};
  267. }
  268. if(enable != AL_TRUE && enable != AL_FALSE)
  269. throw al::context_error{AL_INVALID_ENUM, "Invalid debug enable %d", enable};
  270. static constexpr size_t ElemCount{DebugSourceCount + DebugTypeCount + DebugSeverityCount};
  271. static constexpr auto Values = make_array_sequence<uint8_t,ElemCount>();
  272. auto srcIndices = al::span{Values}.subspan(DebugSourceBase,DebugSourceCount);
  273. if(source != AL_DONT_CARE_EXT)
  274. {
  275. auto dsource = GetDebugSource(source);
  276. if(!dsource)
  277. throw al::context_error{AL_INVALID_ENUM, "Invalid debug source 0x%04x", source};
  278. srcIndices = srcIndices.subspan(al::to_underlying(*dsource), 1);
  279. }
  280. auto typeIndices = al::span{Values}.subspan(DebugTypeBase,DebugTypeCount);
  281. if(type != AL_DONT_CARE_EXT)
  282. {
  283. auto dtype = GetDebugType(type);
  284. if(!dtype)
  285. throw al::context_error{AL_INVALID_ENUM, "Invalid debug type 0x%04x", type};
  286. typeIndices = typeIndices.subspan(al::to_underlying(*dtype), 1);
  287. }
  288. auto svrIndices = al::span{Values}.subspan(DebugSeverityBase,DebugSeverityCount);
  289. if(severity != AL_DONT_CARE_EXT)
  290. {
  291. auto dseverity = GetDebugSeverity(severity);
  292. if(!dseverity)
  293. throw al::context_error{AL_INVALID_ENUM, "Invalid debug severity 0x%04x", severity};
  294. svrIndices = svrIndices.subspan(al::to_underlying(*dseverity), 1);
  295. }
  296. std::lock_guard<std::mutex> debuglock{context->mDebugCbLock};
  297. DebugGroup &debug = context->mDebugGroups.back();
  298. if(count > 0)
  299. {
  300. const uint filterbase{(1u<<srcIndices[0]) | (1u<<typeIndices[0])};
  301. for(const uint id : al::span{ids, static_cast<uint>(count)})
  302. {
  303. const uint64_t filter{filterbase | (uint64_t{id} << 32)};
  304. auto iter = std::lower_bound(debug.mIdFilters.cbegin(), debug.mIdFilters.cend(),
  305. filter);
  306. if(!enable && (iter == debug.mIdFilters.cend() || *iter != filter))
  307. debug.mIdFilters.insert(iter, filter);
  308. else if(enable && iter != debug.mIdFilters.cend() && *iter == filter)
  309. debug.mIdFilters.erase(iter);
  310. }
  311. }
  312. else
  313. {
  314. auto apply_filter = [enable,&debug](const uint filter)
  315. {
  316. auto iter = std::lower_bound(debug.mFilters.cbegin(), debug.mFilters.cend(), filter);
  317. if(!enable && (iter == debug.mFilters.cend() || *iter != filter))
  318. debug.mFilters.insert(iter, filter);
  319. else if(enable && iter != debug.mFilters.cend() && *iter == filter)
  320. debug.mFilters.erase(iter);
  321. };
  322. auto apply_severity = [apply_filter,svrIndices](const uint filter)
  323. {
  324. std::for_each(svrIndices.cbegin(), svrIndices.cend(),
  325. [apply_filter,filter](const uint idx){ apply_filter(filter | (1<<idx)); });
  326. };
  327. auto apply_type = [apply_severity,typeIndices](const uint filter)
  328. {
  329. std::for_each(typeIndices.cbegin(), typeIndices.cend(),
  330. [apply_severity,filter](const uint idx){ apply_severity(filter | (1<<idx)); });
  331. };
  332. std::for_each(srcIndices.cbegin(), srcIndices.cend(),
  333. [apply_type](const uint idx){ apply_type(1<<idx); });
  334. }
  335. }
  336. catch(al::context_error& e) {
  337. context->setError(e.errorCode(), "%s", e.what());
  338. }
  339. FORCE_ALIGN DECL_FUNCEXT4(void, alPushDebugGroup,EXT, ALenum,source, ALuint,id, ALsizei,length, const ALchar*,message)
  340. FORCE_ALIGN void AL_APIENTRY alPushDebugGroupDirectEXT(ALCcontext *context, ALenum source,
  341. ALuint id, ALsizei length, const ALchar *message) noexcept
  342. try {
  343. if(length < 0)
  344. {
  345. size_t newlen{std::strlen(message)};
  346. if(newlen >= MaxDebugMessageLength)
  347. throw al::context_error{AL_INVALID_VALUE, "Debug message too long (%zu >= %d)", newlen,
  348. MaxDebugMessageLength};
  349. length = static_cast<ALsizei>(newlen);
  350. }
  351. else if(length >= MaxDebugMessageLength)
  352. throw al::context_error{AL_INVALID_VALUE, "Debug message too long (%d >= %d)", length,
  353. MaxDebugMessageLength};
  354. auto dsource = GetDebugSource(source);
  355. if(!dsource)
  356. throw al::context_error{AL_INVALID_ENUM, "Invalid debug source 0x%04x", source};
  357. if(*dsource != DebugSource::ThirdParty && *dsource != DebugSource::Application)
  358. throw al::context_error{AL_INVALID_ENUM, "Debug source 0x%04x not allowed", source};
  359. std::unique_lock<std::mutex> debuglock{context->mDebugCbLock};
  360. if(context->mDebugGroups.size() >= MaxDebugGroupDepth)
  361. throw al::context_error{AL_STACK_OVERFLOW_EXT, "Pushing too many debug groups"};
  362. context->mDebugGroups.emplace_back(*dsource, id,
  363. std::string_view{message, static_cast<uint>(length)});
  364. auto &oldback = *(context->mDebugGroups.end()-2);
  365. auto &newback = context->mDebugGroups.back();
  366. newback.mFilters = oldback.mFilters;
  367. newback.mIdFilters = oldback.mIdFilters;
  368. if(context->mContextFlags.test(ContextFlags::DebugBit))
  369. context->sendDebugMessage(debuglock, newback.mSource, DebugType::PushGroup, newback.mId,
  370. DebugSeverity::Notification, newback.mMessage);
  371. }
  372. catch(al::context_error& e) {
  373. context->setError(e.errorCode(), "%s", e.what());
  374. }
  375. FORCE_ALIGN DECL_FUNCEXT(void, alPopDebugGroup,EXT)
  376. FORCE_ALIGN void AL_APIENTRY alPopDebugGroupDirectEXT(ALCcontext *context) noexcept
  377. try {
  378. std::unique_lock<std::mutex> debuglock{context->mDebugCbLock};
  379. if(context->mDebugGroups.size() <= 1)
  380. throw al::context_error{AL_STACK_UNDERFLOW_EXT,
  381. "Attempting to pop the default debug group"};
  382. DebugGroup &debug = context->mDebugGroups.back();
  383. const auto source = debug.mSource;
  384. const auto id = debug.mId;
  385. std::string message{std::move(debug.mMessage)};
  386. context->mDebugGroups.pop_back();
  387. if(context->mContextFlags.test(ContextFlags::DebugBit))
  388. context->sendDebugMessage(debuglock, source, DebugType::PopGroup, id,
  389. DebugSeverity::Notification, message);
  390. }
  391. catch(al::context_error& e) {
  392. context->setError(e.errorCode(), "%s", e.what());
  393. }
  394. FORCE_ALIGN DECL_FUNCEXT8(ALuint, alGetDebugMessageLog,EXT, ALuint,count, ALsizei,logBufSize, ALenum*,sources, ALenum*,types, ALuint*,ids, ALenum*,severities, ALsizei*,lengths, ALchar*,logBuf)
  395. FORCE_ALIGN ALuint AL_APIENTRY alGetDebugMessageLogDirectEXT(ALCcontext *context, ALuint count,
  396. ALsizei logBufSize, ALenum *sources, ALenum *types, ALuint *ids, ALenum *severities,
  397. ALsizei *lengths, ALchar *logBuf) noexcept
  398. try {
  399. if(logBufSize < 0)
  400. throw al::context_error{AL_INVALID_VALUE, "Negative debug log buffer size"};
  401. auto sourcesOut = al::span{sources, sources ? count : 0u};
  402. auto typesOut = al::span{types, types ? count : 0u};
  403. auto idsOut = al::span{ids, ids ? count : 0u};
  404. auto severitiesOut = al::span{severities, severities ? count : 0u};
  405. auto lengthsOut = al::span{lengths, lengths ? count : 0u};
  406. auto logOut = al::span{logBuf, logBuf ? static_cast<ALuint>(logBufSize) : 0u};
  407. std::lock_guard<std::mutex> debuglock{context->mDebugCbLock};
  408. for(ALuint i{0};i < count;++i)
  409. {
  410. if(context->mDebugLog.empty())
  411. return i;
  412. auto &entry = context->mDebugLog.front();
  413. const size_t tocopy{entry.mMessage.size() + 1};
  414. if(logOut.data() != nullptr)
  415. {
  416. if(logOut.size() < tocopy)
  417. return i;
  418. auto oiter = std::copy(entry.mMessage.cbegin(), entry.mMessage.cend(), logOut.begin());
  419. *oiter = '\0';
  420. logOut = {oiter+1, logOut.end()};
  421. }
  422. if(!sourcesOut.empty())
  423. {
  424. sourcesOut.front() = GetDebugSourceEnum(entry.mSource);
  425. sourcesOut = sourcesOut.subspan<1>();
  426. }
  427. if(!typesOut.empty())
  428. {
  429. typesOut.front() = GetDebugTypeEnum(entry.mType);
  430. typesOut = typesOut.subspan<1>();
  431. }
  432. if(!idsOut.empty())
  433. {
  434. idsOut.front() = entry.mId;
  435. idsOut = idsOut.subspan<1>();
  436. }
  437. if(!severitiesOut.empty())
  438. {
  439. severitiesOut.front() = GetDebugSeverityEnum(entry.mSeverity);
  440. severitiesOut = severitiesOut.subspan<1>();
  441. }
  442. if(!lengthsOut.empty())
  443. {
  444. lengthsOut.front() = static_cast<ALsizei>(tocopy);
  445. lengthsOut = lengthsOut.subspan<1>();
  446. }
  447. context->mDebugLog.pop_front();
  448. }
  449. return count;
  450. }
  451. catch(al::context_error& e) {
  452. context->setError(e.errorCode(), "%s", e.what());
  453. return 0;
  454. }
  455. FORCE_ALIGN DECL_FUNCEXT4(void, alObjectLabel,EXT, ALenum,identifier, ALuint,name, ALsizei,length, const ALchar*,label)
  456. FORCE_ALIGN void AL_APIENTRY alObjectLabelDirectEXT(ALCcontext *context, ALenum identifier,
  457. ALuint name, ALsizei length, const ALchar *label) noexcept
  458. try {
  459. if(!label && length != 0)
  460. throw al::context_error{AL_INVALID_VALUE, "Null label pointer"};
  461. auto objname = (length < 0) ? std::string_view{label}
  462. : std::string_view{label, static_cast<uint>(length)};
  463. if(objname.size() >= MaxObjectLabelLength)
  464. throw al::context_error{AL_INVALID_VALUE, "Object label length too long (%zu >= %d)",
  465. objname.size(), MaxObjectLabelLength};
  466. switch(identifier)
  467. {
  468. case AL_SOURCE_EXT: ALsource::SetName(context, name, objname); return;
  469. case AL_BUFFER: ALbuffer::SetName(context, name, objname); return;
  470. case AL_FILTER_EXT: ALfilter::SetName(context, name, objname); return;
  471. case AL_EFFECT_EXT: ALeffect::SetName(context, name, objname); return;
  472. case AL_AUXILIARY_EFFECT_SLOT_EXT: ALeffectslot::SetName(context, name, objname); return;
  473. }
  474. throw al::context_error{AL_INVALID_ENUM, "Invalid name identifier 0x%04x", identifier};
  475. }
  476. catch(al::context_error& e) {
  477. context->setError(e.errorCode(), "%s", e.what());
  478. }
  479. FORCE_ALIGN DECL_FUNCEXT5(void, alGetObjectLabel,EXT, ALenum,identifier, ALuint,name, ALsizei,bufSize, ALsizei*,length, ALchar*,label)
  480. FORCE_ALIGN void AL_APIENTRY alGetObjectLabelDirectEXT(ALCcontext *context, ALenum identifier,
  481. ALuint name, ALsizei bufSize, ALsizei *length, ALchar *label) noexcept
  482. try {
  483. if(bufSize < 0)
  484. throw al::context_error{AL_INVALID_VALUE, "Negative label bufSize"};
  485. if(!label && !length)
  486. throw al::context_error{AL_INVALID_VALUE, "Null length and label"};
  487. if(label && bufSize == 0)
  488. throw al::context_error{AL_INVALID_VALUE, "Zero label bufSize"};
  489. const auto labelOut = al::span{label, label ? static_cast<ALuint>(bufSize) : 0u};
  490. auto copy_name = [name,length,labelOut](std::unordered_map<ALuint,std::string> &names)
  491. {
  492. std::string_view objname;
  493. auto iter = names.find(name);
  494. if(iter != names.end())
  495. objname = iter->second;
  496. if(labelOut.empty())
  497. *length = static_cast<ALsizei>(objname.size());
  498. else
  499. {
  500. const size_t tocopy{std::min(objname.size(), labelOut.size()-1)};
  501. auto oiter = std::copy_n(objname.cbegin(), tocopy, labelOut.begin());
  502. *oiter = '\0';
  503. if(length)
  504. *length = static_cast<ALsizei>(tocopy);
  505. }
  506. };
  507. if(identifier == AL_SOURCE_EXT)
  508. {
  509. std::lock_guard srclock{context->mSourceLock};
  510. copy_name(context->mSourceNames);
  511. }
  512. else if(identifier == AL_BUFFER)
  513. {
  514. ALCdevice *device{context->mALDevice.get()};
  515. std::lock_guard buflock{device->BufferLock};
  516. copy_name(device->mBufferNames);
  517. }
  518. else if(identifier == AL_FILTER_EXT)
  519. {
  520. ALCdevice *device{context->mALDevice.get()};
  521. std::lock_guard filterlock{device->FilterLock};
  522. copy_name(device->mFilterNames);
  523. }
  524. else if(identifier == AL_EFFECT_EXT)
  525. {
  526. ALCdevice *device{context->mALDevice.get()};
  527. std::lock_guard effectlock{device->EffectLock};
  528. copy_name(device->mEffectNames);
  529. }
  530. else if(identifier == AL_AUXILIARY_EFFECT_SLOT_EXT)
  531. {
  532. std::lock_guard slotlock{context->mEffectSlotLock};
  533. copy_name(context->mEffectSlotNames);
  534. }
  535. else
  536. throw al::context_error{AL_INVALID_ENUM, "Invalid name identifier 0x%04x", identifier};
  537. }
  538. catch(al::context_error& e) {
  539. context->setError(e.errorCode(), "%s", e.what());
  540. }